0

I currently have a AngularJS application running on my virtual private server. To run this I'm using NGINX, Node and 'http-server' and pm2 to keep everything running.

So I start my backend using for example 'node server.js' (port 3000) and I setup a second process of 'http-server' (which I think runs on port 8080) in the public folder.

This is the configuration of my NGINX:

server {
listen 80;
server_name mywebsite.be;

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://127.0.0.1:3000/;
}
}

Now I would like to setup a second application which is made with Angular (2). I'm trying to also run my backend with node on port 3001 and then starting http-server on port 8081.

How should I configure my NGINX?

0

Just add a second location with the correct proxy_pass directive (based on your configuration):

location /otherlocation {
    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://127.0.0.1:3001/;
}

You can match on location or if you wish, on a second diffrent domain.

  • Thanks for your answer. It looks like it's kind of working, though when I try an API call it looks like somehow NGINX adds a /. So my call only works when I call for example /otherlocationapi while it should work when I call /otherlocation/api . Any idea what's causing this? – Nicholas Oct 23 '17 at 19:38
  • It looks like I had to add a / to the end of the path! – Nicholas Oct 23 '17 at 19:49
0

Federico's answer helped me further but I also wanted to post my final solution.

I setup two locations right now, one for the backend (api) and one for the gui.

location /backend/ {
    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://127.0.0.1:3001/;
}
location /gui/ {
    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://127.0.0.1:4200/;
}

This works for me for now! :)

  • Hi! Glad it works with your setup. not sure why you need to add an extra / , I will investigate... – Federico Galli Oct 24 '17 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.