5

Is there a way to use nginx as a router while keeping the requested domain in the URL? For example, if I hit mysite.com, the nginx routing server looks at the URL and directs traffic to a particular server, all while maintaining the original requested domain in the URL.

E.g.

mysite.com/site1/params
Router -> site1.mysite.com/params

But even though behind the scenes site1.mysite.com/params is being called, the user sees mysite.com/site1/params in the URL.

I've taken a stab at the configuration, but seem to be getting 404's.

upstream site1 {
  server site1.mysite.com;
}

location /site1 {
  rewrite ^(.*)$ /$1 break;
  proxy_pass  http://site1;
  proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  proxy_redirect off;
  proxy_buffering off;
  proxy_set_header        Host            $host;
  proxy_set_header        X-Real-IP       $remote_addr;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}

1 Answer 1

1

Use location with trailing slash, remove rewrite and use proxy_pass with / uri. Nginx will take of replacing /site1/ with /. Also, you may need to set Host header to site1.mysite.com not the $host.

location /site1/ {
  proxy_pass  http://site1/;
  proxy_set_header Host site1.mysite.com;
  ...
}
Sign up to request clarification or add additional context in comments.

7 Comments

This seems to redirect me to mysite.com:random-port
Just in addition to my last comment, it works if I manually type in port 80 to the initial URL, is there any way to get around that?
You should put port to upstream definition server site1.mysite.com:80;
Yeah, same issue when I try that : /. Perhaps it's cached.
Oops. Port 80 is default. Try to clear browser cache
|

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.