Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 与nginx的Nodejs ws连接不工作_Node.js_Nginx_Websocket_Ws - Fatal编程技术网

Node.js 与nginx的Nodejs ws连接不工作

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;

我对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;
        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');
});
为了使我的客户端能够连接到服务器,我需要做哪些更改