Forcing every visitor onto HTTPS is the smallest, most important Nginx change a site does. The minimal block is three lines; the trap is the one that catches certbot’s renewal because the redirect swallowed the ACME challenge. This article shows the redirect block we keep in every server config and what to do if you want HSTS preload on top.
How to verify
Check the current behaviour from the outside:
curl -sI http://example.com | head -5
curl -sIL http://example.com | grep -iE 'HTTP/|location'
curl -sI https://example.com | grep -i strict-transport-security
curl -sI http://example.com/.well-known/acme-challenge/test 2>&1 | head -5
A clean setup returns 301 Moved Permanently with Location: https://example.com/ on the first request, then a 200 (or other valid response) on the HTTPS hop. The ACME challenge path should return 200 or 404 — not a 301.
What’s happening
Nginx terminates TLS on port 443 and listens on port 80 for plain HTTP. To force HTTPS, you keep a small port-80 block whose only job is to redirect everything to the TLS counterpart. Return codes matter: 301 (permanent) is what you want once the redirect is stable and you want browsers and search engines to cache it; 302 (temporary) is the right choice during a rollout when you might roll back.
The HSTS header complements the redirect. The browser still does one HTTP request on first visit, sees the 301 to HTTPS, hits HTTPS, sees Strict-Transport-Security: max-age=…, and from then on goes straight to HTTPS without asking. With HSTS preload submitted to the browsers’ built-in lists, even the first visit goes to HTTPS.
The procedure
- The minimal redirect block — kept separate from the TLS block:
Theserver { listen 80; listen [::]:80; server_name example.com www.example.com; location /.well-known/acme-challenge/ { root /var/www/letsencrypt; } location / { return 301 https://$host$request_uri; } }/.well-known/acme-challenge/location is the crucial detail — certbot drops its challenge files there, and the redirect must not touch them. - The matching TLS block:
Theserver { listen 443 ssl http2; listen [::]:443 ssl http2; 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; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # ... the rest of the site config ... }alwaysargument onadd_headermakes Nginx emit it on error responses too. - Use
$hostnot$server_namein the redirect.$hostis the requested name;$server_nameis the first one in the block. If a visitor types a name not in your block list,$hostlets them land on themselves rather than your canonical name. - Reload:
sudo nginx -t && sudo systemctl reload nginx. - Validate from outside:
curl -sIL http://example.com/foo?bar=1should follow exactly one redirect tohttps://example.com/foo?bar=1and then return your normal response. - Decide on HSTS preload. Once you are confident every subdomain serves TLS and you will not need to roll back, extend the HSTS header and submit to
https://hstspreload.org/:
Once preloaded, the entry is hard to remove — browsers refresh their built-in list slowly.add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
Common pitfalls
- A catch-all redirect that includes
/.well-known/acme-challenge/. Certbot renews fail with “Invalid response from … 301”. The challenge location must come before the catch-all. - Redirecting to
https://www.example.comfromhttp://example.com(or vice versa) — that is two redirects for a single visitor, which slows the first hit. Pick the canonical name and redirect everything to it directly. - Using
rewrite … redirect;instead ofreturn 301. Both work, butreturnis faster and unambiguous;rewriteis for URL transformation, not protocol redirection. - HSTS on a host that mixes HTTP and HTTPS endpoints (legacy admin, mail interface). The header on the public site causes the HSTS browser cache to block the legacy ones.
- Forgetting
alwayson the HSTSadd_header—add_headeronly fires on 2xx, 3xx (some), and 1xx by default; a 503 from a maintenance page would not carry HSTS.
In the engagements we run, the redirect block is templated and never edited by hand; it is generated by the same Ansible role that manages the TLS material. See Nginx security headers for the wider header posture we apply. Stack Harbor operates this end-to-end as part of managed operations.