Question

How to set up nginx server to work with two nodejs in two different ports

Posted July 5, 2021 119 views
NginxNode.js

Hi.
I wrote two differents apps in nodejs that run in two different ports.
I would like to know how to configure nginx to redirect traffic to those apps.

The first app runs in the port 3000 and it is working fine. When the client type the url https://celr.com.br the traffic is going to the website.

The second app runs in the port 3001 and when the client tyep the url http://votolegal.celr.com.br it is directed to the same website, not to my second app.

The nginx conf file to the first domain is:

server {

        root /var/www/celr.com.br/html;
        index index.js index.html index.htm index.nginx-debian.html;

        server_name celr.com.br www.celr.com.br;

        location / {
#                try_files $uri $uri/ =404;
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;

        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/celr.com.br/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/celr.com.br/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.celr.com.br) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


        listen 80;
        listen [::]:80;

        server_name celr.com.br www.celr.com.br;
    return 404; # managed by Certbot




}

The nginx conf file to the domain votolegal.celr.com.br is:

server {
    listen 80;
    listen [::]:80;

    server_name votolegal.celr.com.br;

    root /var/www/celr.com.br/html;

    location / {
        proxy_pass http://localhost:3001;
    }
}

I have already read a lot of documents showing how to configure that.

I don´t know what I am doing wrong.

Could someone help me?

Thanks in advance

Claudinei

edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @claudineigoncalves,

Here’s an example that should work in your case:

upstream app_server_one {
    server 127.0.0.1:9000 fail_timeout=0;
}

upstream app_server_two {
    server 127.0.0.1:7000 fail_timeout=0;
}

server {
    listen 80 default_server;
    server_name: example.com;
    root /var/www//html;
    index index.html index.htm;

   # [snip...]
}

server {
    listen 80;
    server_name app1.example.com;

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_one;
    }
}

server {
    listen 80;
    server_name app2.example.com;

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_two;
    }
}

In the example, requests to example.com will serve static content from /var/www//html One to app1.example.com will serve a Django app listening on port 9000 and app2.example.com will serve the app listening on port 7000.

Just remember to update the ports and domains to match your own.

Let me know how it goes.

  • Dear KFSys

    The problem was related to the place where I put the app, both were in the same directory /var/www/domain/htm index.js running in the port 3000
    and app.js in the port 3001.

    I put the app.js in other directory and it worked well.

    Thanks