Sub topic 9
Handling Unknown Host Names with a Default Server
This chapter assumes you can edit Nginx configuration files, use sudo, read a server block, and send basic HTTP requests with curl. You should also understand that a DNS name can resolve to an IP address without being a valid site name in Nginx.
Why a default server matters
A server can receive requests for many host names. Some names may be configured sites. Others may be misspelled, abandoned, or sent by a scanner. Nginx needs a rule for requests that do not match any configured server_name.
A default server is the server block Nginx uses when no more specific server name matches the requested address and port. It is selected for a particular listen socket, such as *:80 or [::]:80.
The name _ is commonly used in a catch-all block, but _ has no special catch-all meaning. The important setting is default_server.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}The 444 response is an Nginx-specific behavior. Nginx closes the connection without sending a normal HTTP response. You can instead return a normal status, such as 404, or redirect the request to an approved site.
How Nginx chooses the block
For a request arriving on one listen address and port, Nginx first considers the server blocks attached to that socket. It then compares the request's Host header with their server_name values.
| Request condition | Server block used |
|---|---|
| Host matches an exact name | The block with that name |
| Host matches a wildcard or regex | The best matching block |
| No name matches | The block marked `default_server` |
| No block is marked default | The first block loaded for that socket |
The default is tied to the address and port, not to the whole Nginx installation. You can have one default for port 80 and another for port 443. IPv4 and IPv6 sockets can also need separate declarations.
Configure a deliberate response
A clear 404 response is useful when clients should receive an HTTP status. A silent close can reduce unnecessary responses to unwanted traffic, but it can make testing less obvious.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
access_log /var/log/nginx/default.access.log;
error_log /var/log/nginx/default.error.log;
return 404;
}Place this block in an enabled configuration directory. Test it locally with a host name that no other block declares:
curl -i -H 'Host: unknown.example' http://127.0.0.1/You should see HTTP/1.1 404 Not Found. The request might still reach another block if the request uses a different address family or port than the default block covers.
Common errors
- Defining
default_servertwice for the same address and port causesnginx -tto fail. - Configuring only
listen 80 default_serverleaves IPv6 requests to[::]:80governed by another block. - Returning a redirect from the default block can create loops if the target name also resolves back to the same unexpected configuration.
- Testing with a known
Hostvalue does not test the default. Use an undeclared name or omit theHostheader where appropriate. - A default block does not replace correct
server_namevalues. It only handles requests left unmatched.
Always validate before reloading:
sudo nginx -t && sudo systemctl reload nginxExercise
Which directive makes a server block the fallback for unmatched requests on a socket?
Show answer
The default_server parameter on that socket's listen directive.