Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving multiple node apps with nginx on same domain

Tags:

node.js

nginx

I would like to host 2 different node applications with nginx from the same domain and am having some trouble. I would like to have:

mydomain.com point to node app firstApp and otherapp.mydomain.com point to node app otherapp

Right now, I can access firstApp just fine, but I cannot access otherapp via otherapp.mydomain.com.

My config for firstApp looks like this:

upstream firstApp{
    server 127.0.0.1:8123;
}

server{
    server_name mydomain.com;
    access_log /var/log/nginx/me.log;

    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://firstApp/;
        proxy_redirect off;
    }
}

My config for otherapp looks like this:

upstream otherapp{
    server 127.0.0.1:8124;
}

server{
    server_name otherapp.mydomain.com;
    access_log /var/log/nginx/me.log;

    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://otherapp/;
        proxy_redirect off;
    }
}

I have created both configurations in the nginx sites-available directory, they are both linked in the sites-enabled directory, and I have restarted nginx. Can someone tell me what I'm doing wrong?

Thanks, Swaraj

like image 790
Swaraj Avatar asked Aug 13 '13 00:08

Swaraj


People also ask

Why use nginx?

Because it can handle a high volume of connections, NGINX is commonly used as a reverse proxy and load balancer to manage incoming traffic and distribute it to slower upstream servers – anything from legacy database servers to microservices.

Does node run on Nginx?

Reverse proxy server — As traffic to your app increases, the best approach to improve performance is to use Nginx as a reverse proxy server in front of the Node. js server to load balance traffic across the servers. This is the core use case of Nginx in Node.


1 Answers

Just found out what the problem was. Though my nginx configs were correct, I had not added my desired subdomain to my domain name provider (namecheap). I added my subdomain on namecheap, and everything is working correctly now.

like image 145
Swaraj Avatar answered Sep 29 '22 09:09

Swaraj