server {
    listen 80;
    server_name _;
    # 不暴露 nginx 版本号(server_tokens 同时影响错误页与 Server 头)。
    server_tokens off;

    # Keep Location headers relative (Location: /admin/) instead of letting
    # nginx absolute_redirect rebuild them from $host + the listening port.
    # The gateway is commonly published behind a non-standard port (e.g. 18081),
    # and an absolute redirect would drop that port and send browsers to :80.
    absolute_redirect off;

    # 256 MiB 必须盖过文件上传上限 S3_MAX_FILE_BYTES（默认 128 MiB）。文件体
    # 由网关的流式上传处理器把关（http.MaxBytesReader + LimitReader），
    # nginx 只做最外层限制，避免大文件在到达网关前就被 413 拒绝。
    client_max_body_size 256m;

    root /usr/share/nginx/html;
    index index.html;

    # 基础安全响应头:防 MIME 嗅探、防点击劫持、限制 Referer 泄露来源页面。
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options DENY always;
    add_header Referrer-Policy same-origin always;

    location = / {
        return 302 /__APP__/;
    }

    location = /__APP__ {
        return 301 /__APP__/;
    }

    location = /healthz {
        proxy_pass http://gateway-api:8080/healthz;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location = /readyz {
        proxy_pass http://gateway-api:8080/readyz;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /api/ {
        proxy_pass http://gateway-api:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Request-ID $request_id;
        proxy_set_header X-Forwarded-Proto $scheme;
        # nginx 是控制台（/api/）的唯一入口：用 $remote_addr 覆盖 X-Forwarded-For，
        # 避免客户端自带 X-Forwarded-For 头伪造来源 IP、绕过登录限流。
        proxy_set_header X-Forwarded-For $remote_addr;
    }

    location /v1/ {
        proxy_pass http://gateway-api:8080;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_read_timeout 600s;
        proxy_set_header Host $host;
        proxy_set_header X-Request-ID $request_id;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # Vite emits assets and client-side routes below /admin/ or /portal/.
    location /__APP__/ {
        try_files $uri $uri/ /__APP__/index.html;
    }

    location / {
        return 404;
    }
}
