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

limit_req_zone $binary_remote_addr zone=api_limit:10m rate=60r/m;

server {
    listen 80;
    server_name _;

    client_max_body_size 50m;

    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;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;

    # API 代理到 Java 后端 (SSE 流式禁用缓冲)
    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 "";

        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 = /H6mMYlyS21.txt {
        root /opt/pharmacopoeia-ai/static;
        access_log off;
    }

    # 药品图片（DOCX 提取，新路径 /opt/static/images/）
    location /images/ {
        alias /opt/static/images/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # 前端静态页面
    location / {
        root /opt/pharmacopoeia-ai/static;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}
