Nginx rewrite URL examples with and without redirect address

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.