Sub topic 3
Handling unknown host names with a default server
This chapter assumes you can edit files under /etc/nginx, run sudo nginx -t, reload Nginx with systemd, and have at least two server blocks already working. You should understand the listen and server_name directives and know how DNS points names at an IP address.
When a request arrives, Nginx picks a server block by matching the port and the Host header against each block. If nothing matches, Nginx uses the default server for that address and port. Mark one block with default_server:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}Code 444 closes the connection without a response. This keeps unknown host names from reaching your real sites.
Separate logs for each site
Give each server block its own log files so you can tell sites apart:
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}Redirecting www to the canonical host name
A dedicated block sends www traffic to the main name with a 301 redirect:
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}Putting an application behind a server block
proxy_pass forwards requests to an application on a local port. A snippet holds the shared headers, which the next section explains:
server {
listen 80;
server_name app.example.net;
location / {
proxy_pass http://127.0.0.1:3000;
include snippets/proxy-headers.conf;
}
}Serving several HTTPS sites from one IP address
Nginx uses SNI (Server Name Indication), a TLS extension that sends the requested host name during the handshake. Each HTTPS block then loads its own certificate:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example.com/html;
}Reusing settings with include snippets
Put repeated directives in one file and include it. Save this as /etc/nginx/snippets/proxy-headers.conf:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;Troubleshooting: wrong site, 403, 404 and failed reloads
Run sudo nginx -t before every reload. A failed test names the file and line. Then check the error log for the site:
- Wrong site served: the Host header matched a different block, or the request hit the default server. Check
server_nameand the symlink insites-enabled. - 403: Nginx cannot read the files. Check the permissions on the document root and its parent directories.
- 404: The
rootpath points to the wrong folder, or the file name is wrong.
sudo nginx -t && sudo systemctl reload nginx
sudo tail -n 20 /var/log/nginx/example.com.error.logExercise
A request for unknown.example reaches your default server, which returns 444. Why does the site for example.com still work?
Show answer
The request's Host header does not match any server_name, so Nginx uses the default server. The example.com block matches its own Host header, so Nginx selects that block for those requests.