Nginx 301 302 Redirect Jump configuration

Source: Internet
Author: User
Tags regular expression

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

301 Jump Settings:

server {

Listen 80;

server_name downcc.com;

Rewrite ^/(. *) http://www.downcc.com/$1 permanent;

Access_log off;

}

302 Jump Settings:

server {

Listen 80;

server_name downcc.com;

Rewrite ^/(. *) http://www.downcc.com/$1 redirect;

Access_log off;

}

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

server {

server_name downcc.com;

Rewrite ^/(. *) http://www.downcc.com/$1 permanent;

}

last– basically use this flag.

break– abort Rewirte, do not continue to match

redirect– returns the temporarily redirected HTTP status 302

permanent– returns HTTP status for permanent redirection 301

Nginx redirects to the Nginx Httprewritemodule, the following is a simple way to explain how to use the following:

Rewrite command

Nginx's rewrite is the equivalent of Apache's rewriterule (in most cases, the original Apache rewrite rules can be used directly with quotes), it can be used in the server,location and if condition judgment block, The command format is as follows:

Rewrite a regular expression to replace the target flag tag

Flag tags can be in the following formats:

last– basically use this flag.

break– abort Rewirte, do not continue to match

redirect– returns the temporarily redirected HTTP status 302

permanent– returns HTTP status for permanent redirection 301

Special attention:

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

Redirect and permanent are 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 a file under a directory to another directory, and the corresponding string in the second bracket (. *) is $:

location/download/{

Rewrite ^ (/download/.*)/m/(. *). *$ $1/nginx-rewrite/$2.gz break;

}

Judgment of If condition for nginx redirection

In the case of server and location two, you can use the Nginx if condition to determine the following conditions:

Regular expressions

Such as:

Match judgment

~ to match case sensitivity;!~ does not match case sensitivity

~* for case-insensitive matching;! ~ does not match case insensitive

For example, the following settings Nginx are redirected to the/nginx-ie directory using the user's use of IE:

if ($http _user_agent ~ msie) {

Rewrite ^ (. *) $/nginx-ie/$1 break;

}

File and directory Judgments

-F and!-f to determine whether a file exists

-D and!-d to determine whether a directory exists

-E and!-e to determine whether a file or directory exists

-X and!-x determine if the file is executable

For example, the following setting Nginx redirects when files and directories do 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.downcc.com www.test1.com;

if ($invalid _referer) {

return 404;

}

}

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.