Sub topic 5
Matching Host Names with `server_name`
This chapter assumes that Nginx is installed and running, you can edit files with sudo, and you understand the HTTP Host header. You should also know that DNS maps a host name to an IP address. The examples use Debian and Ubuntu’s sites-available and sites-enabled layout.
How Nginx matches a request
A server block is a group of Nginx settings for one site or one group of host names. The listen directive selects the local address and port. The server_name directive lists the host names that the block can answer.
For an HTTP request, Nginx first finds server blocks listening on the requested address and port. It then compares the request’s Host header with their server_name values.
An exact name has the clearest behavior:
server_name example.com www.example.com;matches both names.- A wildcard such as
*.example.commatches subdomains, but not the bareexample.com. - A regular expression name can match more complex patterns, but exact names and simple wildcards are easier to maintain.
- If no name matches, Nginx uses the default server for that address and port. If no block is marked
default_server, the first matching block in the configuration order becomes the default.
The server_name _; value is often used in a catch-all block. The underscore is not a special wildcard. The default_server option is what makes the block the fallback.
Create two named sites
Create one server block for each domain. Each block can have its own document root:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name example.org;
root /var/www/example.org/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}The two blocks share port 80, but their host names distinguish them. A request for http://example.org/ uses the second block and looks for files under /var/www/example.org/html.
Enable the files with symlinks in sites-enabled, then validate before reloading:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxA reload makes Nginx read the new configuration without stopping existing connections. If nginx -t reports an error, the && prevents the reload.
:::callout note A request sent to an IP address often has a different Host header from a request sent to a domain name. That difference can make Nginx select a different server block. :::
Test before DNS changes
You can test matching locally by choosing the Host header yourself:
curl -H 'Host: example.org' http://127.0.0.1/
curl --resolve example.com:80:127.0.0.1 http://example.com/The first command connects to the local address and supplies example.org. The second makes curl resolve example.com to 127.0.0.1 for that request while keeping the host name in the request.
If the wrong page appears, check the Host value, confirm that the intended file is linked into sites-enabled, and inspect which block is the default. A 404 usually means the selected block’s root does not contain the requested file. A 403 often points to directory permissions or a missing index file.
Exercise
Two blocks listen on port 80. One has server_name example.com;, and the other is marked default_server. Which block answers a request whose Host header is unknown.test?
Show answer
The default server block answers because no server_name matches unknown.test.