简体   繁体   中英

Serving multiple node apps with nginx on same domain

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

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.

I was facing the same problem, after spending time on research I wrote a blogpost where I explained with details how I solved it, I hope it helps. Here it is: http://blog.donaldderek.com/2013/08/cf-i-configure-your-staging-machine-with-node-js-and-nginx/

you should config your nginx file like this

server {
        listen 80;  
        server_name biger.yourdomain.cn;


        access_log      /data/log/nginx/access_ab.log;
        error_log       /data/log/nginx/error_ab.log;

        location /firstApp {
             proxy_store off;
             proxy_redirect off;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header Host $http_host;
             proxy_pass http://localhost:8001/;
        }

}

maeby you need add this code to your project

app.enable('trust proxy');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM