Nginx 301 302 redirect jump configuration tutorial

  

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

301 Jump Settings:

server { listen 80; server_name phpky.com; rewrite ^/(.*) http://www.45it.net/$1 permanent; access_log off; }

302 Jump Settings:

server { listen 80; server_name phpkyr.com; rewrite ^/(.*) http://www.45it.net/$1 redirect; access_log off ; }

See the detailed documentation on the nginx 301 302 jump

server { server_name 45it.net; rewrite ^/(.*) http://www.45it.net /$1 permanent; }

last – Basically use this Flag. Break – aborts Rewirte, does not continue to match redirect – returns temporary redirected HTTP status 302 permanent – returns permanent redirected HTTP status 301

Nginx redirect uses Nginx's HttpRewriteModule, as explained below The following how to use the method: rewrite command

nginx rewrite is equivalent to apache rewriterule (in most cases you can use the original apache rewrite rule with quotes can be used directly), it can be used in the server, In the location and IF conditional decision block, the command format is as follows: rewrite The regular expression replaces the target flag tag The flag tag can be used in the following formats: last – Basically use this Flag. Break – abort Rewirte, not continue matching redirect – return temporary redirected HTTP status 302 permanent – return permanent redirected HTTP status 301


Special Note:

last and break are used to implement URL rewriting, the URL address of the browser address bar is unchanged, but the path of the server-side access has changed;

redirect and permanent are used to implement URL jump, browse The address bar will display the URL address after the jump;

For example, the following section sets nginx to redirect files under one directory to another directory, and $2 corresponds to the second bracket (.*). Corresponding string: location /download/{ rewrite ^(/download/.*)/m/(.*)\\..*$ $1/nginx-rewrite/$2.gz break; } IF condition judgment of nginx redirection

In the case of server and location, you can use the IF condition of nginx to judge. The conditions can be as follows: Regular expression

For example: Match judgment ~ To distinguish between case and case; ~ is case-insensitive. ~* is case-insensitive; !~ is not case-insensitive. For example, set ngi below. Nx is redirected 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 there are files -d and !-d to determine whether there is a directory -e and !-e to determine whether there is a file or directory -x and !-x to determine whether the file is executable. For example, the following setting nginx does not exist in files and directories Redirect: if (!-e $request_filename) { proxy_pass http://127.0.0.1; } return

Return the http code, for example set the nginx anti-theft chain: location ~* \\.(gif| Jpg| Png| Swf| Flv)$ { valid_referers none blocked www.45it.net www.test1.com; if ($invalid_referer) { return 404; } }

Copyright © Windows knowledge All Rights Reserved