nginx 代理 nodejs 的 websocket 服务

在使用 nodejswebsocket 服务的时候, 用了 nginx 做代理, 发现 浏览器报错了

1
websocket.js:111 WebSocket connection to 'wss://api.gg/socket.io/?EIO=3&transport=websocket&sid=Wa-r7QOs37OgwtToAAAD' failed: Error during WebSocket handshake: Unexpected response code: 400

解决方案是 修改 nginx 配置 文件

1
2
3
4
5
6
7
8
9
location / {
proxy_pass http://localhost:3001;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass_request_headers on;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
client_max_body_size 2048m;
}

主要是 设置 proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";

至此完成了