nginx支持websocket及websocket部分原理介紹
最近ipc通過websocket與server進行通行,經過無法通過nginx進行反向代理,只有直連nodejs端口。而且部署到阿里雲用了slb之后同樣存在該問題。因為部署為了規避風險不宜暴露nodejs端口,所以嘗試解決這個問題。
1.本地nginx 支持websocket
在實際的生產環境中,要求多個WebSocket服務器必須具有高性能和高可用,那么WebSocket協議就需要一個負載均衡層,NGINX從1.3開始支持WebSocket,其可以作為一個反向代理和為WebSocket程序做負載均衡。
配置nginx 即支持websocket的反向代理。使用這兩句開啟nginx對websocket的支持功能:
proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";
sample:
server { listen 80; location / { proxy_pass http://localhost:6000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
PS:在學習過程了解到有種情況websocket連上nginx會斷開的問題可能與 nginx這個參數配置有關proxy_read_timeout