Sub topic 11
Redirecting HTTP to HTTPS and Enabling HSTS
Assume nginx is installed, DNS points example.com and www.example.com to this server, and Certbot has created the certificate files. The goal is to make every normal request use HTTPS, then tell browsers to use HTTPS automatically in the future.
Redirect HTTP to HTTPS
HTTP normally uses port 80. HTTPS uses port 443 and protects traffic with TLS. Keep port 80 available so nginx can return a redirect. Do not serve the application over plain HTTP.
A 301 redirect tells browsers and search engines that the move is permanent. $request_uri preserves the path and query string. For example, /login?next=/admin becomes the equivalent HTTPS URL.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}This configuration also chooses example.com as the canonical hostname. If you want to keep the hostname that the visitor used, use this redirect instead:
return 301 https://$host$request_uri;Use the first form when www.example.com should always redirect to example.com. Make sure that every name in server_name has a valid certificate, because the browser checks TLS before it can follow an HTTP-to-HTTPS redirect from port 443.
Configure the HTTPS server
The HTTPS server loads the certificate and private key. fullchain.pem contains the server certificate followed by the intermediate certificates that clients need to build a chain of trust.
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
root /var/www/html;
index index.html;
add_header Strict-Transport-Security "max-age=31536000" always;
}The always option makes nginx add the header even on error responses generated by this server block. The max-age value is measured in seconds. Here, browsers remember the HTTPS requirement for one year.
Enable HSTS carefully
HTTP Strict Transport Security, or HSTS, is a browser policy. After receiving the header over a valid HTTPS connection, a browser changes future HTTP links for that host to HTTPS before sending them. This reduces downgrade attacks and avoids an extra redirect.
HSTS does not protect the first visit if the browser has never seen the policy. It also does not repair an invalid certificate or make an unreachable HTTPS service work.
Start with a short duration while testing:
add_header Strict-Transport-Security "max-age=300" always;After confirming that every required page and subdomain works over HTTPS, increase the duration. Add includeSubDomains only when every subdomain can support HTTPS:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;Do not enable HSTS before checking services such as mail, monitoring, or administrative subdomains. A browser will refuse plain HTTP for those names while the policy is active.
Test the configuration
Always test nginx before reloading it:
sudo nginx -t && sudo systemctl reload nginx
curl -I http://example.com/
curl -sS -D - -o /dev/null https://example.com/The first response should contain 301 and a Location header beginning with https://. The HTTPS response should contain Strict-Transport-Security.
Common mistakes include placing the HSTS header only in the port-80 block, omitting fullchain.pem, redirecting to a hostname that is not covered by the certificate, and enabling includeSubDomains before all subdomains support TLS.
Exercise
Why should HSTS be enabled only after confirming that all required subdomains support HTTPS?
Show answer
A browser that receives includeSubDomains applies the HTTPS-only rule to those subdomains. Any subdomain that lacks working HTTPS can become unreachable through normal browser requests.