Sub topic 7
How Nginx Picks the Server Block for a Request
Assume you already know the Linux command line, basic HTTP requests, the Host header, and how to edit Nginx configuration. Nginx is installed and running under systemd.
A server block is a group of Nginx settings for one site. Several server blocks can listen on the same IP address and port. Nginx selects one block for each request by checking the listening address, port, and requested host name.
The selection process
Nginx makes its choice in stages:
- It finds the listening socket that received the request.
- It considers the
listendirectives attached to that socket. - It compares the request's host name with the
server_namevalues in those blocks. - It uses the matching block. If no name matches, it uses the default block for that socket.
For HTTP/1.1, the host name normally comes from the Host header. For example, a request for http://example.com/index.html includes Host: example.com.
`listen` selects the group
Consider these blocks:
server {
listen 80;
server_name example.com;
}
server {
listen 8080;
server_name example.com;
}A request received on port 80 can match only the first block. The second block belongs to a different listening socket, even though both blocks use the same server_name.
An address can also be specified:
listen 192.0.2.10:80;This block accepts requests sent to that address and port. A block using listen 127.0.0.1:80 belongs to a different address and is not a candidate for requests arriving on 192.0.2.10:80.
`server_name` selects the site
After Nginx identifies the listening group, it compares the host name. Exact names take priority:
server {
listen 80;
server_name example.com;
}
server {
listen 80;
server_name *.example.com;
}example.com uses the first block. shop.example.com uses the second block. If several wildcard names match, Nginx uses the most specific matching wildcard. Regular-expression names are checked later and follow their configuration order.
The server_name value does not create DNS records. DNS must still direct the domain to the server.
The default server
Each listening address and port has one default server. Mark it explicitly:
server {
listen 80 default_server;
server_name _;
return 444;
}The underscore is a conventional name that does not normally match a real domain. The non-standard Nginx status 444 closes the connection without sending an HTTP response. You could instead return a normal status such as 404.
If no block has default_server, Nginx uses the first server block loaded for that address and port. Relying on file order makes the result harder to understand, so declare the default explicitly.
> callout note > default_server belongs to listen, not to server_name. It controls unmatched requests for one address-and-port combination.
HTTPS and SNI
For HTTPS, Nginx must choose a certificate before it can read the encrypted HTTP Host header. Modern clients send the requested name during the TLS handshake using SNI, or Server Name Indication. Nginx uses SNI to select the HTTPS server block and its certificate. The request's host name is then used for normal HTTP processing.
Exercise
Exercise
Two blocks listen on port 80. One has server_name example.com; the other has server_name _ and listen 80 default_server. Which block handles a request with Host: unknown.example?
Show answer
The default server handles it because no exact or wildcard server_name matches unknown.example.