Nginx Rewrite study notes

Source: Internet
Author: User

Relevant directives have if, rewrite, set, return, break, etc., where rewrite is the most critical instruction.

1.if instruction

Syntax: if (confition) {...}

Default value: None

Usage Environment: server,location

The directive is used to check whether a condition is compliant, and if the condition conforms, the statement inside the curly braces is executed. If directives do not support nesting, multiple conditions are not supported && and | | Processing.

The following information can be specified as a condition

(1) Variable name, error value includes: empty string "", or any string starting with 0

(2) Variable comparisons can use "=" (equals) and "! =" (means not equal to) operators

(3) Regular expression pattern matching can use "~*" and "~" symbols

(4) the "~" symbol indicates a match between uppercase and lowercase letters

(5) The "~*" symbol indicates a match between uppercase and lowercase letters (for example, Firefox and Firefox are matched)

(6) The "!~" and "!~*" symbols do exactly the opposite of "~", "~*", indicating a mismatch

(7) "-F" and "!-f" are used to determine whether a file exists

(8) "-D" and "!-d" are used to determine whether a directory exists

(9) "-E" and "!-e" are used to determine whether a file or directory exists

(Ten) "-X" and "!-x" are used to determine whether a file is executable

Some regular expressions can be within the parentheses "()", whose values can be accessed through the following variable from $ $9

2.rewrite instruction

Rewrite directive

Syntax: Rewrite regex replacement flag

Default value: None

Usage Environment: SERVER,LOCATION,IF

The directive redirects the URI based on an expression, or modifies the string. Directives are executed according to the order in the configuration file.

Note that the rewrite expression is valid only for relative paths. If you want to pair the hostname, you should use the IF statement with the following code:

if ($host ~* www\. *)){

Set $host _without_www $

rewrite ^ (. *) $/HTTP $host _without_ www$1 permanent, #$1 contains

'/foo ', not ' www.mydomain.com/foo '

}

If the replacement string starts with http://, a 301 or 302 jump will be used for URL redirection.

The last parameter of the rewrite directive is the flag flag, and the supported flag flags are:

Last-----equivalent to the [L] mark in Apache, indicating completion rewrite

Break----When the rule match is complete, the median matches, not the rule following the match

Once the last:rewrite is matched, a request will be initiated again, and only the rules in the location will be matched again.

After a match is break:rewrite, the request is not initiated and the subsequent rules are not matched.

redirect--returns 302 temporary redirect, the browser address bar displays the URL address after the jump.

permanent-return 301 Permanent Redirect, the browser address bar will show the URL address after the jump

In the above tag, last and break are used to implement the URI rewrite, the URL address of the browser address bar is unchanged, but on the server side

The path to the access has changed. Redirect and permanent are used to implement URL jumps, and the browser address bar displays the URL address after the jump.

The last and break tags are implemented similarly, but there is a subtle edge between them, which must be marked with the last tag when using the alias directive.

Use the break flag when using the PROXY_PASS directive. The last tag will be followed by the completion of this rule

Server{...} The label re-initiates the request, and the break flag terminates the match after the rule match is complete, and no longer matches the following rule. For example, the following rule must use the break flag, which causes a dead loop with the last tag.

location/cms/{

Proxy_pass http://test.youdomain.com;

Rewrite "^/cms/(. *) \.html$"/cms/index.html break;

}

3.set instruction

Set instruction

Syntax: Set Variable Value

Default value: None

Usage Environment: SERVER,LOCATION,IF

The directive is used to define a variable and assign a value to it, and the value of the variable can be a combination of a text variable and a variable text

Set $varname ' Hello ';

4.return instruction

Syntax: Return code

Default value: None

Usage Environment: SERVER,LOCATION,IF

This instruction is used to end the execution of the rule and return the status to the client. The status codes can be: 204,400,402-406,408,410,411,413,416 and 500~504. In addition, non-standard status code 444 will end the connection in a way that does not send any header headers.

example, if the URL visited ends with. SH and. Bash, the status is returned 403

Location ~. *\. (Sh|bash)? $

{

return 403;

}

5.break instruction

Syntax: Break

Default value: None

Usage Environment: SERVER,LOCATION,IF

The purpose of this directive is to complete the current set of rules and no longer process rewrite instructions.

Example:

if ($slow) {

Limit_rate 10k;

Break

}

Nginx Rewrite study notes

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.