Setting nginx reverse proxy with rewrite to apply to all links in site
I have the following Nginx config:
server {
listen 80;
location /test {
rewrite /test(.*) /$1 break;
proxy_pass "http://www.example.com/";
}
}
This works well for the home page and when entering 'http://localhost/test' I get the content of http://www.example.com/ and the URL remains 'http://localhost/test'.
Cool, but when I hit a link in the website, say /more_examples
I want the link to be rewritten as such: http://localhost/test/more_examples.
In practice, I get the following: http://localhost/more_examples, which obviously will not work. Is this even possible?
Any help would be greatly appreciated.
Solution 1:
I've tested your code and for me this works like you describe it:
location /test {
rewrite /test(.*) $1 break;
proxy_pass http://www.basicwebsiteexample.com;
}
I call http://localhost/test
and for sub pages like this http://localhost/test/media
.
I don't get any redirect or URL rewriting in my browser.
If you still get an URL redirect in your browser there must be a redirect in the website you're proxying or in your nginx config.