Nginx反向代理WebSocket連結失敗問題
阿新 • • 發佈:2021-12-02
問題記錄:本地socket測試無誤後部署發現WebSocket connection to "xxx/xxx"failed
解決方案:
在nginx.conf的http模組新增如下內容
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
其次在反向配置中Nginx Location下新增如下程式碼
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection"upgrade";
從版本1.3.13開始,nginx實現特殊的操作模式,如果代理伺服器返回帶有程式碼101(交換協議)的響應,則允許在客戶端和代理伺服器之間建立隧道,
並且客戶端要求通過請求中的“升級”標頭:Nginx官網
events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; map $http_upgrade $connection_upgrade { defaultupgrade; '' close; } upstream jiaxun.com { server localhost:8082; } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location/realtime/websocket { proxy_pass http://jiaxun.com; proxy_http_version 1.1; proxy_connect_timeout 4s; proxy_read_timeout 3600s; #預設60s沒有傳輸資料就會關閉,延長時間 proxy_send_timeout 12s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }