Lesson 4 of 9 · 24m
TLS Termination Without Superstition
Certificates, chains, modern protocol settings, redirects, HSTS, and the operational details that decide whether TLS is actually correct.
TLS at nginx is easy to enable and easy to misunderstand. A padlock in a browser does not mean your chain is complete, your redirect behavior is safe, your session resumption settings are sensible, or your application knows the request was originally HTTPS. Production TLS is a chain of custody problem: certificates, protocol policy, redirects, and forwarded metadata all have to agree.
Start with a correct certificate chain
nginx expects the server certificate followed by intermediates in one file. With Let's Encrypt, use fullchain.pem, not cert.pem:
server {
listen 443 ssl;
http2 on;
server_name learn.sudosecurity.org;
ssl_certificate /etc/letsencrypt/live/learn.sudosecurity.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/learn.sudosecurity.org/privkey.pem;
include snippets/tls-modern.conf;
}A browser may cache intermediates and hide a broken chain for months while command-line clients, mobile apps, and some operating systems fail immediately. Verify with an independent TLS checker or openssl s_client -connect host:443 -servername host after every certificate automation change.
Modern TLS is short and boring
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:TLS:10m;
ssl_session_tickets off;
ssl_stapling on;TLS 1.3 handles most cipher concerns for you. Long manually curated cipher strings are usually cargo cult. Session tickets are disabled here because shared ticket keys complicate forward secrecy and multi-host rotation; that is a deliberate tradeoff, not a universal commandment. OCSP stapling only works if nginx can reach the issuer resolver and the chain is complete.
Redirect HTTP with as little logic as possible
server { listen 80; server_name learn.sudosecurity.org; return 301 https://$host$request_uri; }Do not proxy ACME or internal health checks accidentally through the same redirect if they need HTTP. Give them precise locations or separate listeners. HSTS comes only after HTTPS is proven stable:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;HSTS is sticky. includeSubDomains means every subdomain must be HTTPS-capable. Do not copy a preload directive into production because a blog post said it was best practice; preload is effectively irreversible for real users.
Tell the application what happened
TLS termination means the application sees plain HTTP from nginx. It still needs original scheme and host to generate correct links and secure cookies:
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host;If redirects inside the application bounce HTTP to HTTPS incorrectly, check framework trusted-proxy settings before adding more nginx rewrites. Double redirects are usually two layers independently trying to be helpful.
Field notes
- Automate renewal, but alert on renewal failure before expiry; certificate automation fails silently more often than teams expect.
- A reload is enough for certificate replacement in normal nginx operation; a full restart is rarely needed.
- SNI means one IP can host many certificates, but clients without SNI are ancient and not a reason to weaken policy.
Was this lesson helpful?