Nginx Location Rule Priority

Source: Internet
Author: User
Tags nginx server

The Nginx server searches multiple location blocks in the server block for a standard URI and a request string match, and if there are multiple matches, record the one with the highest matching degree. The server then matches the regular URI in the location block with the request string, and when the first regular URI matches successfully, ends the search and uses the location block to process the request, or if the regular match fails all, This request is processed using the most matching location block just recorded.

Location-Priority

Nginx configuration is as follows

server {        listen        80;        server_name  www.nginxtest.me s.nginxtest.me  a.nginxtest.me;        root /data/wwwroot/web/nginxtest.me/ ;         #access_log  /data/wwwroot/log/ www.nginxtest.me-access.log main_zdy;        access_log on;         error_log /data/wwwroot/log/www.nginxtest.me-error.log;         location / {                          return 500;        }         location /testa/bb {                return 501;         }        location / testb {            return 404;         }        location / testb/bb {            return 405;         }        location ~  ^/testa {                 return 503;        }         location = /testa {                 return 502;        }                 location ~ ^/testa/bb {                 return 504;         }                 location ^~ /testa/bb/cc {                 return 403;         }        location ^~ /testa/bb/cc/gg {                 return 402;         }}

1) "=" has the highest priority, and if the match succeeds, it stops matching the other. So the input Http://www.nginxtest.me/testa returns 502 but http://www.nginxtest.me/testa/and so on are not matched.
2) If there is no = match or = match is not matched, the regular match is used. The regular match is from top to bottom once the match is no longer executed (so the above-written regular should be more accurate and better) Http://www.nginxtest.me/testa/bb returns 503 instead of 504
3) If the regular match does not match, it is found by an empty match. The order of the empty matches is different from the regular match, not the match from top to bottom, but according to the accuracy, so the input http://www.nginxtest.me/testb/bb returns 405.
4) Special ^~ if the ^~ is connected to a standard URL (which must be a standard URI), then a regular match will not be made (its priority is higher than the regular match) if the match is on HTTP://WWW.NGINXTEST.ME/TESTA/BB/CC/HTTP/ WWW.NGINXTEST.ME/TESTA/BB/CC/DD returns 403.
This match is also matched by accuracy, as we enter HTTP://WWW.NGINXTEST.ME/TESTA/BB/CC/GG to return 402

In summary: If we want to add a more accurate location under a server with a lot of location, there are three ways
1 if the URI of this location is OK then you can use the standard URI to match with the = number
2 If the URI of this location is not deterministic in the latter part, it can be matched using ^~. such as: Location ^~/testa/bb/cc such as input http://www.nginxtest.me/testa/bb/cc/dd or http://www.nginxtest.me/testa/bb/cc? SDFDJF can be matched.
3 If the URI is not in the front and in the middle, you need to write a regular URI. However, the regular URI must precede the other location-regular URI that can be matched, otherwise it is not valid.

Nginx Location Rule Priority

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.