In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the matching logic of Server and Location in Nginx". In daily operation, I believe that many people have doubts about the matching logic of Server and Location in Nginx. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "what is the matching logic of Server and Location in Nginx?" Next, please follow the editor to study!
Matching Logic of server
When nginx decides which server block to execute the request, it mainly focuses on the listen and server_name fields in the server block.
Listen instruction
The listen field defines the ip and port of the server response. If the listen field is not explicitly configured, the default listener is 0.0.0.0ip 8080 (root) or 0.0.0.0ip 8080 (non-root).
Listen can be configured to:
A combination of ip and port
A separate ip that listens to port 80 by default
A single port that listens on all ip interfaces by default
A unix socket path
The last item is usually only used to pass requests between different server
The rules for selecting the server to use are as follows:
Nginx first converts all the "incomplete" listen instructions, such as those without listen fields to listen 0.0.0.0 listen 80, listen 1.1.1.1, and so on.
Nginx creates a list of server blocks that best match the request based on the ip and port of the request, first matches the server block with a specific ip, and then selects the server block with listen 0.0.0.0. But in either case, the port must be an exact match
If there is only one best match, the matching server block will be used to respond to the request, otherwise the server_name instruction for each server block will be evaluated
Again, evaluating listen instructions is considered only if the server_name instruction cannot find the best match.
For example, let's assume that the example.com domain name points to 192.168.0.1, and the nginx on 192.168.0.1 has and only the following two server blocks:
# server block 1server {listen 192.168.0.1; server_name other.com...} # server block 2server {listen 80; server_name example.com...}
Server_name instruction
If the best match cannot be obtained according to the listen instruction, the server_name directive will be parsed. Nginx will check the "host" header in the request. This value contains the domain name or ip address that the client is actually trying to request. Nginx will match the server_name directive according to this value. The matching rules are as follows:
Nginx will try to find a server block that exactly matches the sever_name and host values. If multiple exact matches are found, the first matching server block will be used.
If no exact matching server block is found, nginx attempts to find the server block whose server_name begins with *. If more than one is found, the longest matching server block is selected.
If no server blocks are found that start with, then look for server blocks that end with them. Similarly, if there are multiple matches, select the longest match.
If no server blocks are found that use * matches, then look for server blocks that define server_name using regular expressions (starting with ~). If more than one match is found, the first match is used
If no server block is found that the regular expression matches, nginx will select a default server block that matches the listen field. Each ip and port combination can be configured with one and only one default default_server block. If not, the first server in the available list is selected (in this case, the selection is random and the order is not fixed)
Examples are as follows:
(1) accurate server_name matching, for example:
Server {listen 80; server_name www.domain.com;.}
(2) A string that begins with a * wildcard:
Server {listen 80; server_name * .domain.com;...}
(3) A string ending with a * wildcard:
Server {listen 80; server_name www.*;.}
(4) match regular expressions:
Server {listen 80; server_name ~ ^ (?. +)\ .domain\ .com $;...}
(5) if none of the above matches, use default_server. If no default_server is specified, the first available server. Exe is selected. We can specify that an error is returned to the client when there is no matching host value. It can be used to prevent others from diverting spam traffic to your site.
Server {listen 80 default_server; server_name _; return 444;}
Disconnect nginx from the browser by returning the non-standard error code 444 of nginx
Matching Logic of location
Location syntax parsing
Location optional_modifier location_match {...}
The available modifier modifiers are as follows
Decision rule
1. Nginx first checks prefix-based location matches (that is, matches without regular expressions)
2. If a location block with the = modifier exactly matches the requested url, use the location immediately to respond to the request
3. If no location block match with the = modifier is found, the inexact prefix will continue to be calculated, the longest matching prefix will be found based on the given uri, and then the following processing will be performed:
(1) if the longest matching location has a ^ ~ modifier, nginx immediately uses the location to respond to the request
(2) if the longest matching location does not have a ^ ~ modifier, nginx will temporarily store the match and then continue the subsequent matching.
4. After determining and saving the longest matching prefix location block, nginx continues to check for regular expression matching location (case-sensitive / case-insensitive). If there is a match of the regular expression that satisfies the requirement, the location of the first regular expression matching the requested uri is selected to correspond to the request
5. If a regular expression location that matches the requested uri is not found, the request is responded with the previously stored longest prefix location
Supplement
In general, once a location is chosen to respond to a request, the request will be processed within that location, regardless of other location. However, some instructions in location trigger new location matches, such as:
(1) try_files
(2) rewrite
(3) error_page
At this point, the study of "what is the matching logic of Server and Location in Nginx" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.