Rewrite jump scene
- URL looks more standardized and reasonable;
- Dynamic URL address of the company will be disguised as a static address to provide services;
- After the Web site domain name renewal, let the old access jump to the new domain name;
- Server and certain operational adjustments.
Rewrite jump achieve
Rewrite practical scene
Nginx jumps implementation requirements
- Use rewrite match Jump
- After the match if using global variables Jump
- Use location and then jump match
rewrite on the server {}, if {}, paragraph location {}
The domain name or parameter string
- If the use of global variables match
- Use reverse proxy proxy_pass
Common regular expression metacharacters
character | Explanation |
---|---|
^ | Matches the input character string starting position |
$ | Matches the input end of the string |
* | Matches the preceding character zero or more times |
+ | In front of the match character one or more times |
? | Matches the preceding character zero or one time |
. | Matches any single character except \ n, the usage patterns such as "[. \ N]" and the like, may be included to match any character "\ n" is |
\d | Pure digital matching [0-9] |
{n} | Repeated several times |
{n,} | Repeated n times or more |
[c] | Matches a single character c |
[a-z] | Matches any lowercase letters az a |
[A-zA-Z] | ] Az matches any lowercase or uppercase letters AZ a |
Rewrite command
grammar:
rewrite <regex> <replacement> [flag];
正则 跳转后的内容 rewrite支持的flag标记
flag Signs
mark | Explanation |
---|---|
last | Apache equivalent of [L] flag, indicating the completion rewrite |
break | Complete match this rule is terminated and no longer match any subsequent rules |
redirect | Returned 302 temporary redirect, the browser displays the URL address after the jump, reptile does not update the url |
permanent | Returned 301 permanent redirect, the browser address bar displays the URL address after the jump, reptile update url |
last break and compare
last | break | |
---|---|---|
scenes to be used | Generally written in the server and if the | In general use in location |
URL matches | Does not terminate after the match url rewriting | Termination url rewriting after match |
location Category
location = patt {} [精准匹配]
location patt {} [一般匹配]
location ~ patt {} [正则匹配]
Common regular expression matching
mark | Explanation |
---|---|
~ | Performing a regular matching case-sensitive |
~* | Performing a regular matching insensitive |
!~ | Performing a regular matching case sensitive mismatch |
!~* | Performing a regular matching insensitive mismatch |
^~ | Ordinary character matches; prefix match. If the match is successful, no longer match other location |
= | Precise matching ordinary characters. That is exact match |
@ | Define a named location, orientation when used internally |
location priority
The same type of expression, the characters will give priority to creating a long match
prioritized
- Type =
- ^ ~ Type expression
- Regular Expressions (~ and ~ *) type
- Conventional string matching type press prefix matching
- Generally matching (/), if no other matches, any requests are matched to the
Compare rewrite and location
The same point
can be realized Jump
different points
- rewrite is to change the access to resources within the same domain name path
- location is to do a kind of path or direction to control access to the proxy, you can also proxy_pass to other machines
rewrite will be written in the location where the execution order - Performing server rewrite instruction block inside
- Execution location match
- Perform the selected location in the rewrite instructions
Priority exemplary location
##精确匹配/,主机名后面不能带任何字符串
location = / {
[ configuration A ]
}
##所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配
location / {
[ configuration B ]
}
##匹配任何以/documents/开头的地址,当后面正则表达式没有匹配到时,才起作用
location /documents/ {
[ configuration C ]
}
##匹配任何以/documents/abc开头的地址,当后面正则表达式没有匹配到时,才会起作用
location ~ /documents/abc {
[ configuration D ]
}
##以/images/开头的地址,匹配符合后,停止往下匹配
location ^~ /images/ {
[ configuration E ]
}
##匹配所有以gif结尾的请求,/images/下的图片会被[configuration E]处理,因为^~的优先级更高
location ~* \.(gif|jpg|jpeg)$ {
[ configuration F ]
}
##最长字符匹配到/images/abc,优先级最低
location /images/abc {
[ configuration G ]
}
##以/images/abc开头的,优先级次之
location ~ /images/abc {
[ configuration H ]
}
##如果和正则~/images/abc/1.html相比,正则优先级更高
location /images/abc/1.html {
[ configuration I ]
}
location precedence rules
Match a specific file
(location =完整路径) > (location ^~完整路径) > (location ~*完整
路径) > (location ~完整路径) > (location 完整路径) > (location /)
Access a file directory do match
(location=目录) > (location ^~目录/) > (location~目录)>
(location ~*目录) > (location 目录) > (location /)