URL rewriting refers to the direction/jump to a rule by configuring the Conf file so that the URL of the site reaches a certain state, such as common pseudo static, 301 redirects, browser orientation, etc.
Rewrite
Grammar
Written in the server block of the configuration file, such as:
server {
Rewrite rule oriented path rewrite type;
}
Rule: can be a string or a regular to indicate the destination URL that you want to match
Directed path: Represents the path to be directed after matching to the rule, and if there is a regular in the rule, you can use $index to represent the captured groupings in the regular
Override Type:
Last: Equivalent to the Apache Reed (L) tag, indicating complete rewrite, browser address bar URL address unchanged
break; After this rule match completes, terminates the match, no longer matches the following rule, the browser address bar URL address does not change
Redirect: Return 302 temporary redirect, browser address will display the URL address after the jump
Permanent: Returns 301 permanent redirects, the browser address bar displays the URL address after the jump
Simple example
server {
# When accessing/last.html, page content is rewritten into/index.html
Rewrite/last.html/index.html last;
# When accessing/break.html, page content is rewritten into/index.html, and subsequent matches are stopped
Rewrite/break.html/index.html break;
# when visiting/redirect.html, the page is directed 302 to the/index.html
Rewrite/redirect.html/index.html redirect;
# when visiting/permanent.html, the page is directed 301 to the/index.html
rewrite/permanent.html/index.html permanent;
# put/html/*.html =>/post/*.html, 301 Directional
Rewrite ^/html/(. +?). html$/post/$1.html Permanent;
# put/search/key =>/search.html?keyword=key
Rewrite ^/search\/([^\/]+?) (\/|$)/search.html?keyword=$1 permanent;
}
The difference between last and break
Since 301 and 302 cannot simply return a status code, there must also be a redirected URL, which is why the return instruction cannot be returned to 301,302. The difference between last and break here is a little hard to understand:
Last is generally written in server and if, and break is generally used in location
Last does not terminate the rewrite URL match, that is, the new URL will go through the server again from the matching process, and break termination of the rewrite after the match
Both break and last can organize the following rewrite instructions to continue
In location, once the break is returned, it takes effect directly and stops subsequent matches location
server {
Location/{
rewrite/last//q.html last;
rewrite/break//q.html break;
}
Location =/q.html {
return 400;
}
}
When accessing/last/, rewrite to/q.html and then match with the new URI, matching exactly to Locatoin =/q.html and then returning 400
When accessing the/break, it is overridden to/q.html, and as a result of the return break, it stops directly
If judgment
Only the above simple rewrite many times can not meet the requirements, such as the need to determine when the file does not exist, when the path contains XX, and so on conditions, you need to use the IF
Grammar
if (expression) {
}
When an expression is just a variable, if the value is empty or any string that starts with 0 will be false
When comparing variables and content directly, use = or!=
~ Regular expression matching, ~* case-insensitive matching,!~ mismatched case mismatch
Some built-in conditions to judge:
-F and!-f are used to determine whether a file exists
-D and!-d are used to determine whether there is a directory
-E and!-e used to determine whether a file or directory exists
-X and!-x are used to determine whether a file can perform
built-in global variables
$args : This variable equals the parameter in the request line, the same $query_string
$content _length: The Content-length field in the request header.
$content _type: The Content-type field in the request header.
$document _root: The value specified in the root directive for the current request.
$host: Request the Host header field, otherwise the server name.
$http _user_agent: Client Agent information
$http _cookie: Client cookie information
$limit _rate: This variable can limit the connection rate.
$request _method: A client-requested action, usually a get or post.
$remote _addr: The IP address of the client.
$remote _port: The port of the client.
$remote _user: User name that has been validated by Auth Basic module.
$request _filename: The file path of the current request, which is generated by the root or alias directive and the URI request.
$scheme: HTTP methods (such as Http,https).
$server _protocol: The protocol that is requested to use, usually http/1.0 or http/1.1.
$server _ADDR: The server address that can be determined after a system call is completed.
$server _name: server name.
$server _port: The port number on which the request reaches the server.
$request _uri: The original URI containing the request parameter, which does not contain the host name, such as: "/foo/bar.php?arg=baz".
$uri: The current URI without the request parameter, $uri does not contain a host name, such as "/foo/bar.html".
$document _uri: Same as $uri.
such as:
Access link is: http://localhost:88/test1/test2/test.php
Site path is:/var/www/html
$host: localhost
$server _port:88
$request _uri:http://localhost:88/test1/test2/test.php
$document _uri:/test1/test2/test.php
$document _root:/var/www/html
$request _filename:/var/www/html/test1/test2/test.php
Example
# returns 400 if the file does not exist
if (!-f $request _filename) {
return 400;
}
# If host is not xuexb.com, 301 to Xuexb.com
if ($host!= "xuexb.com") {
Rewrite ^/(. *) $ https://xuexb.com/$1 permanent;
}
# returns 405 if the request type is not post
if ($request _method = POST) {
return 405;
}
# If the parameter has a=1 301 to the specified domain name
if ($args ~ a=1) {
rewrite ^ http://example.com/permanent;
}
In some scenarios, it can be combined with location rules, such as:
# when visiting/test.html
Location =/test.html {
# Default value is Xiaowu
Set $name Xiaowu;
# Use this value if there is name=xx in the argument
if ($args ~* name= (\w+?) (&|$)) {
Set $name $;
}
# 301
Rewrite ^/$name. HTML permanent;
}
The above indicates:
/test.html =>/xiaowu.html
/test.html?name=ok =>/ok.html?name=ok
Location
Grammar
Used in a server block, such as:
server {
Location Expression {
}
}
Type of location expression
If you write a path directly, matching the
~ in the path performs a regular match, case-sensitive
~* indicates that a regular match is performed, case-insensitive
^~ represents a common character match. Use prefix matching. If the match succeeds, the other location are no longer matched.
= Exact match for normal characters. Which is exactly the same. The
Priority
equals type (=) has the highest precedence. Once the match succeeds, no more matches are found. An
^~ type expression. Once the match succeeds, no more matches are found. The precedence of the
Regular expression type (~ ~*). If there are more than one location can match, the one with the longest regular expression is used. The
General string match type. Match by prefix.