diff --git a/docker/nginx.stage.conf b/docker/nginx.stage.conf index d779457..e18b363 100644 --- a/docker/nginx.stage.conf +++ b/docker/nginx.stage.conf @@ -292,6 +292,45 @@ server { proxy_set_header X-Forwarded-Proto https; } + # ============================================================ + # /ws/* → api-server :3721 (推論 inference WS + 既有 pairing/events/system WS) + # + # ⚠️ 為什麼必須有這條 location: + # 下方 catch-all `location /` 反代到 Next.js frontend (:3000)。若沒有這條, + # /ws/devices/:id/inference 等所有 /ws/* 會落到 frontend → Next 沒有此 route + # → 404,前端推論頁的 WS 握手斷掉。必須在 catch-all 之前用前綴 location 攔下, + # 導到 api-server (:3721),該處掛 wsAuthGroup(/ws/devices/:id/inference 需 auth) + # 與 registerWebSocketStubs(其餘 /ws/* 目前 501 stub)。 + # + # 與 /tunnel/connect 的關係:兩者是不同 path 前綴(/ws/ vs /tunnel/connect), + # nginx 前綴 location 各自最長匹配、互不 shadow。/ws/ 不會吃到 /tunnel/connect。 + # + # WS upgrade 三要素 + 長 timeout + buffering off,比照 /tunnel/connect。 + # $connection_upgrade 繼承自 http-context map(檔頭 38-41 行)。 + # cookie 由 proxy_pass 預設透傳(inference WS 走 cookie/session auth)。 + # ============================================================ + location /ws/ { + proxy_pass http://visiona_api; + proxy_http_version 1.1; + + # WebSocket upgrade headers + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + + # 推論 WS 為 long-lived 串流;拉長 timeout(比照 /tunnel/connect) + proxy_read_timeout 86400s; + proxy_send_timeout 86400s; + + # 不 buffer,避免延遲 WS 訊框 + proxy_buffering off; + + 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 https; + proxy_set_header X-Forwarded-Host $host; + } + # ============================================================ # Next.js hashed static assets — 永久 cache # /_next/static/{hash}.js 等 @@ -480,6 +519,29 @@ server { proxy_set_header X-Forwarded-Proto http; } + # ── /ws/* → api-server :3721(推論 inference WS + 其餘 /ws/* stub)── + # 完整比照 stage-9527 block 的 /ws/ 設定:http/1.1 + Upgrade + Connection + # + 86400s timeout + buffering off。$connection_upgrade 繼承自 http-context map。 + # 不會 shadow 上面的 /tunnel/connect(不同 path 前綴)。 + location /ws/ { + proxy_pass http://visiona_api; + proxy_http_version 1.1; + + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + + proxy_read_timeout 86400s; + proxy_send_timeout 86400s; + + proxy_buffering off; + + 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 http; + proxy_set_header X-Forwarded-Host $host; + } + # ── Next.js hashed static(內網瀏覽器用)── location /_next/static/ { proxy_pass http://visiona_frontend;