# Nginx 环境配置模板
# 使用环境变量进行配置替换

server {
    listen ${NGINX_PORT:-80};
    server_name ${SERVER_NAME:-localhost};
    root /usr/share/nginx/html;
    index index.html;

    # 安全头设置
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

    # 健康检查端点
    location /health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }

    # API 代理到后端
    location ${API_PREFIX:-/prod-api}/ {
        proxy_pass http://${BACKEND_HOST:-qaup-app}:${BACKEND_PORT:-8080}/;
        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_connect_timeout ${PROXY_CONNECT_TIMEOUT:-30s};
        proxy_send_timeout ${PROXY_SEND_TIMEOUT:-60s};
        proxy_read_timeout ${PROXY_READ_TIMEOUT:-60s};
        
        # 缓冲设置
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
        proxy_busy_buffers_size 8k;
        
        # 请求体大小限制
        client_max_body_size ${MAX_UPLOAD_SIZE:-20M};
    }

    # 静态资源缓存
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires ${STATIC_CACHE_TIME:-1y};
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # 前端路由处理（SPA）
    location / {
        try_files $uri $uri/ /index.html;
        
        # HTML 文件不缓存
        location ~* \.html$ {
            add_header Cache-Control "no-cache, no-store, must-revalidate";
            add_header Pragma "no-cache";
            add_header Expires "0";
        }
    }

    # 错误页面
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}