我目前正在尝试在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块位置并定义一个特定的代理吗? 如果是,怎么办?
我是否必须替换var“ ws = new WebSocket('ws:// localhost:40510');”通过我的客户端代码中的另一条指令?
提前感谢您的回答!
答案 0 :(得分:0)
如果您已经有一个服务器块,请将其放入内部(通常在sites-available或nginx.conf中):
first
现在,根据您的Nginx监听端口,您将配置客户端IP /端口(默认情况下,Nginx监听端口80)
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;
}
完整的配置文件(示例):
var ws = new WebSocket('ws://localhost:80/ws');
答案 1 :(得分:0)
非常感谢
我已经使用了一个块位置,并使用了一个反向重定向:
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');
?