修改 nginx.conf
文件
event{
...
}
stream{
upstream abc {
server 127.0.0.1:3000;
}
server {
listen 3001;
proxy_connect_timeout 1s;
proxy_timeout 5s;
proxy_pass abc;
}
}
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
...
}
主要是新增了 steam
塊和 map
部分的代碼
修改對應站點的vhost配置文件中 添加
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server{
....
location / {
proxy_pass http://127.0.0.1:3001;
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_connect_timeout 600s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
client_max_body_size 30m;
}
....
}
使用 curl測試 websocket
curl --no-buffer -H 'Connection: keep-alive, Upgrade' -H 'Upgrade: websocket' -v -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: websocket' http://websocket地址 ws | od -t c
問題
- 返回
Session ID unknown
nodejs連接socketio負載均衡報錯Session ID unknown 中看到
原本以為是因為多台socket服務器沒有連接到同一個io adapter導致session id互相不認識
我這里是因為2台socket服務器運行導致的,停止一個socket運行服務就解決了。
References
- nginx代理socket tcp/udp 參考 nginx.conf中配置 steam
- Nginx支持Socket轉發過程詳解 參考 nginx.conf中配置 steam
- 寶塔通過nginx 反向代理,由不加端口的url直接代理到 websocket服務 參考到 vhost中設置 對應的值
- curl測試websocket鏈接 curl 測試websocket連接