The syntax format for location in Nginx is:Location [=|~|~*|^~|@]/uri/{...}
Description:= indicates full consistency ~ Represents a case-sensitive regular match, ~* indicates a case-insensitive regular match ^~ means no further matching regular @ denotes internal redirection, does not need to interact with the browser, is a forwarding on the server side
several concepts in location:Ordinary location:location using literal strings regular location:location using regular expressions, with a ~ or ~* to do the exact location of the prefix: match to the file name, such as /first/second/file.html Maximum prefix match: This concept is used when the common location is matched, e.g., when the URL is/first/second/index.html, there are/first/and/first/ second/two location exists,/first/second/is the maximum prefix match for this URL.
The matching principle in location:Match the normal location first, then match the regular location, and the regular location is matched to the normal location. The matching process is as follows: a) the match of the normal location is unrelated to the order recorded in the configuration file B) The match of the regular location is related to the order of the records in the configuration file, and the match to the next will no longer match. c) the match in normal location is matched by the principle of maximum prefix matching d) The maximum prefix of the normal location matches to the following, then the regular match e) If the regular location is matched, the maximum prefix match for the normal location will be overwritten, and the exact location will be covered by the regular location F) If none match, then the location/
There are also special cases where the normal location can no longer be matched to the regular match: a normal location with a = or ^~ prefix is matched to no longer regular match
Example
nginx page File storage directory/usr/share/nginx/html file structure is as follows:. ├──50x.html├──else│└──index.html├──first│├──index.html│├──second││├──exactmatch.html││├ ──index.html││├──test.htm││├──test.html││└──third││├──exactmatch.htm││├── Exactmatch.html││├──index.html││├──test1.html││├──test2.html││├──test.htm ││└──test.html│├──test.htm│└──test.html├──index.html├──test.htm└──test.html
Note: The file content is the path of the file, such as the contents of the index.html in the first directory:/first/index.html
The Nginx configuration reads as follows:
server {Listen 80;
server_name 192.168.0.125;
server_name localhost;
root HTML;
Location/{index index.html;
Deny all;
} location/first/{index index.html;
} location/first/second/{index test.htm;
} location/first/second/exactmatch.html {index index.html;
} location ^~/first/second/third/{index index.html;
} location/first/second/third/exactmatch.html {index index.html;
} location/first/second/third/exactmatch.htm {index index.html;
} location =/first/second/third/test1.html {index index.html;
} location/first/second/third/test2.html {index index.html;
} location ~ \.html$ {rewrite. */else/index.html break;
}
Error_page 404 = @notfound;
Location @notfound {Proxy_pass http://www.taobao.com;
} error_page 502 503 504/50x.html;
Location =/50x.html {root html; }
}
Test Exercise (The server_name for this nginx is 192.168.0.125)
Access URL |
Access Results |
Description |
http://192.168.0.125/first/ |
/else/index.html |
The default index.html is the. html end, matched by a regular match |
Http://192.168.0.125/first/test.htm |
/first/test.htm |
Common location Matching |
Http://192.168.0.125/first/test.html |
/else/index.html |
Regular location Matching |
http://192.168.0.125/first/second/ |
/first/second/test.htm |
index specifies test.htm, not ending with. html, so does not match the regular |
Http://192.168.0.125/first/second/exactmatch.html |
/else/index.html |
Exact match, but is covered by the regular |
http://192.168.0.125/first/second/third/ |
/first/second/third/index.html |
^~ prefix reason, normal location matches, no longer matches the regular |
Http://192.168.0.125/first/second/third/exactmatch.html |
/else/index.html |
Exact match, but is covered by the regular |
Http://192.168.0.125/first/second/third/exactmatch.htm |
/first/second/third/exactmatch.htm |
Exact match |
Http://192.168.0.125/first/second/third/test1.html |
/first/second/third/test1.html |
= The reason for the prefix, not matching the regular |
Http://192.168.0.125/first/second/third/test2.html |
/else/index.html |
There is no = prefix, and the exact match is overwritten by the regular |
Http://192.168.0.125/first/second/third/notfound.html |
404 Go to Taobao page |
Notfound.html page does not exist, although the end of the. HTML, but the maximum prefix matches the/first/second/third/, it is preceded by a ^~ prefix, so does not match the regular, will be reported 404; however, 404 is NotFound directed to Taobao page |
Http://192.168.0.125/first/second/third/notfound.htm |
404 Go to Taobao page |
Ditto |
Http://192.168.0.125/first/second/notfound.html |
/else/index.html |
The page does not exist, but ends with a. html and is matched by a regular location |
Http://192.168.0.125/first/second/notfound.htm |
404 Go to Taobao page |
404 and does not match the regular, was @notfound directed to the Taobao page |
Http://192.168.0.125/first/notfound.html |
/else/index.html |
The page does not exist, but ends with a. html and is matched by a regular location |
Http://192.168.0.125/first/notfound.htm |
404 Go to Taobao page |
404 and does not match the regular, was @notfound directed to the Taobao page |
Http://192.168.0.125/notfound.html |
/else/index.html |
The page does not exist, but ends with a. html and is matched by a regular location |
Http://192.168.0.125/notfound.htm |
403 Forbidden |
The page does not exist, and does not end in. html, as per the principle of common location, and/match; however, the deny all defined in the location/inside will be accessed by forbidden, so it is reported 403 |
http://192.168.0.125/ |
403 Forbidden |
With location/match, reported 403 |