1

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 i.e. app.example.com or www.example.com It gives the same application i.e. 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?

9
  • Why do you need nginx at all to achieve this? You can simply bind both Node.js apps to the designated ports (4000/5010) and it would work since you can bind freely to any port < 1024 on Linux without root. Commented Nov 15, 2015 at 21:24
  • They are binded to different ports aren't they? I need the name resolution to happen on my own terms which is not happening. Commented Nov 15, 2015 at 21:33
  • No need for name resolution (virtual hosts) if the port is different - simply run both Node.js apps (on different ports), and then point www.example.com and app.example.com to the same server IP. Commented Nov 15, 2015 at 21:44
  • That's what I am doing. It's not working Commented Nov 15, 2015 at 21:49
  • Do both of them should be in nginx.conf file to work ? I have given both of them as separate file as www.example.com and app.example.com in sites-available and symlinked to sites-enabled folder? What is the difference in both methods Commented Nov 15, 2015 at 21:52

1 Answer 1

1

Here is something that is not given anywhere. You have to link your sites-enabled files into your ngnix.conf. Your sites-enabled files i.e. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.