Nginx - 为 websocket 添加新端口

Nginx - 为 websocket 添加新端口

我使用了 2 个 websocket:一个在默认端口 (3000) 上运行,一切运行正常。但现在我想添加一个监听端口 81 的新 websocket。

以下是我的当前配置:

# Redirection
server {
    listen 80;
    listen [::]:80;
    server_name hello.com www.hello.com;

    return 301 https://$host$request_uri;
}

# Config
server {
     server_name hello.com www.hello.com;

     root /home/me/hello/gui;

     index index.html index.htm;

     # Audio files
     location ~ \.wav$ {
       max_ranges 0;
     }

     location / {
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $http_host;
       proxy_set_header X-NginX-Proxy true;
       proxy_pass    http://1.2.3.4:3000/;

       # Websocket
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
    }

    if ($host = 'www.hello.com') {
       return 301 https://hello.com$request_uri;
    }

    # Custom error pages
    error_page 404 /404.html;
    location /404.html {
      root /var/www/html;
    }
    error_page 500 502 503 504 /500.html;
    location /500.html {
      root /var/www/html;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/hello.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/hello.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

以下是我尝试过的:

  • 添加listen 81;listen 80;
  • 像第一个一样添加一个新块,并将 80 更改为 81
  • 添加新的块服务器并更改proxy_pass为端口 81
  • 以及其他类似的解决方案

我总是收到这样的错误:

May 11 10:49:26 vps-7f2b967f systemd[1]: Starting A high performance web server and a reverse proxy server...
May 11 10:49:26 vps-7f2b967f nginx[13879]: nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Address already in use)
May 11 10:49:27 vps-7f2b967f nginx[13879]: nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Address already in use)
May 11 10:49:27 vps-7f2b967f nginx[13879]: nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Address already in use)
May 11 10:49:28 vps-7f2b967f nginx[13879]: nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Address already in use)
May 11 10:49:28 vps-7f2b967f nginx[13879]: nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Address already in use)
May 11 10:49:29 vps-7f2b967f nginx[13879]: nginx: [emerg] still could not bind()
May 11 10:49:29 vps-7f2b967f systemd[1]: nginx.service: Control process exited, code=exited status=1
May 11 10:49:29 vps-7f2b967f systemd[1]: nginx.service: Failed with result 'exit-code'.
May 11 10:49:29 vps-7f2b967f systemd[1]: Failed to start A high performance web server and a reverse proxy server.

我已经尝试停止端口 81、重新启动 nginx 等......

配置:Ubuntu 18.04

编辑:简而言之,我正在运行一个 Nodejs/Angular 应用程序,它已经使用 websocket(端口 3000),运行良好。现在我想添加一个额外的 websocket 来监听不同的端口(81 或其他)。它在本地运行良好,但在生产环境中却不行(因为我的 nginx 配置)。

编辑:

lsof -i :8988不返回任何内容。

我编辑了配置,将端口 81 更改为 8988,然后重新启动服务。结果相同:

504 网关超时(来自服务工作者)

lsof -i :8988又跑了:

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    25231 root   20u  IPv6 823853      0t0  TCP *:8988 (LISTEN)

答案1

如果我理解正确的话,你只希望同一主机另外监听 81 端口——如果它还没有被使用,它看起来就像

# the Block will look like
server {
    listen 80;
    listen 81;
    server_name hello.com www.hello.com;

    return 301 https://$host$request_uri;
}

事实上listen [::]:80; 可以做到,但它已经并且只做了同样的事情listen 80; NGINX 文档

0.0.0.0:81 失败(98:地址已被使用)意味着该地址已被其他程序使用,可以通过搜索来修复lsof -i :81

上面显示的区块对我来说似乎是合法的。

相关内容