Syntax rules: location [=|~|~*|^~]/uri/{...}
= indicates an exact match, which is the highest priority
^~ means that the URI begins with a regular string and is understood to match the URL path. Nginx does not encode the URL, so the request is/STATIC/20%/AA, which can be matched to the rule ^~/static//aa (note is a space).
~ Indicates a case-sensitive regular match
~* indicates a case-insensitive regular match (the only difference above is case)
!~ and!~* are case-insensitive and match-insensitive.
/generic match, any request will match to the default match.
Here are some rules and priorities for these grammars
Multiple location configuration in the case of the matching order (reference, not actually verified, try to know, do not have to rigidly, for reference only):
Priority =>^~>
First match =, next match ^~, followed by the regular match in the order of the file, and finally to the/General match. When a match succeeds, the match is stopped and the request is processed according to the current matching rule.
example, there are the following matching rules:
Location/ { Echo "/";//You need to install the Echo module, this way you can change to their own rules.}location= / { Echo "=/";} Location= /Nginx {Echo "=/nginx";} Location~ \. (gif|jpg|png|js|CSS) $ {Echo "Small-gif/jpg/png";} Location~*\.png$ {Echo "All-png";} Location^~/static/ { Echo "Static";}
Here are a variety of access situations
Access to http://a.losbyda.com/. Because/is exactly match
As follows:
[[email protected] conf] Curl http://a.losbyday.com/# =/
Access Http://a.losbyday.com/nginx because the "=/nginx" is exactly matched
[[email protected] conf] Curl http://A.losbyday.com/nginx#=/nginx
Access the Http://a.losbyday.com/nginx, try the match from the first, and finally match to the ~* \.png$.
[[email protected] conf] Curl http://a.losbyday.com/xxx/1111.png (Note that this is uppercase)
Access to Http://a.losbyday.com/static/1111.png, although static is placed on the last side, but because of the ^ 's sake, he is the most matching.
[[email protected] conf] Curl http://a.losbyday.com/static/1111.pngStatic
Finally, we give a matching rule for the static file of our first environment.
Location ~*. *\. (JS|CSS)? $
{
Expires 7d; Expires in 7 days
Access_log off; Do not save the log
}
Location ~*. *\. (Png|jpg|gif|jpeg|bmp|ico)? $
{
Expires 7d;
Access_log off;
}
Location ~*. *\. (ZIP|RAR|EXE|MSI|ISO|GHO|MP3|RMVB|MP4|WMA|WMV|RM)? $
{
Deny all; Prohibit these files to download, can be configured according to their own environment
}
Nginx location Configuration (URL)