# 妙境 AI 创作平台 Nginx 生产配置模板
# 使用前替换：
# - example.com
# - /etc/letsencrypt/live/example.com/fullchain.pem
# - /etc/letsencrypt/live/example.com/privkey.pem
# - 如脚本中修改了前端端口，请同步 proxy_pass 的 5000

limit_req_zone $binary_remote_addr zone=miaojing_auth:10m rate=10r/m;
limit_req_zone $binary_remote_addr zone=miaojing_email:10m rate=6r/m;
limit_req_zone $binary_remote_addr zone=miaojing_generation:10m rate=20r/m;
limit_req_zone $binary_remote_addr zone=miaojing_download:10m rate=60r/m;
limit_req_zone $binary_remote_addr zone=miaojing_admin:10m rate=120r/m;

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    server_name example.com www.example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;

    client_max_body_size 80m;
    keepalive_timeout 65;
    proxy_connect_timeout 60s;
    proxy_send_timeout 360s;
    proxy_read_timeout 360s;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=()" always;

    access_log /var/log/nginx/miaojing-access.log;
    error_log /var/log/nginx/miaojing-error.log warn;

    location = /api/auth/login {
        limit_req zone=miaojing_auth burst=20 nodelay;
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_set_header X-Forwarded-Proto https;
    }

    location = /api/auth/register {
        limit_req zone=miaojing_auth burst=20 nodelay;
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_set_header X-Forwarded-Proto https;
    }

    location ^~ /api/email/ {
        limit_req zone=miaojing_email burst=10 nodelay;
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_set_header X-Forwarded-Proto https;
    }

    location ^~ /api/generate/ {
        limit_req zone=miaojing_generation burst=30 nodelay;
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_set_header X-Forwarded-Proto https;
    }

    location = /api/download {
        limit_req zone=miaojing_download burst=120 nodelay;
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_set_header X-Forwarded-Proto https;
    }

    location ^~ /api/admin/ {
        limit_req zone=miaojing_admin burst=120 nodelay;
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_set_header X-Forwarded-Proto https;
    }

    location / {
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-Proto https;
    }
}
