Nginx 301 302 Jump Configuration Method and summary

Source: Internet
Author: User
Transferred from: http://blog.sina.com.cn/s/blog_5d73ba76010145rr.html

First look at a complete code example, about Nginx 301 302 jump.

301 Jump Settings:

server {
Listen 80;
server_name 123.com;
Rewrite ^/(. *) http://456.com/$1 permanent;
Access_log off;
}

302 Jump Settings:

server {
Listen 80;
server_name 123.com;
Rewrite ^/(. *) http://456.com/$1 redirect;
Access_log off;
}

Take a look at the detailed documentation for Nginx 301 302 Jump

server {
server_name test.com;
Rewrite ^/(. *) http://www.test1.com/$1 permanent;
}

last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status of a temporary REDIRECT 302
permanent– returns the HTTP status of permanent redirection 301

Nginx redirection to the Nginx Httprewritemodule, the following simple explanation of how to use the following methods:
Rewrite command

Nginx's rewrite is equivalent to Apache's rewriterule (in most cases, the original Apache rewrite rules can be used directly with quotation marks), it can be used in server,location and if condition judgment block, The command format is as follows:
Rewrite regular expression replaces the target flag flag
The flag tag can be in several formats:
last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status of a temporary REDIRECT 302
permanent– returns the HTTP status of permanent redirection 301

Special attention:

Last and break are used to implement URL rewriting, the URL address of the browser address bar does not change, but the server-side access to the path has changed;

Redirect and permanent used to implement the URL jump, the browser address bar will show the URL after the jump address;


For example, the following setting Nginx redirects the files below a directory to another directory, with the corresponding string in the second parenthesis (. *):
location/download/{
Rewrite ^ (/download/.*)/m/(. *) \. *$ $1/nginx-rewrite/$2.gzbreak;
}
If condition judgment of nginx redirection

In the case of server and location two, you can use Nginx if condition to judge, the condition can be the following several kinds:
Regular expressions

Such as:
Match judgment
~ Case-sensitive matching;!~ is case-sensitive
~* for case-insensitive matching;!~* for case insensitive
For example, the following setting Nginx is redirected to the/nginx-ie directory using IE's use of the user:
if ($http _user_agent ~ MSIE) {
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
File and directory Judgments
-F and!-f determine if a file exists
-D and!-d determine if a directory exists
-E and!-e determine if a file or directory exists
-X and!-x determine if the file is executable
For example, the following setting Nginx redirects when the file and directory does not exist:
if (!-e $request _filename) {
Proxy_pass http://127.0.0.1;
}
Return

Return HTTP code, such as setting the Nginx anti-theft chain:
Location ~* \. (gif|jpg|png|swf|flv) $ {
Valid_referers none blocked www.test.com www.test1.com;
if ($invalid _referer) {
return 404;
}
}

The above describes the Nginx 301 302 Jump Configuration method and summary, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.