When the request reaches the nginx server
If our configuration file is as follows:
server { listen 80; server_name example.org www.example.org; ... } server { listen 80; server_name example.net www.example.net; ... } server { listen 80; server_name example.com www.example.com; ... }
1.nginx First, the server_name will be matched according to the Host in the request information, and the corresponding server will be selected
2.If the Host field in the request does not have the domain name format (that is, access the website in the ip format, such as : Enter 127.0.0.1 for access), Then nginx selects the first server by default
3.You can also add default_server to listen to indicate the default virtual service (listen 80 default_server ), then if the server_name cannot be found, ngnix will select this server by default
If you want to return an error message after failing to match the server_name, you can add the following server configuration
server { listen 80; server_name ''; return '404' }
Another configuration is as follows. Listen is ip:port
server { listen 192.168.0.1:80 server_name example.org *.example.org ... } server { listen 192.168.0.1:80 server_name example.net *.example.net ... } server { listen 192.168.0.2:80 server_name example.com *.example.com ... }
Then nginx will first select the one that matches the listen, and then match the server_name according to the Host. If it cannot match, you can make the default settings as above
For more Nginx related technical articles, please visit the Nginx usage tutorial column to learn!
The above is the detailed content of How nginx handles requests. For more information, please follow other related articles on the PHP Chinese website!