TLS
Certificates and chains, protocols, ALPN and HTTP/2, HSTS, and renewal
TLS gives three things: encryption (nobody on the path can read the traffic), integrity (nobody can change it), and authentication (the client knows it's talking to the real server, because the server proves it holds the private key for a certificate the client trusts).
The handshake, observed
docker compose -f compose_prod.yaml run --rm labs python tls_and_headers.py
TLS version: TLSv1.3
cipher: TLS_AES_256_GCM_SHA384
ALPN (protocol): h2
certificate for: ['localhost', 'nginx', '127.0.0.1']
issued by: Infra Labs CA
expires: Nov 13 16:04:55 2027 GMT
TLS 1.1 client: refused (NO_PROTOCOLS_AVAILABLE)
TLS 1.2 client: accepted (TLSv1.2)
SNI evil.example: TLSV1_UNRECOGNIZED_NAME
without the lab CA: certificate verify failed: unable to get local issuer certif...- SNI (Server Name Indication): the client says which name it wants before the server sends a certificate, so one IP can serve many sites.
- ALPN: client and server agree on the application protocol inside TLS.
h2means HTTP/2; Nginx enables it withhttp2 on;. - The certificate lists the names it's valid for in SAN (Subject Alternative Name). Clients check SAN, not the old Common Name.
- The chain: the client builds a path from the server certificate to a CA it trusts. A client without the lab CA refuses the connection: that's the system working.
Certificates and the lab CA
scripts/certs.sh plays both roles: it creates a CA, then signs a server
certificate with it.
Without the basicConstraints and keyUsage lines on the CA, curl
accepted the certificate and Python 3.13 rejected it: "CA cert does not
include key usage extension." Python now enables strict X.509 checks by
default, as browsers and Go do. Test certificates with the strictest
client you have, not the most forgiving one.
In production, certificates come from a public CA via ACME (Let's
Encrypt with certbot, acme.sh, or lego), or from an internal CA for
internal services. Public certificates have been capped at 200 days since
March 2026, on a schedule down to 47 days by 2029, which makes automated
renewal mandatory: certbot's
--deploy-hook "nginx -s reload" loads new certificates without dropping
connections.
Protocols and ciphers
The Mozilla SSL Configuration Generator is the usual starting point; "intermediate" matches this config.
HSTS
Strict-Transport-Security: max-age=63072000 tells browsers: for two
years, never use plain HTTP for this host, even if a link says http://.
It closes the window where the first request is unencrypted and could be
redirected by an attacker.
HSTS can't be undone quickly: browsers remember it for max-age. Turn it
on with a short max-age first, and add includeSubDomains only when
every subdomain serves HTTPS.
Port 80
plain HTTP: 301 -> https://nginx:8443/api/whoami
HTTP /healthz: 200Everything on port 80 redirects to HTTPS, except /healthz, kept on HTTP
for load balancers that check plain HTTP.
Monitoring expiry
The Prometheus stack probes the site over HTTPS with the Blackbox exporter,
which reports probe_ssl_earliest_cert_expiry. The alert
TLSCertificateExpiringSoon fires when that's under 14 days away. An
expired certificate is one of the most common full outages, and one of the
easiest to prevent.
Lab
- Run
openssl s_client -connect localhost:8443 -servername localhost -CAfile ca.crt </dev/nulland find the chain and the protocol. - Remove
TLSv1.2fromssl_protocolsand run the lab again. - Make a certificate without
IP:127.0.0.1in its SAN and requesthttps://127.0.0.1:8443/. What error do you get?