Nginx Address redirection using

Source: Internet
Author: User
Tags http redirect tomcat server

Nginx address redirection using

When using Nginx as a server, the common need is to redirect addresses (301 redirects), while redirection can solve many server application requirements, and nginx it all simpler. So, what are some of our common redirects? Here I'll list a few common requirements: HTTP redirect to HTTPS, www auto complement, redirect support for HTTPS. Of course, there are also many people using rewrite rewrite addresses to implement redirection, but not recommended here, because it is in the different Nginx version is there is nothing else, and 301 is recommended, because it is the standard of HTTP/HTTPS communication protocol, and the server itself is not directly related.

L HTTP to HTTPS

L WWW Auto-complement

L Jump to HTTPS support

One, HTTP to HTTPS

HTTPS in the Nginx configuration, the reader can view the article "Nginx CA Certificate Usage Summary", here has its detailed implementation process. Among them, how to achieve HTTP to HTTPS switch, use the Nginx 301 redirect implementation, as follows (put in the nginx.conf of the server area):

if ($scheme = http) {

return 301https://$server _name$request_uri;

}

Ii. www auto-complement

www automatic completion, refers to the need to enter the WWW, only the first level of domain name can be automatically added to the front of the WWW, such as: XXX.com for the first class domain name, www.xxx.com two domain name, then the input xxx.com can automatically switch to www.xxx.com access. The above situation is the same domain name automatic positioning, in fact, we can directly in the domain name management platform, such as: Ali Cloud directly add a xxx.com record value, the host value set to @ can (recommended), not necessarily through the configuration Nginx implementation.

Another situation, that is, in the www.xxx.com redirect to the yyy.com domain name, and to achieve automatic positioning to the www.yyy.com level two domain name, the same can be the first domain name management platform to add yyy.com a domain name, similar operations above. Then, add the following configuration in Nginx:

server_name yyy.com

if ($host = ' yyy.com ') {

return to Http://www.yyy.com$request_uri;

}

If you want to jump to https at the same time, only the following configuration is required:

server_name yyy.com

if ($host = ' yyy.com ') {

return to Https://www.yyy.com$request_uri;

}

Third, jump to HTTPS support

Many times, we deploy the application, through its own provided by the redirect to implement the Jump function, if the jump before the HTTPS mode request, jump, then the protocol defaults to HTTP, resulting in a lot of need for HTTPS to access the operation can not be carried out. Then, at this time we need to configure nginx.conf, so that when the jump, support automatic from HTTP to HTTPS, in fact, the configuration is simpler, as follows:

location/xxx {

Proxy_set_header Host$host;

Proxy_set_headerx-forwarded-for $remote _addr;

Proxy_pass Https://www.yyy.com:port;

Index index.htmlindex.htm index.php;

Proxy_redirect http://$scheme://;

}

As shown above, this configuration is done using Nginx as a proxy server, passing Proxy_pass to the Tomcat server (this is a more common use). Then, by Proxy_redirect configuration, you can achieve the perfect application to use redirect jump, do retain the switch to HTTPS, because most of the application redirect only support the default HTTP protocol.

In fact, Nginx's 301 redirects can solve a lot of needs and require the reader to use it flexibly.

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.