nginx 可以转发websocket
nginx 配置,注意代理端口后面不要有“/”
location /mysocket/ {
proxy_pass http://xxx.com:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
socket.io client
'/'代表http:xxx.com/,使用path选择,替换默认的socket.io选项,不需要使用ws:xxx.com:3000这类地址,直接使用http,或者https,socket.io会自动将其转换成websocket的
let socket= io('/',{
path: '/mysocket',
query: {
args: "122"
}
});
socket.io server
const io = require('socket.io')(server,{
path: '/mysocket'
});