In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
When nginx receives a request, it matches a server through server_name, and then uses the location in server to continue the match.
Match server_name
In nginx, server_name determines which server will be used when a request is received. Nginx uses the Host field in the request header to match the server_name. When defining server_name, you can use full names, wildcard names, and regular expression names, which match in the following order:
Wildcard matching before exact matching, that is, wildcard matching after * .example.org, that is, mail.* regular expression matching
If there is no match, default_server will be used for processing. If it is not defined, the first one defined will be default_server. Use three simple server as examples and let them listen on port 80. The server_name is set to * .org, * .net, * .com:
Server {listen 80; server_name example.org www.example.org; return 401;} server {listen 80; server_name example.net www.example.net; return 402;} server {listen 80; server_name example.com www.example.com; return 403;}
In the above configuration, the default server is the first, and random access to a non-existent server will return 401. However, you can manually set a default host using default_server. The default_server is set in the listen field, as follows:
Server {listen 80 default_server; server_name example.net www.example.net;}
When matching later, it does not match to the server that will be used.
Access prohibited
If you want to disable a request without a Host field, you can define the following server:
Server {listen 80; server_name ""; return 444;}
Server_name is defined as an empty string. If the Host field is empty or does not exist, it will match to the server and return a 404 status code.
The 444 status of Nginx is special, if it returns 444, then the client will not receive the information returned by the server, just like the website cannot be connected. However, if you use a reverse proxy, the normal status code is still displayed.
If you want to disable access to a host that does not exist, you can define it as follows:
Server {listen 80 default_server; server_name _; return 444;}
_ does not have any special meaning here, because it will not appear in a domain name, so it will not be the same as any real domain name, and the use of other illegal characters is the same.
Match both IP and server_name
Now let's take a look at what happens when listening for a mix of different IP and different server_name:
Server {listen 192.168.1.1 server 80; server_name example.org www.example.org;} server {listen 192.168.1.1 server 80; server_name example.net www.example.net;} server {listen 192.168.1.2 server_name example.org www.example.org; 80; server_name example.com www.example.com;}
In this configuration, nginx first matches IP, then matches their server_name, and if it does not match server_name, their default server is used. For example, if a request with the domain name www.example.com comes from 192. 168. 1. 1. 1. However, there are only two server listening on 192.168.1.1 server 80, neither of which can match the www.example.com, so use the default host in these two server. Since defualt_server is not used to define listening, it defaults to the first one, that is, the server. Of course, you can define defualt_server:
Server {listen 192.168.1.1 server 80; server_name example.org www.example.org;} server {listen 192.168.1.1 server 80 default_server; server_name example.net www.example.net;} server {listen 192.168.1.2 server 80 default_server; server_name example.com www.example.com;}
Match location
After the nginx matches a server, the request will continue to be processed through location. Here is an example:
Server {listen 172.17.0.3 return 80; server_name _; location / {return 401;} location ~ *\. (gif | jpg | png) ${return 402;} location ~ *\. (gif | jpg | png) ${return 404;} location / api {return 403;}}
Nginx will first search all location for prefixes to match. After matching the prefixes, the location defined by regular expressions will be matched sequentially. If no matches are reached, the location that previously matched the prefixes will be used for processing. Here is an example of matching:
For a / x.gif request, the prefix is /, and then the remaining x.gif is used to match the regularities of location. (gif | jpg | png) $is returned. A / x.pdf request is processed using location / because the x.pdf cannot be matched. A / api/x.gif, first matches to the prefix / api, then uses the remaining x.gif to match the location regularities, and first matches to location ~ *\. (gif | jpg | png) $and returns 402. A / api/x.pdf request is processed using location / api because the x.pdf cannot be matched.
Referenc
How nginx processes a requestserver names
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.
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.