nginx location configuration regular expression instance elaboration
- 2020-05-15 03:33:06
- OfStack
1. location is introduced
The & # 8226; location is configured in the server block to sort through different requests by matching the received uri, such as reverse proxy, fetch static files, etc
The & # 8226; location can be multiple in the server block, in sequence, and will be processed by the first matched location
The & # 8226; The matching function of localtion is only used for matching distribution, and it will not change the content or other functions of uri. When I first started to understand it, I confused some concepts of uri. I suggest more testing to see the actual effect
2.localtion matching rules
The & # 8226; location [= | ~ | ~* | ^~] uri {... }
The & # 8226; location @ name {... }
Note 1: rules cannot be mixed
Note 2 the following examples are based on this server
server {
listen 8861;
server_name abc.com;
}
2.1 "=" exact match
The & # 8226; The content must match the expression exactly 1
Ex. :
location = / {
.....
}
# Matches only http://abc.com
# http://abc.com [ The match is successful ]
# http://abc.com/index [ Matching failure ]
2.2 "~", case sensitive
Ex ・ :
location ~ /Example/ {
.....
}
#http://abc.com/Example/ [ The match is successful ]
#http://abc.com/example/ [ Matching failure ]
2.3. "~*", case ignored
Ex. :
location ~* /Example/ {
.....
}
# Will be ignored uri Partial case
#http://abc.com/test/Example/ [ The match is successful ]
#http://abc.com/example/ [ The match is successful ]
2.4. "^~", matches only those starting with uri
Ex. :
location ^~ /index/ {
.....
}
# In order to /img/ The first request will be matched
#http://abc.com/index/index.page [ The match is successful ]
#http://abc.com/error/error.page [ Matching failure ]
2.5. "@", nginx internal jump
Ex. :
location /index/ {
error_page 404 @index_error;
}
location @index_error {
.....
}
# In order to /index/ The initial request, if the status of the link is 404 . It's going to match @index_error On this rule.
2.6 no rules
The & # 8226; When no rules are added, the default is case-sensitive and the prefix matches, which is equivalent to adding "~" and "^~".
The & # 8226; Only/means match all uri
location /index/ {
......
}
#http://abc.com/index [ The match is successful ]
#http://abc.com/index/index.page [ The match is successful ]
#http://abc.com/test/index [ Matching failure ]
#http://abc.com/Index [ Matching failure ]
# Match to all uri
location / {
......
}
conclusion