Regular configuration of location in Nginx

Source: Internet
Author: User
Tags regular expression
background

The URI configured in the location in a project is variable and needs to be matched by a regular item. Also, by the way, the configuration problem in location is sorted out. Detailed:

Mode meaning
Location =/uri = indicates exact match, only valid on exact match
Location ^~/uri The ^~ begins by prefixing the URL path and before the regular
Location ~ pattern The beginning indicates a case-sensitive regular match
Location ~* pattern The beginning indicates a case-insensitive regular match
Location/uri Does not take any modifiers, it also indicates a prefix match, but after a regular match
Location/ Generic match, any request that does not match to another location will match, equivalent to the default in switch

In the case of multiple location configurations, the matching order is (reference comes, not actually verified, try to know, do not have to be specific, for reference only): first exact match = second prefix match ^~ followed by regular matching in the order of the file The match is then matched with a prefix without any adornments. Finally, give/general match when a match succeeds, stop the match and process the request according to the current matching rule

Note: The prefix matches, and if there is a inclusion relationship, matches by the maximum matching principle. For example, in the prefix match: Location/test1 and Location/test1/test2, if there is a request Http://localhost/test1/test2/file will eventually match to Localhost/test1/test2.
It is true that it has been tested. Note:

If there is {} in the regular expression, which conflicts with the {} of the location module, you need to enclose the regular expression in single or double quotation marks.

Test Case:

Location =/test1 {
        echo "test1";
    }

    Location ^~/test2 {
        echo "test2";
    }

    Location ^~/test2/test3/test4 {
        echo "TEST2/3/4";
    }

    Location =/test2018 {
        echo "test2018";
    }

    Location ~*/test2018 {
        echo "TEST2018";
    }

    Location  /test20182019 {
        echo "/test20182019";
    }

   #location ~ "/([\s]{8})" {
   #echo "Regu model";
   #}

    location  /test2019 {
        echo "/test20192019";
    }

    Location  /test2019/test2020 {
        echo "/test2019test20202020";
    }

can try it yourself.

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.