In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Location instruction URI matching rules in Nginx. The article is very detailed and has a certain reference value. Interested friends must read it!
1. Introduction
The location instruction is the core configuration of the http module, which receives the request sent by the user according to the predefined URL matching rules, forwards the request to the background server according to the matching result, rejects the illegal request directly and returns 403,404,500 error handling.
2. Location instruction syntax
Location [= | ~ | ~ * | ^ ~ | @] / uri/ {… } or location @ name {… }
3. URI matching pattern
There are two matching modes for location instructions:
1 > normal string matching: rules that begin with = or without a pilot character (~)
2 > regular matching: start with ~ or ~ * to indicate regular matching, and ~ * means regular case-insensitive
4. Location URI matching rules
When nginx receives a request, it intercepts the URI portion of the request and searches for all the URI matching patterns defined in the location directive. Multiple location instructions can be defined in the server module to match different url requests and multiple URI matching patterns with different location configurations. The overall matching principle is: first match the ordinary string pattern, and then match the regular pattern. Only the URI part is identified. For example, the request is: / test/abc/user.do?name=xxxx
After a request arrives, the process for Nginx to match the request is as follows:
1 > first find out whether there is an exact match that begins with =, such as location = / test/abc/user.do {… }
2 > find a normal match based on the maximum prefix. If there are two location below, the latter will be matched.
* location / test/ {… }
* location / test/abc {… }
3 > after matching to a normal format, the search does not end, but temporarily stores the results of the current match and continues to search for regular matching patterns.
4 > after the first match item is found in all regular matching patterns location, this item is used as the final matching result.
So regular matching rules are affected by the order before and after the definition, but ordinary matching patterns do not.
5 > if no regular match is found, the cached result in 3 is used as the final match result.
6 > if no match is found, 404 is returned.
5. The difference between exact matching and fuzzy matching.
Location = / {… } and location / {… } the difference:
* the previous one is an exact match and only responds to / requests. All requests of / xxx or / xxx/xxxx classes will not match it in the form of a prefix.
* the latter is that all requests prefixed with / will be matched. For example, / abc, / test/abc, / test/abc/aaaa
6. Regular and irregular matching
1 > location ~ / test/.+.jsp$ {… }: regular matching, supporting standard regular expression syntax.
2 > location ^ ~ / {… }: ^ ~ means to turn off regular matching, and when this normal matching pattern is found, the search for regular matching patterns will no longer be continued.
... http {... Server {listen 80; server_name localhost; location / {root html; index index.html index.htm; # deny all; rejects the request and returns 403 # allow all; allow request} location / abc {deny all;} location ~ /. +\ .jsp ${proxy_pass http://location:9090; } # match all jsp files under / test path location ~ / test/.+\ .jsp ${proxy_pass http://localhost:8080;} # define various error pages error_page 404 / 404.html error_page 500502 503 504 / 50x.hml; location = / 50x.html {root html;} # @ similar to variable definition # error_page 403 http://blog.csdn.net; # this definition is not allowed. We need to use @ to define temporary variables to implement error_page @ page403; location @ page403 {proxy_pass http://blog.csdn.net;}. The above is all the contents of the article "sample Analysis of URI matching rules of Location directives in Nginx". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.