# 巡樓 Console nginx（容器內）：serve 前端靜態檔 + /api 反代到 gateway 容器。
# upstream 走 compose service name（gateway:8890），不是 127.0.0.1。

upstream haixun_gateway {
    server gateway:8890;
    keepalive 32;
}

server {
    listen 80;
    listen [::]:80;
    server_name _;

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

    # 安全標頭
    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 X-XSS-Protection "0" always;

    # 靜態資源快取（vite build 帶 hash 檔名）
    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # API 反向代理（一般 JSON 與 SSE 共用）
    location /api/ {
        proxy_pass http://haixun_gateway;
        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;

        # Authorization（provider token）與 X-Member-Authorization（會員 JWT）由 nginx 預設轉發；
        # 以下確保 SSE 串流不被緩衝、長連線不被提前中斷。
        proxy_set_header Connection "";
        proxy_buffering off;
        proxy_cache off;
        chunked_transfer_encoding off;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }

    # SPA：所有非檔案路徑都回 index.html，交給 react-router
    location / {
        try_files $uri $uri/ /index.html;
    }
}
