Running multiple express apps through nginx showing port already in use

0

Issue

Here’s my nginx file:

server {
    server_name domain.net www.domain.net;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:3000;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.net/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
}
server {
    if ($host = www.domain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = domain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name domain.net www.domain.net;
    return 404; # managed by Certbot
}

server {
   listen 4000;
   server_name <I input the IP address of the server here without http or slash at the end>;    

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://localhost:4000;
   }
}

When I visit domain.net, it’s showing whatever app I’m running on port 3000, however, when I run npm start in the app that has port 4000 in it, it’s returning "port already in use".
I don’t know why this is happening.

Solution

This is happening because the NGINX process as well as the Node process trying to bind to port 4000. If you start NGINX before your node process node will fail to start with this error message or vise versa.

If you want to proxy your nodeJS app with NGINX and they are running on the same server you have to use two different ports.

You can not have two different processes listening on the same port at the same time… how do you think the request will find the right service?

Answered By – Timo Stark

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More