Server Blocks & server_name
One Nginx usually serves many sites. Each site is a server block, and the first decision for every request is which server block handles it. Getting this wrong is how a request for api.example.com ends up on the marketing site, or how anyone who points their own domain at your IP address gets your app served under their name.
Two steps: address, then name
listen: Nginx keeps only the server blocks that listen on the address and port the connection arrived on. A block withlisten 8080;listens on all addresses;listen 127.0.0.1:8080;only on that one, and the more specific address wins.server_name: among those, it compares the request'sHostheader (or the TLS SNI name for HTTPS) with each block'sserver_name, in this order:
| Priority | Form | Example |
|---|---|---|
| 1 | exact name | server_name example.com www.example.com; |
| 2 | longest wildcard starting with * |
server_name *.example.com; |
| 3 | longest wildcard ending with * |
server_name mail.*; |
| 4 | first matching regular expression, in file order | server_name ~^(?<tenant>[a-z]+)\.shop\.test$; |
| 5 | otherwise: the default server for that address:port | listen 8080 default_server;, or the first block in the file |
A special form .example.com matches both example.com and *.example.com. Regex captures become variables ($tenant above), which is how one block can serve many tenants.
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
access_log off;
default_type text/plain;
server {
listen 127.0.0.1:8080;
server_name shop.test;
return 200 "exact: shop.test\n";
}
server {
listen 127.0.0.1:8080;
server_name *.shop.test;
return 200 "leading wildcard for $host\n";
}
server {
listen 127.0.0.1:8080;
server_name ~^(?<tenant>[a-z]+)\.eu\.shop\.test$;
return 200 "regex: tenant=$tenant\n";
}
server {
listen 127.0.0.1:8080 default_server;
server_name _;
return 421 "unknown host $host\n";
}
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
for h in shop.test api.shop.test acme.eu.shop.test evil.example 127.0.0.1:8080; do
printf '%-20s -> ' "$h"; curl -s -H "Host: $h" 127.0.0.1:8080/
done
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/nullshop.test -> exact: shop.test
api.shop.test -> leading wildcard for api.shop.test
acme.eu.shop.test -> leading wildcard for acme.eu.shop.test
evil.example -> unknown host evil.example
127.0.0.1:8080 -> unknown host 127.0.0.1
acme.eu.shop.test matched the wildcard, not the regex: wildcards (priority 2) are checked before regular expressions (priority 4), however specific the regex is. To let the tenant regex win, give it a name the wildcard doesn't also cover, or make the wildcard narrower. server_name _; is just an invalid name that never matches; what makes the last block the catch-all is default_server.
Always define the default server yourself
Without an explicit default_server, the first server block for that port becomes the default. On a server with sites-enabled/ symlinks, "first" is alphabetical order of the included files, so adding 00-new-site.conf can quietly change which site answers unknown names, and requests for any domain someone points at your IP land on a real site, which search engines may then index under the wrong name.
A good catch-all refuses politely: 421 Misdirected Request or 404 for HTTP. Nginx also has the non-standard return 444;, which closes the connection without any response. For HTTPS, the catch-all needs a certificate to complete the handshake; since Nginx 1.19.4, ssl_reject_handshake on; refuses the handshake instead, so you don't present your real certificate to scanners probing by IP:
server {
listen 443 ssl default_server;
ssl_reject_handshake on; # no certificate needed; unknown SNI names get a TLS alert
}
server {
listen 80 default_server;
return 444;
}$host, $http_host and $server_name
| Variable | Value |
|---|---|
$http_host |
the Host header exactly as the client sent it, including a port; empty if absent |
$host |
the host from the request line, else the Host header lowercased without the port, else the matched server_name |
$server_name |
the first name of the matched server block, whatever the client sent |
Use $host when proxying (proxy_set_header Host $host;) and building redirects. Never build absolute URLs from $http_host in a block that is also the default server: it is attacker-controlled. The session shows it:
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
access_log off;
default_type text/plain;
server {
listen 127.0.0.1:8080;
server_name shop.test www.shop.test;
return 200 "host=$host http_host=$http_host server_name=$server_name\n";
}
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
curl -s -H "Host: WWW.Shop.Test:8080" 127.0.0.1:8080/
curl -s -H "Host: attacker.example" 127.0.0.1:8080/
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/nullhost=www.shop.test http_host=WWW.Shop.Test:8080 server_name=shop.test
host=attacker.example http_host=attacker.example server_name=shop.test
The only block is the default, so it also answered for attacker.example, and $host faithfully reported the attacker's name. A password-reset email that builds its link from that value sends the victim to the attacker (the classic "host header injection"). The fix is a separate default server, as above, plus an allow-list of hosts in the app.
Test yourself
Server blocks: server_name *.shop.test; and server_name ~^(?<t>.+)\.eu\.shop\.test$;. Which one serves acme.eu.shop.test?
- The regex, because it is more specific
- The wildcard: wildcard names are checked before regular expressions
- Whichever comes first in the file
- The default server
No server block has default_server. Which block answers a request with an unknown Host?
- None, Nginx returns 400
- The first server block that listens on that address and port
- The last one
- A random one
What does ssl_reject_handshake on; in an HTTPS default server do?
- Requires client certificates
- Rejects TLS handshakes for names no other server block matches, without presenting a certificate
- Disables TLS 1.2
- Redirects to HTTP