Original: Http://outofmemory.cn/code-snippet/3037/nginx-configuration-file-de-location-explain-in-detail
Location
Syntax: Location [=|~|~*|^~]/uri/{...} default: No
Context: Server
This command accepts different structures depending on the URL. You can configure the use of regular strings and regular expressions. If you use a regular expression, you must use the ~* prefix to select a case-insensitive match or ~ To select a match that matches the casing.
Determine which location directive matches a specific instruction, the first test of a regular string. The general string matches the beginning of the request and is case-sensitive, and the most explicit match will be used (see below for an understanding of how nginx determines it). The regular expression is then tested in the order of the configuration file. A regular expression that finds the first histograms stops the search. If no matching regular expression is found, the result of the regular string is used.
There are two ways of modifying this behavior. The first method is to use the "=" prefix, which will only perform a strict match. If the query matches, then the search stops and the request is processed immediately. Example: If a "/" request occurs frequently, then using "location =/" will expedite processing of this request.
The second one is to use the ^~ prefix. If you use this prefix for a regular string then tell Nginx if the path matches then do not test the regular expression.
And it is important that NGINX does not have URL encoding, so if you have a URL link '/images/%20/test ', then use "images//test" to limit location.
Summary, instructions are accepted in the following order: 1. = prefix the instruction strictly matches this query. If found, stop the search. 2. The remainder of the regular string, long in front. If this match uses the ^~ prefix, the search stops. 3. Regular expressions, according to the order in the configuration file. 4. If the third step produces a match, the result is used. Otherwise, the matching result of the second step is used.
Example:
Location= / {# Match/Query only.[Configuration A]}Location/ {# matches any query, because all requests have/begin. However, regular expression rules and long block rules will be matched by precedence and query.[Configuration B]}Location^~ /images/ { # matches any query that has/images/start and stops the search. Any regular expression will not be tested. [ configuration C ]< Span class= "pun" >}location ~* \. (gif| Jpg| Jpeg) $ {# matches any gif, JPG or the request at the end of the JPEG. However, all requests for the/images/directory will use Configuration C. [ configuration D ]< Span class= "pun" >
Example Request:
/ -Configuration A/documents/document. Configuration B/images/1.gif, configuration C/Documents /1.jpg, configuration D
Note: Defining these 4 configuration results in any order will still be the same.
(location =) > (location full path) > (location ^~ path) > (location ~*, ~ Regular) > (Location section start path) The regular expression affects the previous order in the configuration file Match the previous priority. The others are matched according to the matching length.
Reference: Http://wiki.nginx.org/NginxHttpCoreModule#location
Location details in the Nginx configuration file