Nginx--rewrite Module

Source: Internet
Author: User
Tags what is nginx

1. What is Nginx's rewrite rule?
The main function of rewrite is to implement the rewrite of the URL, the nginx rewrite rule adopts pcre (Perl
Compatible Regular Expressions) Perl-compatible regular expression syntax for rule matching, if


You need nginx rewrite function, before compiling nginx, need to compile and install the Pcre library .
Through the rewrite rule, you can implement the URL of the specification, and make the URL steering and select configuration according to the variables.

(1) Some programs that use the MVC framework have only one entry, which can be achieved through rewrite.
(2) Some dynamic URL addresses need to be disguised as static HTML to facilitate search engine crawling, but also rewrite to handle
(3) Some old URLs due to the directory structure, the change of the domain name, need to jump to the new URL, can be rewrite to

Processing.

2.Nginx Rewrite rules related directives
If, rewrite, set, return, break
(1) Break, complete the current rule set, and no longer process the rewrite instruction.
(2) if

Syntax: if (condition) {...}
Used to check if a condition is compliant, and if the condition conforms, the statement inside the curly braces is executed. If directive does not support embedding

Sets, does not support multiple conditions && and | | Processing.
The following information can be specified as a condition:
1) The variable name, where the value of the error includes an empty string "", or any string starting with 0.
2) Variable comparison you can use the = and! = Operators
3) the "~" symbol indicates a match between uppercase and lowercase letters.
4) The "~*" symbol indicates a match between uppercase and lowercase letters. Firefox and Firefox are matched.
5) the "!~" and "!~*" symbols do exactly the same as "~", "!~" instead, which means they do not match.
6) "-F" and "!-f" are used to determine whether a file exists.
7) "-D" and "!-d" are used to determine whether a directory exists.
8) "-E" and "!-e" are used to determine whether a file or directory exists.
9) "-X" and "!-x" are used to determine whether a file is executable.
(3) Return syntax: Return code
This instruction is used to end the execution of the rule and return the status code to the client. The status code can use these

Values: 204,400,402-406,408,410,413,416, and 500-504. Non-standard status code, 444 will not be sent

End the connection in any way header.
403Forbidden. The server has understood the request, but refuses to execute it.
404Not Found. The request failed and the requested resource was not found on the server. 404 This Status code

is widely used when the server does not want to reveal why a request is rejected, or if no other appropriate response is available

Conditions.
500Internal server Error. The server encounters an unexpected condition that causes it to not complete the

Processing. In general, this problem occurs when the server's code is in error.
502Bad Gateway. When a server that is working as a gateway or proxy attempts to execute a request, it receives from the upstream server

to an invalid response.
503Service unavailable. The server is currently unable to process requests due to temporary server maintenance or overloading.

This situation is temporary and will be resumed after a period of time. 503 presence of a status code does not mean that the server

It must be used when it is overloaded. Some servers simply want to deny connections to clients.
504 Gateway Timeout failed to execute the request as a gateway or proxy server

Servers (such as HTTP,FTP,LDAP) or secondary servers (such as DNS) that receive a

Should.
(4) Rewrite directive. Syntax: Rewrite regex replacement flag. The directive is based on the expression to re-

Directs the URI, or modifies the string. Directives are executed according to the order in the configuration file. Note that the rewrite expression only

Relative paths are valid. If you want to pair a host name, you should use the IF statement.
The last parameter of the rewrite directive is the flag flag, and the supported flag flags are:
last--is equivalent to the [L] mark in Apache, indicating completion of rewrite.
break--This rule match is complete, the match is terminated and the subsequent rule is no longer matched.
redirect--returns 302 temporary redirect, the browser address bar displays the URL address after the jump.
permanent--returns a 301 permanent redirect, and the browser address bar displays 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 not changed, but in the service

The path to the server-side access has changed. Redirect and permanent used to implement URL jumps, browser address

The column displays the URL address after the jump.
1) Generally in root location (ie location/{...}) or write rewrite rules directly in the Server tab

It is recommended to use the last tag, in the non-root location (location/cms/{...}), then use the break flag.
Such as:
Rewrite ^ (/download/.*)/media/(. *) \. *$ $1/mp3/$2.mp3 last;
Rewrite ^ (/download/.*)/audio/(. *) \. *$ $1/mp3/$2.ra last;
return 403;

location/download/{
Rewrite ^ (/download/.*)/media/(. *) \. *$$1/mp3/$2.mp3 break;
Rewrite ^ (/download/.*)/audio/(. *) \. *$$1/mp3/$2.ra break;
return 403;
}
2) If the URI being replaced contains a parameter (that is, a URI like/app/test.php?id=5), the default

The next parameter is automatically appended to the replacement string, and you can resolve this by adding the tag to the end of the replacement string.

Question. The URL jump difference between mark and add tag:

Rewrite ^/test (. *) $ http://www.yourdomain.com/home permanent;
Visit http://www.yourdomain.com/test?id=5 after 301 Jump URL address is

Http://www.yourdomain.com/home?id=5

Rewrite ^/test (. *) $ http://www.yourdomain.com/home? Permanent
Visit http://www.yourdomain.com/test?id=5 after 301 Jump URL address is

Http://www.yourdomain.com/home

(5) Set instruction. Syntax: Set variable value. The directive is used to define a variable and assign a value to the variable.

The value of a variable can be a union of text, variables, and text variables. For example: Set $varname ' Hello '; u

(6) Uninitialized_variable_warn instruction. Grammar uninitialized_variable_warnon|

Off this directive is used to turn on or turn off warning messages about uninitialized variables, and the default value is on.

(7) The global variables that Nginx rewrite can use.
In the IF, location, rewrite directives, the following local variables can be used.
$args, $content _length, $content _type, $document _root, $document _uri,

$host, $http _user_agent, $http _cookie, $limit _rate,

$request _body_file, $request _method, $remote _addr, $remote _port,

$remote _user, $remote _filename, $request _uri, $query _string, $scheme

, $server _protocol, $server _addr, $server _name, $server _port, $uri


Nginx--rewrite Module

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.