In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Because the team is doing front-end separation and the front end takes over the Nginx and node layers, there is a lot of dealing with Nginx in the day-to-day work. Among them, location is the place with the most use and changes. There was little knowledge of the matching rules of location before. In order to understand how location matches, I spent some time looking up some information to sum up this article. I hope I can help you all.
rule of grammar
Location [= | ~ | ~ * | ^ ~] uri {...} location @ name {...}
The syntax rules are simple: a location keyword, followed by an optional modifier, followed by a character to match, and an action to be performed in curly braces.
Modifier
= indicates an exact match. It will be hit only if the requested url path is exactly equal to the following string. ~ indicates that the rule is defined using regularity and is case-sensitive. ~ * indicates that the rule is defined using regularity and is not case-sensitive. ^ ~ means that if the character after the symbol is the best match, the rule is adopted and no further search is made.
Matching process
Url serialization of the request. For example, decode characters such as% xx, remove multiple contiguous / in url, and parse the.,.. in url. Wait. This step is to match the pre-work.
Location has two representations, one using prefix characters and the other using regular. If it is regular, it is preceded by a ~ or ~ * modifier.
The specific matching process is as follows:
First check the location defined with prefix characters, select the longest matching item, and record it.
If an exact match of location is found, that is, location with the = modifier, end the search and use its configuration.
Then look for the location that uses the regular definition in order, and if there is a match, stop looking and use the configuration defined by it.
If there is no matching regular location, the longest matching prefix character location recorded earlier is used.
Based on the above matching process, we can get the following two revelations:
The order in which location using regular definitions appears in the configuration file is important. Because after finding the first matching regular, the search stops, and the regular defined later is that there is no chance to match again. The use of exact matching can improve the speed of search. For example, if you request / frequently, you can use = to define location.
Example
Next, let's use an example to illustrate the matching process.
Suppose we have the following configuration file:
Location = / {[configuration A]} location / {[configuration B]} location / user/ {[configuration C]} location ^ / images/ {[configuration D]} location ~ *\. (gif | jpg | jpeg) ${[configuration E]}
Request / exact match A, no longer look down.
Request / index.html matches B. First look for matching prefix characters, find the longest match is configuration B, and then look for matching regularities in order. The result was not found, so the longest match of the previous tag, configuration B.
Request / user/index.html matches C. The longest match C is found first, and since there is no matching regularity, the longest match C is used.
Request / user/1.jpg matches E. First of all, look for the prefix characters to find the longest match C, and then continue the regular search to find the match E. So use E.
Request / images/1.jpg matches D. First of all, the prefix characters are searched to find the longest matching D. However, what is special is that it uses the ^ ~ modifier and no longer does the next regular matching search, so it uses D. Here, without the previous modifier, the final match is actually E. You can think about why.
Request / documents/about.html matches B. Because B means that any URL that begins with / matches. In the above configuration, only B is satisfied, so it matches B.
The usage of location @ name
@ to define a named location. It is mainly used for internal redirection and cannot be used to handle normal requests. Its usage is as follows:
Location / {try_files $uri $uri/ @ custom} location @ custom {#... do something}
In the above example, when you try to access url and cannot find the corresponding file, you will redirect to our custom named location (custom in this case).
It is worth noting that no other named location can be nested in a named location.
Is / required at the tail of the URL
There are three points about the tail of URL that need to be explained. The first point is related to location configuration, and the other two points have nothing to do with it.
It doesn't matter whether there are characters in location or not. That means / user/ and / user are the same.
If the URL structure is in the form of https://domain.com/, the tail does not / will not cause redirection. Because the browser adds / by default when it initiates the request. Although many browsers do not display / in the address bar. You can verify this by visiting baidu.
If the structure of URL is https://domain.com/some-dir/. Lack of tail / will result in redirection. Because by convention, the / at the end of the URL represents a directory and no / represents a file. So when you visit / some-dir/, the server will automatically go to this directory to find the corresponding default file. If you visit / some-dir, the server will look for the some-dir file first. if you can't find it, it will take some-dir as a directory and redirect to / some-dir/, to find the default file in that directory. You can test your website to see if it is like this.
Summary
Location can be configured in two forms, prefix characters and regular. When looking for a match, first look for the prefix character, select the longest match, and then find the regular. Regular takes precedence over prefix characters.
Regular and other lookups are done in the order in the configuration file. Therefore, the order of regularity and so on is very important, and the finer the suggestion is, the more advanced it is.
Use = precise matching to speed up the search order. It is recommended to use = if the root domain name is frequently accessed.
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.