1

I have multiple node instances running each on a different port. (8000, 8001, etc.) I also have nginx running on port 80. What I am trying to do is allow a user to enter my site via a single domain name, and then via different urls be able to access a different node server.

Currently everything works as following

http://example.com:8000/index.html (This is one node server running a site) http://example.com:8001/index.hmtl (this is another node server running a different site) 

What I want to be able to do is allow the user to type in a url path and auto direct them to the correct site without them having to specify the port. So for instance:

http://example.com/site1/inedx.html (This is one node server running a site at port 8000) http://example.com/site2/index.html (this is another node server running a different site at port 8001) 

I have been trying to achieve this with nginx proxy_pass rules but am having trouble getting it to work. Essentially nginx reroutes to the correct express server, but then all my routing in the express app breaks because it lacks the port.

So when I go to http://example.com/site1/index.html it takes me to http://example.com/index.html. Any help would be greatly appreciated. I have pasted the relevant portions of the nginx.conf and and example express route below.

nginx.conf

server { listen 80; server_name 11.11.11.111; root /home/ubuntu; location /site1/ { proxy_pass http://11.11.11.111:8000/; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $proxy_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://11.11.11.111:8000/; proxy_redirect http://11.11.11.111/* http://11.11.11.111:8000/*; } location /site2/ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $proxy_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://11.11.11.111:8001/; proxy_redirect http://11.11.11.111/* http://11.11.11.111:8001/*; } } 

app.js

app.get('/', function(req, res){ res.redirect('/index.html'); }); 
1
  • What do you mean by "it lacks the port" ? Your node instances should understand that when hit with a Host header all URLs and links must be relative to the domain and not to ip:port it's running on ... How could it work for links anyway ? You won't rewrite them with nginx too do you ? Commented Oct 13, 2014 at 19:47

1 Answer 1

1

When you set up proxying via nginx for site/ and site2/, nginx will transparently remove the path, unless you explicitly set it up in the proxy_pass directive.

For example:

location /site1/ { proxy_pass http://11.11.11.11:8000; } 

When you access example.com/site1, nginx will capture that request due to the location block specified above, and pass it to express. Express will see the request as /. /site1 is removed.

So if you want to maintain the /site1, either:

  • Specify this in your config, that the "relative path" is /site1, and pepper it throughout your code.
  • Or, change nginx to proxy_pass http://11.11.11.11:8000/site1

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.