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?
1 Answer
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;
}
}
8 Comments
user1200387
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.
user400654
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.
user1200387
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/[email protected]?v=9b05545ca1 200 2ms. Should both servers being hit on this request?
Bulkan
@user1200387 have you setup the
upstream's correctly ?user1200387
Do I need to add more then what you put in your answer?
|