Syntax rules for location-matching URLs:
Location [=|~|~*|^~]/uri/{...}
= indicates exact match
~ Indicates a case-sensitive regular match
^~ represents a URL that begins with a regular string;
~* indicates a regular match that does not differentiate between large messages
!~ (*!~) indicates a case-insensitive match
/wildcard, any request can match to this location, if any other location can match, it will overwrite the location
Match order:
1) match normal URL first, match regular
2) the "normal location" matching rule is "maximum prefix", so "normal location" does not have to do with location editing order;
3) The execution logic of the location is basically not related to the order, but in the way of regular matching, the first URL is matched, and the following URL is not continued;
In this case, if there is a match on the normal localtion, there is no regular match, then a normal match is used, and if the maximum prefix match for both normal location and regular match, then the regular match overrides the maximum prefix match.
4) After matching "normal location", sometimes need to continue to match "regular location", and sometimes do not need to continue to match "regular location".
In both cases, you do not need to continue to match the regular location: (1) when the normal location in front of the specified "^~", specifically told Nginx this article general location once the match, you do not need to continue regular match, (2) when the normal location exactly match, not is the maximum prefix match, the matching regular is no longer continued.
For example:
Location =/{ # matches the query/only. [Configuration A]} Location /{ # matches * query, since all queries begin with/, but regular # expressions and any longer con Ventional blocks would be # matched first. [Configuration B]} location/documents/{ # matches any query beginning With/documents/and continues searching, # so regular expre Ssions'll be checked. This would be matched only if # Regular expressions T find a match. [Configuration C]} Location ^~/images/{ # matches * Query beginning With/images/and halts searching, # so regular expressions Won't is checked. [Configuration D]} Location ~* \. (gif|jpg|jpeg) $ { # matches any request ending in gif, JPG, or JPEG. However, all # requests to The/images/directory would be a handled by # Configuration D. [Configuration E]}
Request example, matching location:
- /-Configuration A
- Configuration B/index.html
- /documents/document.html, Configuration C
- /images/1.gif, Configuration D
- /documents/1.jpg, Configuration E
Matching rules for location in Nginx configuration