In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Location matching order
1. The "=" prefix instruction matches. If the match is successful, the other matches are stopped.
two。 Ordinary string instruction matching, the order is from long to short, successful location if you use ^ ~, then stop other matches (regular matching)
3. Regular expression instructions match, according to the order in the configuration file, and stop other matches if successful.
4. If there is a match in the third step, use this result, otherwise use the second step result
Pay attention
1. The order of matching is to match the normal string first and then the regular expression. In addition, the matching order of ordinary strings is based on the length of characters in the configuration from long to short, that is, the order of location configured with ordinary strings is irrelevant. Anyway, nginx will match according to the length of the configuration, but it is important to note that regular expressions are tested according to the order in the configuration file. Finding the first matching regular expression stops the search.
two。 In general, regular expression location matching is performed after a successful match of the normal string location. There are two ways to change this behavior, one is to use the "=" prefix, which performs a strict match, and immediately stops other matches after a successful match, while processing the request; the other is to use the "^ ~" prefix, which tells nginx not to test the regular expression if the path matches.
Matching pattern and order
The beginning of location = / uri = indicates an exact match, which can only take effect on an exact match.
Location ^ ~ / uri ^ ~ begins with a prefix match on the URL path and comes before the regular.
The beginning of location ~ pattern ~ indicates a case-sensitive regular match.
The beginning of location ~ * pattern ~ * indicates a case-insensitive regular match.
Location / uri does not have any modifiers, which also means that the prefix matches, but after the regular match.
Location / generic match, any request that does not match another location will be matched, which is equivalent to default in switch.
Experimental case
Test "^ ~" and "~", and the nginx configuration is as follows. Enter http://localhost/helloworld/test in the browser and return 601. If you open # 1 and # 2, enter http://localhost/helloworld/test in the browser and return to 603. Note: # 1 and # 2 cannot be opened at the same time. If you open it at the same time, starting nginx will report nginx: [emerg] duplicate location "/ helloworld". Because both are ordinary strings.
Location ^ ~ / helloworld {# 1 return 601;} # location / helloworld {# location ~ / helloworld {return 603;}
Test the length of a normal string (the matching of an ordinary string is not related to the order, but to the length). Enter http://localhost/helloworld/test/a.html in the browser and return 601. Enter http://localhost/helloworld/a.html in the browser and return 602.
Location / helloworld/test/ {# 1 return 601;} location / helloworld/ {# 2 return 602;}
Test the order of regular expressions (regular matching is related to order). Enter http://localhost/helloworld/test/a.html in the browser and return 602; change the order of # 2 and # 3, enter http://localhost/helloworld/test/a.html in the browser and return 603
Location / helloworld/test/ {# 1 return 601;} location ~ / helloworld {# 2 return 602;} location ~ / helloworld/test {# 3 return 603;}
Therefore, in actual use, I think there are at least three matching rule definitions, as follows:
# directly match the root of the website, and visit the home page of the website more frequently through the domain name. Using this will speed up the processing, according to the official website. # this is forwarded directly to the backend application server, or it can be a static home page # the first required rule location = / {proxy_pass http://tomcat:8080/index} # the second required rule is to handle static file requests, which is the strength of nginx as a http server # there are two configuration modes Directory matching or suffix matching, choose one or use location ^ ~ / static/ {root/ webroot/static/ } location ~ *\. (gif | jpg | jpeg | png | css | ico) ${root/ webroot/res/;} # the third rule is the general rule, which is used to forward dynamic requests to the back-end application server. non-static file requests are dynamic requests by default, according to the actual grasp. # after all, in some current frameworks, there are few location / {proxy_pass http://tomcat:8080/} with .php, .jsp suffixes.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.