Sub topic 8
Testing each site before DNS points at it
This chapter assumes you can read a basic Nginx server block and that Nginx is running under systemd with at least two sites enabled, as in the earlier chapters. It also assumes you know what the HTTP Host header is and that you have curl installed.
Why test before DNS changes
DNS changes are slow to undo. Resolvers cache answers for the record's TTL, so a site that is broken when you switch can stay broken for some visitors after you fix it. Testing on the server first lets you find mistakes while nobody is affected.
Nginx doesn't use DNS to choose a site. It reads the Host header of each request and compares it to the server_name values. DNS only gets the visitor's request to your IP address. You can therefore send a request to your own server with any Host value you like and see which server block answers, without any DNS record existing.
Method 1: set the Host header with -H
The simplest test connects to the local address and overrides the header:
curl -H 'Host: example.org' http://127.0.0.1/If each document root holds a page that names its site (the earlier chapter created <h1>example.org</h1> and <h1>example.com</h1>), the output tells you at once which block answered:
<h1>example.org</h1>Add -i to see the status line and response headers, or -I to send a HEAD request and show only the headers. Headers such as Server, Location and Content-Type often explain a surprising result faster than the body does.
Run the same command for every name you plan to serve, including the www variants. Do not stop after the first success. A site that works can hide a second site that falls through to the wrong block.
Method 2: resolve the name locally with --resolve
The --resolve option tells curl to use a fixed address for one name and port, as if DNS had returned it:
curl --resolve example.com:80:127.0.0.1 http://example.com/The format is name:port:address. The URL keeps the real name, so curl sends Host: example.com on its own. This matters in two ways:
- Redirects behave realistically. If a block returns
301tohttp://example.com/, curl with-Lfollows it using the same fixed address. With-Hit would follow the redirect to the real DNS name, which may still point at the old server or at nothing. - HTTPS works properly. curl uses the URL's name for the TLS handshake (SNI) and for certificate checking. With
-Hand an IP address in the URL, curl sends no useful SNI, so Nginx picks a certificate for the wrong name.
Use -H for a quick check of plain HTTP. Use --resolve when you test redirects or HTTPS.
Testing HTTPS before DNS
A certificate from Let's Encrypt usually needs DNS to be in place first, because the validation request must reach your server by name. Until then you may have a self-signed or temporary certificate. You can still test which block and which certificate Nginx selects:
curl -k --resolve example.com:443:127.0.0.1 https://example.com/The -k option skips certificate verification, so use it only on a test you control. To see which certificate Nginx presents for a name, ask OpenSSL to send that name in the handshake:
openssl s_client -connect 127.0.0.1:443 -servername example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuerChange -servername to another site's name and the subject should change with it. If it doesn't, Nginx is using the default certificate for that name, which points to a server_name or ssl_certificate mistake.
Testing a whole set at once
Typing one command per name is easy to get wrong. A short loop checks every name and prints only the status code:
for host in example.com www.example.com example.org unknown.test; do
printf '%-18s ' "$host"
curl -s -o /dev/null -w '%{http_code}\n' -H "Host: $host" http://127.0.0.1/
doneA possible result, with a redirect block for www and a catch-all that returns 444:
example.com 200
www.example.com 301
example.org 200
unknown.test 000The 000 is how curl reports a connection closed without a response. That is the expected outcome of return 444; in the catch-all block. A name you expect to be handled by a real site but that shows 000 is going to the default server instead.
Testing from another machine
Tests against 127.0.0.1 prove the Nginx configuration but not the network path. After the local tests pass, repeat one request from another computer, using the server's public or private IP address:
curl --resolve example.com:80:203.0.113.10 http://example.com/Replace 203.0.113.10 with your server's address. If this fails while the local test works, the cause is outside Nginx: a firewall rule, a cloud security group, or Nginx listening only on a different address than the one you used. Use ss -tlnp | grep nginx on the server to see what Nginx is bound to.
Another option is an entry in /etc/hosts on your own workstation, such as 203.0.113.10 example.com example.org. This lets you open the sites in a browser before DNS changes. Remove the line afterwards, or you will keep seeing the old setup long after the real DNS change.
Comparison of the methods
| Method | What it changes | Good for | Limits |
|---|---|---|---|
| `curl -H 'Host: name'` | Only the Host header | Quick HTTP checks | No SNI for HTTPS, redirects go to real DNS |
| `curl --resolve name:port:ip` | Name resolution for one request | Redirects, HTTPS, realistic behavior | You must give the port each time |
| `/etc/hosts` on a client | Name resolution for the whole machine | Browser testing | Affects all programs, easy to forget |
| `openssl s_client -servername` | SNI value only | Checking which certificate is served | No HTTP response from the site |
Reading the results
Interpret each result against what you expected for that name:
- The right page and
200: the block matches and the files are readable. - The wrong site's page: the name matched another block, or no block matched and the default server answered. Check the spelling in
server_name, and check whether adefault_serveris set where you thought. 403: the block was selected, but Nginx can't read the directory or has no index file and directory listing is off. Check permissions on every directory in the path.404: the block was selected, but the file is not atrootplus the request path. Check therootpath for typos.301or302: a redirect block answered. Read theLocationheader with-Ito confirm the destination.000or "Empty reply from server": the connection was closed, which areturn 444;block does on purpose.- "Connection refused": nothing is listening on that address and port.
The wrong-page and 403/404 cases are different problems. In the first, Nginx chose the wrong block. In the other two it chose the right block and then failed on the file system.
Test after every change
Make the check a habit tied to the reload step. After sudo nginx -t && sudo systemctl reload nginx, run your loop. A new site can change the selection for existing sites, for example by adding a default_server or a wildcard name, so you should test all names and not just the new one.
Exercise
You run curl -H 'Host: example.org' http://127.0.0.1/ and receive the page of example.com. List two likely causes and one command for checking each.
Show answer
First, the example.org site may not be enabled or loaded, so no block has that name and Nginx falls back to the default block. Check with ls -l /etc/nginx/sites-enabled/ and sudo nginx -T | grep server_name to see which names are loaded, then reload after fixing. Second, the example.org block may have a misspelled server_name, or the example.com block may be the default server for this address and port. Run sudo nginx -T | grep -E 'server_name|listen' to compare the names and the default_server flag.
Exercise
Why does curl --resolve example.com:443:127.0.0.1 https://example.com/ give a more reliable HTTPS test than curl -k -H 'Host: example.com' https://127.0.0.1/?
Show answer
With --resolve, curl connects to 127.0.0.1 but uses the name example.com in the URL, so it sends example.com as the SNI value in the TLS handshake and as the Host header. Nginx can then choose the right server block and certificate. With an IP address in the URL, curl sends no useful SNI name, so Nginx may present the default certificate and select the wrong block before the Host header is read.