How nginx process a HTTP request
One Host on one IP
The request’s header field “Host”. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.
If requests without the “Host” header field should not be allowed, a server that just drops the requests can be defined:
where: the code 444 is a special status nginx code that closes the connection.
One host multiple listen IP
- nginx first tests the IP address and port of the request against the listen directives of the server blocks.
- It then tests the “Host” header field of the request against the server_name entries of the server blocks that matched the IP address and port.
- If the server name is not found, the request will be processed by the default server.
- With the below configuration
- A request for www.example.com received on the 192.168.1.1:80 port
- will be handled by the default server (first server) of the 192.168.1.1:80 port
- because there is no www.example.com defined for this port.
server_name example.org www.example.org;
server_name example.com www.example.com;
After the server has been found, the second step is to find the location block.
The Location block processing follows the following algorithm;
- nginx first searches for the most specific prefix location given by literal strings regardless of the listed order.
- nginx checks locations given by regular expression in the order listed in the configuration file.
- The first matching regular expression is chosen
- If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.