【问题标题】:How do i configure "ws" websocket on nginx nodejs server?如何在 nginx nodejs 服务器上配置“ws”websocket?
【发布时间】:2018-11-26 09:04:14
【问题描述】:

我目前正在尝试在 nginx debian 远程服务器上部署我的 NODEJS 应用程序。 它在本地主机中运行良好。 但是我在让 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 = new WebSocket('ws://localhost:40510');"通过我的客户端代码中的另一条指令?

提前感谢您的回答!

【问题讨论】:

    标签: node.js nginx websocket


    【解决方案1】:

    如果你已经有一个 server 块,把它放在里面(通常在 sites-available 或 nginx.conf 里面):

    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;
    }
    

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

    var ws = new WebSocket('ws://localhost:80/ws');
    

    完整的配置文件(示例):

    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;
            }
        }
    }
    

    【讨论】:

    • 非常感谢。您确认,我不需要更改指令“var wss = new WebSocket_.Server({port: 40510});”中的任何内容?
    • 如果它现在在没有 Nginx 的情况下工作,它应该没问题。
    • 我的服务器监听配置:监听 80 default_server;听 [::]:80 default_server;
    • 其实显然是client端,websocket地址必须以ws开头://
    • 未捕获的 DOMException:无法构造“WebSocket”:URL 的方案必须是“ws”或“wss”。不允许使用“http”。
    【解决方案2】:

    非常感谢

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

        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');
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 2017-08-26
      • 2019-05-13
      • 2018-05-30
      • 2018-01-24
      • 2018-07-12
      • 2021-09-14
      相关资源
      最近更新 更多