# --- HTTP 重定向到 HTTPS ---
server {
    listen 80;
    server_name _; # 匹配所有域名/IP

    # 将所有 HTTP 请求强制跳转到 HTTPS
    return 301 https://$host$request_uri;
}

# --- HTTPS 服务器配置 ---
server {
    listen 443 ssl;
    server_name _;

    # 1. SSL 证书配置
    ssl_certificate     /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    # SSL 优化配置 (可选)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    # 允许上传大文件
    client_max_body_size 20M;

    # 开启 Gzip
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 6;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript image/jpeg image/gif image/png;

    # 2. 前端 Vue 页面
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    # 3. 后端 API 接口代理
    location /api/ {
        proxy_pass http://backend:8000;

        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; # 告诉后端这是 HTTPS 请求

        proxy_connect_timeout 300s;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }

    # 4. 图片资源托管
    location /uploads/ {
        alias /usr/share/nginx/html/uploads/;
        expires 30d;
    }
}