background
The URI configured in the location in a project is variable and needs to be matched by a regular item. Also, by the way, the configuration problem in location is sorted out. Detailed:
Mode |
meaning |
Location =/uri |
= indicates exact match, only valid on exact match |
Location ^~/uri |
The ^~ begins by prefixing the URL path and before the regular |
Location ~ pattern |
The beginning indicates a case-sensitive regular match |
Location ~* pattern |
The beginning indicates a case-insensitive regular match |
Location/uri |
Does not take any modifiers, it also indicates a prefix match, but after a regular match |
Location/ |
Generic match, any request that does not match to another location will match, equivalent to the default in switch |
In the case of multiple location configurations, the matching order is (reference comes, not actually verified, try to know, do not have to be specific, for reference only): first exact match = second prefix match ^~ followed by regular matching in the order of the file The match is then matched with a prefix without any adornments. Finally, give/general match when a match succeeds, stop the match and process the request according to the current matching rule
Note: The prefix matches, and if there is a inclusion relationship, matches by the maximum matching principle. For example, in the prefix match: Location/test1 and Location/test1/test2, if there is a request Http://localhost/test1/test2/file will eventually match to Localhost/test1/test2.
It is true that it has been tested. Note:
If there is {} in the regular expression, which conflicts with the {} of the location module, you need to enclose the regular expression in single or double quotation marks.
Test Case:
Location =/test1 {
echo "test1";
}
Location ^~/test2 {
echo "test2";
}
Location ^~/test2/test3/test4 {
echo "TEST2/3/4";
}
Location =/test2018 {
echo "test2018";
}
Location ~*/test2018 {
echo "TEST2018";
}
Location /test20182019 {
echo "/test20182019";
}
#location ~ "/([\s]{8})" {
#echo "Regu model";
#}
location /test2019 {
echo "/test20192019";
}
Location /test2019/test2020 {
echo "/test2019test20202020";
}
can try it yourself.