1

I'm using 2 websockets: one is running on default port (3000) and all is working fine. But now I want to add a new websocket listening to port 81.

Here is my current config:

# 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

}

Here is what I tried:

  • Add listen 81; under listen 80;
  • Add a new block like the first one and change 80 by 81
  • Add a new block server and changed proxy_pass with port 81
  • and other solutions like that

I all the time got an error like this one:

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.

I already tried to stop port 81, restart nginx etc...

Config: Ubuntu 18.04

Edit: In few words I'm running a Nodejs/Angular app already using a websocket (port 3000) which works fine. Now I want to add an additionnal websocket listening to a different port (81 or whatever). It works fine on local but not in prod (because of my nginx config).

Edit:

lsof -i :8988 returns nothing.

I edited my config to change port 81 to 8988 and restart the service. Same result:

504 Gateway Timeout (from service worker)

I ran lsof -i :8988 again:

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    25231 root   20u  IPv6 823853      0t0  TCP *:8988 (LISTEN)
11
  • do what it says - port 81 is already in use, show with lsof -i :81 what's using it. but the way is correct and you only need listen 80 for use ipv4/6 at the same time – djdomi May 11 at 11:57
  • @djdomi node 14165 root 20u IPv6 791217 0t0 TCP *:81 (LISTEN) I killed it but it started again each time i run service nginx restart – C.Norris May 11 at 12:39
  • NODE =! ngninx - look for a node service stop that and try again and for what Reason do you want also use a second port for the same site? – djdomi May 11 at 13:46
  • WebSocket is not the same thing as HTTP listener. You need to more clearly define what is your objective and what is the complete environment. Otherwise it is difficult to give an answer that is related to your root issue. – Tero Kilkanen May 11 at 15:36
  • @TeroKilkanen I updated my answer. I just got a bit confused with port 80 and 3000 sorry. Maybe what I need to do is add a new location block and change proxy_pass instead of server ? – C.Norris May 11 at 16:01
0

If, in case if i understood you correctly, you only want the same Host listen to additionally Port 81 - in case its NOT been already used, it would look like

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

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

In fact listen [::]:80; CAN be done, but it, it's already and only doing the same as listen 80; NGINX documentation

0.0.0.0:81 failed (98: Address already in use) Means that the Adress is already used by a other Programm this can be fixed by searching with lsof -i :81

The block shown above seems to me legit.

1
  • 1
    As I said, it's exactly what I tried. – C.Norris May 11 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.