Sub topic 11
Redirecting `www` to the Canonical Host Name
Assume you know how Nginx matches listen and server_name, how to edit a site file, and how to run nginx -t and systemctl reload. This chapter uses example.com as the canonical host name, meaning the one public name you want users and search engines to use.
Why use a dedicated redirect server block?
A site may be reachable as both example.com and www.example.com. Serving the same content under both names can create inconsistent links, duplicate content, and confusing cookies or logs. A redirect gives clients one stable address.
Create a separate server block for the alternate name. It should not contain a document root. Its only job is to return an HTTP 301 redirect:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}The return directive sends the response immediately. The 301 status means “permanently moved.” The $scheme variable preserves http or https. The $request_uri variable preserves the requested path and query string. For example, /products?id=7 remains /products?id=7 after the redirect.
The canonical site needs its own server block:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}Both blocks can listen on port 80. Nginx uses the request's Host header to choose between their server_name values.
HTTPS and redirect loops
A browser must complete the TLS handshake before it can receive an HTTPS redirect. Therefore, the certificate presented for www.example.com must include that name, even if every HTTPS request will be redirected.
Use a separate HTTPS redirect block after obtaining a certificate covering the alternate name:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}Do not redirect example.com to www.example.com in another block at the same time. Opposing redirects create a loop. Also ensure that a TLS-terminating proxy is not telling Nginx the wrong scheme through forwarding headers.
Apply and test the change
Save the redirect block in the site configuration, enable it if your distribution uses sites-available and sites-enabled, then validate before reloading:
sudo nginx -t
sudo systemctl reload nginx
curl -I --resolve www.example.com:80:127.0.0.1 \
http://www.example.com/products?id=7The response should contain 301 and a Location header such as:
Location: http://example.com/products?id=7During testing, use 302 instead of 301 if you expect to change the rule repeatedly. Browsers cache permanent redirects.
Exercise
Why must the HTTPS certificate include www.example.com when that host only redirects?
Show answer
The TLS handshake happens before Nginx can send the HTTP redirect, so the certificate must be valid for www.example.com.