Sub topic 9
Handling unknown host names with a default server
This chapter assumes you can edit Nginx configuration under /etc/nginx, that you know a request carries a Host header, and that you already have at least one working server block. It covers what Nginx does when no server_name matches, and how to take control of that case with an explicit default server.
What happens when no name matches
Nginx first narrows the candidate server blocks by the address and port the connection arrived on. Within that group it compares the request's Host header with each server_name. If nothing matches, Nginx does not return an error. It hands the request to the default server for that address and port.
Unless you say otherwise, the default server is the first server block that listens on that address and port in the loaded configuration. Include order decides "first". With the Debian layout, files in sites-enabled are read alphabetically, so the site whose file name sorts first becomes the accidental default.
This has a practical effect. Suppose example.com is the first block, and someone points unknown.test at your server's IP address. They receive the content of example.com. The same happens with requests addressed to the bare IP, and with scanners that probe IP ranges. Your real site is exposed under names you never chose, and search engines can index duplicate copies of it.
Marking a default server explicitly
Add the default_server parameter to the listen directive of exactly one block per address and port pair. The parameter belongs to the pair, not to the whole server. A block that listens on 80 and 443 needs the parameter on each listen line where you want it to apply.
A common pattern is a dedicated catch-all block that does nothing useful for the client:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}Three details matter here.
default_serveris what makes the block the fallback. The name does not._is only a conventional placeholder. It is a valid host name that no real client sends. Any name that never occurs would work the same. Removingserver_namealtogether also works, but then the block's name is an empty string, so_is clearer to readers.return 444is a non-standard Nginx code. It closes the connection without sending any response. The client sees an empty reply instead of an HTTP status.
You can use a different response if closing the connection is too abrupt. return 404; is a visible and harmless choice. return 301 https://example.com$request_uri; sends strays to your main site. A redirect has a cost: it can make a typo domain look like an alias of your site, so many administrators prefer 444 or 404.
Marking a real site as the default
You do not need a separate catch-all block. You can mark a real site instead:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
}This block still answers to its own names. It also answers anything unmatched. That is acceptable for a single-purpose host. On a server with several unrelated sites it means the main site absorbs traffic meant for nobody, so a separate catch-all is cleaner. It also keeps behaviour stable if you later remove or rename the real site.
Order and duplicates
Because marking is explicit, file order no longer matters. Two rules still apply:
- Only one block per
address:portpair may carrydefault_server. A second one makesnginx -tfail with a message such asa duplicate default server for 0.0.0.0:80. - Addresses count separately. A block with
listen 80;binds the wildcard address. A block withlisten 192.0.2.10:80;is a different pair. Each pair has its own default, and the more specific address wins when a connection arrives on it.
If you list the same pair in several files by accident, the error output from nginx -t names the file and line of the conflict.
Checking the default with curl
Use the technique from the previous chapters to send names that match nothing:
curl -i -H 'Host: not-a-real-site.test' http://127.0.0.1/
curl -i http://127.0.0.1/
curl -i -H 'Host: example.com' http://127.0.0.1/With the catch-all above, the first two commands print curl: (52) Empty reply from server. The first sends a name that matches nothing. The second sends Host: 127.0.0.1, which also matches nothing. The third still returns your real site. Test all three after every change. If the third fails, the catch-all is shadowing a real name, which usually means a typo in a server_name.
HTTPS and the default server
Port 443 needs more care, because the TLS handshake happens before Nginx sees the Host header. The server block must already hold a certificate to complete the handshake. A default server on 443 therefore needs its own ssl_certificate and ssl_certificate_key:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_certificate /etc/nginx/ssl/default.crt;
ssl_certificate_key /etc/nginx/ssl/default.key;
return 444;
}A self-signed certificate is enough, because no legitimate client should ever see it. You can create one with OpenSSL:
sudo mkdir -p /etc/nginx/ssl
sudo openssl req -x509 -nodes -newkey rsa:2048 -days 3650 \
-subj '/CN=default' \
-keyout /etc/nginx/ssl/default.key \
-out /etc/nginx/ssl/default.crt
sudo chmod 600 /etc/nginx/ssl/default.keyWithout a default on 443, an unknown name is served by the first 443 block, together with its real certificate. That reveals the certificate's name to anyone who connects to the bare IP address. Recent Nginx releases (1.19.4 and later) also offer ssl_reject_handshake on; in the catch-all block, which refuses the handshake and needs no certificate. Check your version with nginx -v before relying on it.
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_reject_handshake on;
}Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Unknown names show your first site | No `default_server` anywhere | Add a catch-all block |
| `duplicate default server` from `nginx -t` | Two blocks marked for the same address and port | Keep the parameter on one block |
| Catch-all works on port 80 but not on 443 | `default_server` only on the port 80 `listen` line | Add it to the 443 `listen` line too |
| Real site returns an empty reply | Its `server_name` is misspelled, so the catch-all took the request | Compare `server_name` with the `Host` you send |
| Requests to the IP address reach a real site | Real site marked `default_server` | Move the parameter to a catch-all block |
After any change, validate and reload as before:
sudo nginx -t && sudo systemctl reload nginxSummary of decisions
- Always define a default server for every address and port pair you serve.
- Prefer a separate catch-all block over marking a real site.
- Use 444 or 404 for strays, and a redirect only if you want typo domains to reach your main site.
- On HTTPS, give the default server its own certificate, or use
ssl_reject_handshake on;when your Nginx version supports it.
Exercise
Your server hosts example.com and example.org on port 80. A scanner requests http://203.0.113.5/ and receives the example.com home page. Explain why this happens, and write the block that stops it. Then name the curl command that confirms the fix without changing DNS.
Show answer
The request carries Host: 203.0.113.5, which matches no servername. No block is marked defaultserver, so Nginx uses the first block that listens on port 80, which is example.com. Add a catch-all block: server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 444; }, run sudo nginx -t && sudo systemctl reload nginx, then confirm with curl -i -H 'Host: not-a-real-site.test' http://127.0.0.1/, which should report an empty reply. Also check curl -i -H 'Host: example.com' http://127.0.0.1/ still returns the real site.
Exercise
You add listen 443 ssl default_server; to a catch-all block and nginx -t reports a duplicate default server for 0.0.0.0:443. What do you check, and what is the fix?
Show answer
Another server block, possibly in a different file under sites-enabled or conf.d, already carries default_server on its port 443 listen line. Search with grep -R default_server /etc/nginx/. Remove the parameter from the older block, or remove your new one if the old one is the intended default, so that only one block per address and port pair has it.