# ============================================
# LiteMall 轻商城 - Nginx HTTPS 配置
# ============================================
# 使用 certbot 申请证书后启用此配置
# 前置条件：
#   apt install certbot python3-certbot-nginx -y
#   certbot --nginx -d litemall.你的域名.com
# ============================================

server {
    listen 443 ssl http2;
    server_name litemall.你的域名.com;

    # SSL 证书（certbot 会自动填充）
    ssl_certificate /etc/letsencrypt/live/litemall.你的域名.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/litemall.你的域名.com/privkey.pem;

    # SSL 安全配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_session_tickets off;

    # HSTS
    add_header Strict-Transport-Security "max-age=63072000" always;

    # 静态资源缓存
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
        proxy_pass http://127.0.0.1:3001;
        expires 7d;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # API反向代理
    location /api/ {
        proxy_pass http://127.0.0.1:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 60s;
        proxy_buffering off;
    }

    # 前台页面
    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;

    client_max_body_size 20m;
    access_log /var/log/nginx/litemall-ssl-access.log;
    error_log /var/log/nginx/litemall-ssl-error.log;
}

# HTTP → HTTPS 重定向
server {
    listen 80;
    server_name litemall.你的域名.com;
    return 301 https://$server_name$request_uri;
}
