linux – Nginx – Add new port for websocket

I’m using 2 websockets: one is running on default port (80) 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
  • 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