Original address: Http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VY9nfJeqqko
Nginx can handle the rewrite parameter differently, depending on the destination syntax.
Here is some examples how to define redirects and URLs rewrites in Nginx.
server {
server_name www.example.com;
root/var/www/www.example.com;
Location/{
Rewrite ^/$ http://websrv1.example.com/mypage redirect;
}
}
This would result in forwarding the browser to Http://websrv1.example.com/mypage. The redirect address is shown in the Address bar.
Let's try this without a redirect or Permanent option, with break or last:
server {
server_name www.example.com;
root/var/www/www.example.com;
Location/{
Rewrite ^/$ http://websrv1.example.com/mypage last;
}
}
Although the rewrite option is now set to last, the browser would still follow the URL and changes the URL in the address B Ar.
The reason for this is the http://which is interpreted as external redirect.
If you want to keep your domain and simply want to rewrite the URL (as in Apache with mod_rewrite), you must use a re Lative Path:
server {
server_name www.example.com;
root/var/www/www.example.com;
Location/{
Rewrite ^/$/mypage last;
}
}
This would load the website for www.example.com from the Subfolder/mypage within the document root (/var/www/www.example.c OM).
But what if the destination website are loaded from somewhere else, for example from a TOMCAT server in the background?
The following configuration covers this:
Upstream Tomcat {
Server 127.0.0.1:8080;
}
server {
server_name www.example.com;
root/var/www/www.example.com;
Location/{
Include proxy-settings.conf;
Proxy_pass Http://tomcat;
Rewrite ^/$/mypage last;
}
}
First everything (location/) was passed to Tomcat (the defined upstream server). Then the redirect for the root path (/) was happening and is relative to the path.
This results in keeping the browser's address URL at www.example.com but loads the website from 127.0.0.1:8080/mypage.
Nginx rewrite URL examples with and without redirect address