Sub topic 8
Testing Each Site Before DNS Points at It
Assume Nginx is installed and running, the server blocks are enabled, and you understand DNS, HTTP, and the Host header. The goal is to test name-based routing while the public DNS records still point somewhere else.
Why the host name matters
When a browser requests http://example.org/, it sends an HTTP Host header:
Host: example.orgNginx uses the request's destination address and port to find matching listen directives. It then compares the Host header with server_name values among those candidates. The selected server block supplies the document root, proxy settings, logs, and other behavior.
A request to 127.0.0.1 normally sends Host: 127.0.0.1, so it may select the default server instead of the site you want. Testing the IP address alone does not prove that name-based routing works.
Send a chosen Host header
Use curl with the local address and explicitly choose the host name:
curl -i -H 'Host: example.com' http://127.0.0.1/
curl -i -H 'Host: example.org' http://127.0.0.1/The -i option displays response headers as well as the body. Check the status line, the Content-Type, any redirect location, and the returned page. Give each test site a distinctive temporary page, such as example.com or example.org, so that an incorrect match is obvious.
The same method works against a server's private or public IP address:
curl -i -H 'Host: example.com' http://203.0.113.10/This sends an ordinary HTTP request to 203.0.113.10, but asks Nginx to route it as example.com.
Use `--resolve` for a browser-like test
--resolve tells curl which address to use for one host name and port. It preserves the requested name in the URL and in the Host header:
curl -i --resolve example.com:80:127.0.0.1 http://example.com/
curl -i --resolve example.org:80:127.0.0.1 http://example.org/This is usually preferable to manually setting Host because it also tests name handling performed by the client. It does not change DNS and affects only that curl command.
For HTTPS, specify port 443 and use the HTTPS URL:
curl -i --resolve example.com:443:127.0.0.1 https://example.com/Certificate validation can fail if the test certificate is self-signed or does not contain the name. Do not treat -k as a production fix. It disables certificate verification and should be limited to controlled testing.
Follow the request through Nginx
If the wrong page appears, verify that the site file is linked in sites-enabled, that no duplicate server_name is active, and that the request uses the expected port. Run sudo nginx -T to inspect the complete loaded configuration.
Exercise
Which command tests the example.org server block at localhost without changing DNS?
Show answer
curl -i --resolve example.org:80:127.0.0.1 http://example.org/