Node.js “如何配置?”;ws";nginx nodejs服务器上的websocket?

Node.js “如何配置?”;ws";nginx nodejs服务器上的websocket?,node.js,nginx,websocket,Node.js,Nginx,Websocket,我目前正试图在nginx-debian远程服务器上部署我的NODEJS应用程序。 它在localhost中工作得非常好。 但是,要使websocket在远程nginx服务器中工作,我有一些困难。 我使用“ws”nodejs模块 以下是我声明websocket服务器端的方式: var WebSocket_ = require('ws'); var wss = new WebSocket_.Server({port: 40510}); 下面是我如何打开websocket客户端的: var ws =

我目前正试图在nginx-debian远程服务器上部署我的NODEJS应用程序。 它在localhost中工作得非常好。 但是,要使websocket在远程nginx服务器中工作,我有一些困难。 我使用“ws”nodejs模块

以下是我声明websocket服务器端的方式:

var WebSocket_ = require('ws');
var wss = new WebSocket_.Server({port: 40510});
下面是我如何打开websocket客户端的:

var ws = new WebSocket('ws://localhost:40510');
我知道我必须在我的nginx VPS上配置/etc/nginx/sites available/default:

是否需要添加websocket块位置并定义特定的proxipass? 如果是,怎么做

我是否必须用客户端代码中的另一条指令替换var“ws=newwebsocket('ws://localhost:40510');”


提前感谢您的回答

如果您已经有一个服务器块,请将其放入(通常放在可用站点或nginx.conf中):

现在,根据您的Nginx侦听端口,您将配置客户端IP/端口(默认情况下,Nginx侦听端口80)

完整配置文件(示例):

多谢各位

我已经使用了块位置,带有重定向代理反转:

    location / {


           proxy_pass http://localhost:8888;
           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;


            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
    }
我不应该改用我的域名吗? 比如:

var ws = new WebSocket('http://vincentfritz-dev.fr/ws');

非常感谢。您可以确认,我不需要更改指令“var wss=new WebSocket_uu.Server({port:40510})”?如果它现在在没有Nginx的情况下工作,它应该可以正常工作;侦听[:]:80默认_服务器;事实上,显然在客户端,websocket地址必须以ws://uncaughtdomexception:Failed构造“websocket”:URL的方案必须是“ws”或“wss”不允许使用“http”。
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    fastcgi_buffers 64 4K;

    server {
        listen       localhost:80 default_server;
        server_name  localhost;

        # Logs
        access_log /var/log/nginx/main.access.log;
        error_log /var/log/nginx/main.error.log;

        # Websocket
        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:40510;
        }
    }
}
    location / {


           proxy_pass http://localhost:8888;
           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;


            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
    }
var ws = new WebSocket('http://vincentfritz-dev.fr/ws');