简体   繁体   中英

How to set up two node JS applications on my nginx server

I have a node js application running successfully on app.example.com on port 4000. Now I want to run another node js application on www.example.com on port 5010. How would I do it?

My attempt. Create two files in sites-available folder. one is www.example.com and one is app.example.com content of the files.

app.example.com

server {
        listen  80;

        server_name app.example.com;

        location / {
        proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://localhost:4000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
}
}

If I have this one alone. It works. Now adding
www.example.com

server {
        listen  80;

        server_name www.example.com;

        location / {
        proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://localhost:5010;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
}
}

And both node apps are running on running on forever on respective port. Now it doesn't matter what url I give ie app.example.com or www.example.com It gives the same application ie Hello World. How do I achieve having two different applications on two different ports?

EDIT It's happening if I give www.example.com:5010, but I want it without having to type 5010. How would I achieve that?

Here is something that is not given anywhere. You have to link your sites-enabled files into your ngnix.conf. Your sites-enabled files ie www.domain.com, app1.domain.com app2.domain.com will not work . Unless you include them in nginx.conf. Something like this.

include /etc/nginx/sites-enabled/*.conf;
include /etc/nginx/sites-enabled/app.example.com;
include /etc/nginx/sites-enabled/www.example.com;

and your app.example.com and www.example.com sites-available file will look like that posted in question. At last run your node apps from the port mentioned in your node apps. Don't forget to have same port numbers in your app.domain.com and www.domain.com otherwise you will get a 502 Bad gateway error. after that sudo service nginx restart And you are good to go.

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