upstream backend {
    server 127.0.0.1:8000;
    keepalive 64;
}

server {
    listen 80;
    server_name _;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name your-domain.com;

    ssl_certificate     /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    client_max_body_size 50m;

    # Gzip
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript image/svg+xml;

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # API 反向代理
    location /api/ {
        proxy_pass http://backend;
        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;
        proxy_set_header Connection "";

        # SSE 支持：禁用缓冲
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 300s;

        # 限流
        limit_req zone=api_limit burst=20 nodelay;
    }

    # 健康检查
    location /health {
        proxy_pass http://backend;
        access_log off;
    }

    # 管理后台
    location /admin {
        alias /var/www/admin/dist;
        try_files $uri $uri/ /admin/index.html;
        index index.html;
    }
}

# 限流区域定义
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=60r/m;
