I have two node js applications I am running on the same box and I would like for it to run the first node js app for all routing except if the url is www.domain.com/blog to go to the other node js application. Is this even possible with this setup or do I have to setup subdomains and use nginx or something?
Asked
Active
Viewed 3,161 times
4
-
Yes, this is very much possible using a reverse proxy. – Kevin B May 15 '15 at 00:57
1 Answers
4
You can achieve this using nginx as a reverse proxy.
Assuming you have your blog node process running on port 3000 and another node process on 3001 a simple config would look like;
upstream blog {
server 127.0.0.1:3000;
}
upstream other {
server 127.0.0.1:3001;
}
server {
listen 80;
server_name www.domain.com;
location /blog {
proxy_pass http://blog;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
location / {
proxy_pass http://other;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
}

Bulkan
- 2,555
- 1
- 20
- 31
-
When I try this my main node server works just fine, however my other one shows some promise but not quite there. When i go to /blog I get a "404 Page Not Found Go to the front page →" Then when I click go to the front page it sends me straight to localhost:3000. So clearly there is some link there. One caveat is I removed default_server proxy_protocol from the listen line because when it was there it broke, is this related? Also note I pretty much copied and pasted what you put there verbatim and threw it into my nginx conf file. – user1200387 May 15 '15 at 02:54
-
Make sure your node servers are configured to serve everything as if they are running from root, because from the node server's perspective, it is the root. Links in html however will need to be based on the out-side url, so, from /blog. – Kevin B May 15 '15 at 03:07
-
Looking at the logs when I hit the /blogs endpoint here is what I get: Blog App Log: GET /blog 301 0.571 ms - - GET /blog/ 404 9.134 ms - - Regular app log: GET /ghost/css/ghost.min.css?v=9b05545ca1 200 2ms GET /ghost/img/404-ghost@2x.png?v=9b05545ca1 200 2ms. Should both servers being hit on this request? – user1200387 May 15 '15 at 03:27
-
-
-
You just need to makes sure that the upstream configuration is correctly pointing to the port the node processes are starting on – Bulkan May 15 '15 at 17:17
-
Ok then it must be something else. Is there anything on the node side that I have to configure to ignore all "/blog" requests because it still prints log statements on both node processes when I hit "/blog" – user1200387 May 16 '15 at 02:44
-
For those with the "Cannot GET //api" error, just use `location /blog/` instead of `location /blog` – Guilherme Agostinelli Jan 20 '19 at 20:15