Node.js 与nginx的Nodejs ws连接不工作
我对nginx比较陌生。我在本地机器上制作了一个WS-server,它工作得很好。但在将代码推送到服务器时,客户端无法连接到WebSocket服务器 我在nginx服务器块中做了一些更改,看起来是这样的:Node.js 与nginx的Nodejs ws连接不工作,node.js,nginx,websocket,ws,Node.js,Nginx,Websocket,Ws,我对nginx比较陌生。我在本地机器上制作了一个WS-server,它工作得很好。但在将代码推送到服务器时,客户端无法连接到WebSocket服务器 我在nginx服务器块中做了一些更改,看起来是这样的: server_name chat.app.ga www.chat.app.ga; location /ws { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade;
server_name chat.app.ga www.chat.app.ga;
location /ws {
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;
proxy_pass http://localhost:5001/ws;
}
location / {
proxy_pass "http://localhost:5001";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
WSServer在节点和端口5001上实现。
WSS代码:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
为了使我的客户端能够连接到服务器,我需要做哪些更改