正常连接WebSocket
未使用nginx反向代理
var ws = new WebSocket("ws://10.134.10.19:7771/chat?userId="+username);
使用Nginx反向代理
nginx配置文件添加如下内容
注意:我nginx监听80端口连接websocket失败,换其他端口正常,原因不详
server { listen 7772; server_name testserver; location /chat { proxy_pass http://10.134.10.19:7771/chat; proxy_read_timeout 300s; proxy_send_timeout 300s; 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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
连接websocket地址更改为
var ws = new WebSocket("ws://testserver:7772/chat?userId="+username);