Nginx location Configuration (URL)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.