Recently, I used nginx to configure on the server. When doing path matching, I encountered details. I will record it here. For installation, please go to the nginx tutorial to install it under windows.
1. Location matching rules
1. Prefix matching: without symbols
server {
listen 80;
server_name 192.168.100.123;
location /abc {}
}
#可以匹配到
http://192.168.100.123/abc
http://192.168.100.123/abc?name=zs
http://192.168.100.123/abc/
http://192.168.100.123/abcd
# 下列写法,当输入http://192.168.100.123时匹配到
location / {}
2. Exact match: Symbol =
: Indicates exact match
server {
listen 80;
server_name 192.168.100.123;
location = /abc {}
}
#可以匹配到
http://192.168.100.123/abc
http://192.168.100.123/abc?name=zs
#不能匹配到
http://192.168.100.123/abc/
http://192.168.100.123/abcd
3. Regular matching: Symbol ~
and ~*
: Execute a regular matching, the former is case-sensitive, the latter is not
server {
listen 80;
server_name 192.168.100.123;
location = ~* \.(jpg|png|gif)$ {}
}
4. Symbol ^~
: Once matched, do not continue to match
server {
listen 80;
server_name 192.168.100.123;
# 匹配静态文件
location ^~ /static/ {}
}
5. Match priority
1. Exact match
=
: if it matches, the match ends, otherwise it continues to match;
2. Prefix match (three cases):
(1) If it matches, record all successful items, if there is the longest item^~
, stop matching;
(2 ) If it is matched, record all successful items, the longest think if not^~
, perform regular matching;
(3) If not matched, perform regular matching
3. Regular matching :~
match~*
from top to bottom, with the last matching item as the match Result
4. No matches, return 404
Haven't understood yet? Don't worry, general, please see this picture
Two, proxy_pass rules
Access address: http://192.168.1.123/test/xxoo.html as an example, server_name
it is 192.168.1.123, if /
there are , special attention should be paid when using it
Case 1: location with / and proxy_pass with /
location /test/ {
proxy_pass http://192.168.1.123/
}
proxy addresshttp://192.168.1.123/xxoo.html
Case 2: location with / and proxy_pass without /
location /test/ {
proxy_pass http://192.168.1.123;
}
proxy addresshttp://192.168.1.123/test/xxoo.html
Case 3: location with / and proxy_pass with secondary directory and /
location /test/ {
proxy_pass http://192.168.1.123/api/;
}
proxy addresshttp://192.168.1.123/api/xxoo.html
Case 4: location with / and proxy_pass with secondary directory without /
location /test/ {
proxy_pass http://192.168.1.123/api;
}
proxy addresshttp://192.168.1.123/apixxoo.html
Case 5: location without / and proxy_pass with secondary directory without /
location /test {
proxy_pass http://192.168.1.123/api;
}
proxy addresshttp://192.168.1.123/api/xxoo.html
Case 6: location without / and proxy_pass with /
location /test {
proxy_pass http://192.168.1.123/;
}
proxy addresshttp://192.168.1.123//xxoo.html
Case 7: location without / and proxy_pass without /
location /test {
proxy_pass http://192.168.1.123;
}
proxy addresshttp://192.168.1.123/test/xxoo.html
Three, alias and root
Using alias, when accessing /test/, you will find files in the /www/abc/ directory
location /test/ {
alias /www/abc/;
}
When using root, when accessing /test/, it will find files in the /www/abc/test/ directory (if there is no test directory, it will report 403)
location /test/ {
root /www/abc;
}