Location
Syntax: Location [= | ~ | ~ * | ^ ~] /Uri /{... }
Default: No
Context: Server
This command accepts different structures with different URLs. You can configure regular strings and regular expressions. If you use a regular expression, you must use ~ * The prefix is case-insensitive or ~ Select case-sensitive matching.
Determine which location command matches a specific command. The regular string is the first test. The starting part of a regular string matching request is case sensitive. The most explicit match will be used (see how nginx determines it ). Then, the regular expression is tested according to the order in the configuration file.Finding the regular expression for the first match will stop searching. If no matching regular expression is found, the regular string result is used.
There are two methods to modify this behavior. The first method is to use the "=" prefix and only perform strict matching. If the query matches, the search will be stopped and the request will be processed immediately. Example: If a "/" request occurs frequently, use "location =/" to accelerate the processing of the request.
The second is to use ^ ~ Prefix. If this prefix is used for a regular string, it indicates that if the path matches, the regular expression is not tested.
In addition, nginx does not have URL encoding. Therefore, if you have a URL link '/images/% 20/test', use "images/test" to limit the location.
In summary, commands are accepted in the following order:
1. = the prefix command strictly matches this query. If yes, stop searching.
2. The remaining regular strings are long in front. If this match is used ^ ~ Prefix. The search is stopped.
3. Regular expressions, in the order in the configuration file.
4. If the third step produces a match, use this result. Otherwise, use the matching result in step 2.
Example:
Location = /{
# Match/query only.
[Configuration A]
}
Location /{
# Match any query because all requests start. However, regular expression rules and long block rules are preferentially matched with queries.
[Configuration B]
}
Location ^ ~ /Images /{
# Match any queries starting with/images/and stop searching. No regular expression will be tested.
[Configuration C]
}
Location ~ * \. (GIF | JPG | JPEG) $ {
# Match any request that has ended with GIF, JPG, or JPEG. However, configuration C is used for all/images/directory requests.
[Configuration D]
}
Example request:
/-> Configuration
/Documents/document.html-> Configuration B
/Images/1.gif-> Configuration C
/Documents/1.jpg-> Configuration d
Note: The results of defining the four configurations in any order will remain the same.