Home >Backend Development >PHP Tutorial >nginx 301 302 jump configuration method and summary
Reprinted 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 Transfer settings:
server {
listen 80;
server_name 123.com;
rewrite ^/(.*) http://456.com/$1 redirect;
access_log off;
}
Look at nginx 301 Detailed documentation for 302 jump
server {
server_name test.com;
rewrite ^/(.*) http://www.test1.com/$1 permanent;
}
last – Basically use this Flag .
break - Abort Rewirte and no longer continue matching
redirect - Return the HTTP status 302 of temporary redirection
permanent - Return the HTTP status 301 of permanent redirection
Nginx's redirection uses Nginx's HttpRewriteModule. Here is a brief explanation of how to use it. Method:
rewrite command
nginx's rewrite is equivalent to apache's rewriterule (in most cases, the original apache rewrite rules can be used directly with quotes). It can be used in server, location and IF conditional judgment blocks , the command format is as follows:
rewrite regular expression to replace the target flag tag
flag tag can use the following formats:
last – basically use this Flag.
break - Abort Rewirte and no longer continue matching
redirect - Return the HTTP status 302 of temporary redirection
permanent - Return the HTTP status 301 of permanent redirection
Special note:
last and break are used to implement URL rewriting, browser The URL address in the address bar remains unchanged, but the access path on the server side has changed;
redirect and permanent are used to implement URL jump, and the browser address bar will display the redirected URL address;
For example, the following Set nginx to redirect files under a certain directory to another directory. $2 corresponds to the corresponding string in the second bracket (.*):
location /download/ {
rewrite ^(/download/.*) /m/(.*)..*$ $1/nginx-rewrite/$2.gzbreak;
}
IF condition judgment of nginx redirection
You can use nginx’s IF condition judgment in both server and location situations. Condition It can be the following:
Regular expression
For example:
Matching judgment
~ is a case-sensitive match; !~ is a case-sensitive non-match
~* is a case-insensitive match; !~* is a case-insensitive match Write does not match
For example, the following setting nginx redirects to the /nginx-ie directory when the user uses ie:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
File and directory judgment
-f and !-f judge whether the file exists
-d and !-d judge whether the directory exists
-e and !-e judge whether the file or directory exists
-x and !-x judge Whether the file is executable
For example, the following sets nginx to redirect when the file and directory do not exist:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}
return
The above introduces the nginx 301 302 jump configuration method and summary, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.