Header RSS Feed
 
If you only want to see the articles of a certain category, please click on the desired category below:
ALL Android Backup BSD Database Hacks Hardware Internet Linux Mail MySQL Monitoring Network Personal PHP Proxy Shell Solaris Unix Virtualization VMware Windows Wyse

Nginx rewrite URL examples with and without redirect address
Monday - Jan 13th 2014 - by - (2 comments)

Nginx can handle the rewrite parameter differently, depending on the destination syntax. 

Here are some examples how to define redirects and URL rewrites in nginx.

server {
    server_name www.example.com;
    root /var/www/www.example.com;

    location / {
        rewrite ^/$ http://websrv1.example.com/mypage redirect;
    }
}

This will result in forwarding the browser to http://websrv1.example.com/mypage. The redirect address will be shown in the address bar.

Let's try this without a redirect or permanent option but 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 will still follow the URL and changes the URL in the address bar.
The reason for this is the http:// which is interpreted as external redirect.

So if you want to keep your domain and simply want to rewrite the URL (like in Apache with mod_rewrite), you must use a relative path:

server {
    server_name www.example.com;
    root /var/www/www.example.com;

    location / {
        rewrite ^/$ /mypage last;
    }
}

This will load the website for www.example.com from the subfolder /mypage within the document root (/var/www/www.example.com). 

But what if the destination website is 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 /) is passed to tomcat (the defined upstream server). Then the redirect for the root path (/) is 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.

 

Add a comment

Show form to leave a comment

Comments (newest first):

ck from St. Gallen, Switzerland wrote on Apr 8th, 2015:
Hi U-lis. It worked fine in my testing environment but now I do not have access to this environment from back then anymore. But what is also possible is that your application itself (mypage) is automatically causing the redirect, not nginx. You can try with the setting "proxy_redirect off" and do the debugging with curl -v, following all the redirects manually to find out where the redirect happens.

U-lis wrote on Mar 27th, 2015:
Hello, I have a question!

I tried your last server config to show example.com on address bar and load page from 127.0.0.1:8000/mypage.

But I got example.com/mypage on my addressbar with correct page I wanted.

Is Nginx config changed or my mistake?

I need your help. Thanks for your article.

== following is my nginx config, and I use python(gunicorn) webserver ==

upstream main { 127.0.0.1:8000 }
server {
listen 80;
server_name example.com;

location / {
include proxy_params;
proxy_pass http://main;
rewrite ^/$ /mypage last;
}
}


Go to Homepage home
Linux Howtos how to's
Monitoring Plugins monitoring plugins
Links links

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

7605 Days
until Death of Computers
Why?