skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    I ended up doing:

    server {
       listen <server-ip>:80;
    
       location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_pass http://localhost:4000;
       }
    }
    

    And running my app on the main IP port (80). If anyone knows a solution to my original question, I would really appreciate it.


  2. 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?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search