Skip to content

TLS on Nginx with Let's Encrypt — certbot setup and renewal

How to issue and auto-renew Let's Encrypt certificates for Nginx with certbot, plus the HSTS, OCSP stapling, and renewal-monitoring details that keep TLS healthy.

Public TLS on Nginx is a solved problem: certbot fetches a certificate from Let’s Encrypt, drops it under /etc/letsencrypt/live/, and a systemd timer renews it every 60 days. The interesting parts are the bits that fall off if you ignore them — HSTS, OCSP stapling, the renewal that is silently failing because the validation port is firewalled.

How to verify

Check what is in place and when it expires:

sudo certbot certificates
sudo openssl s_client -connect example.com:443 -servername example.com </dev/null \
  2>/dev/null | openssl x509 -noout -dates -issuer
sudo systemctl list-timers | grep -i certbot
curl -sI https://example.com | grep -i strict-transport-security

If s_client shows a cert issued by Let’s Encrypt and notAfter more than 30 days out, you are healthy. If the Strict-Transport-Security header is missing, browsers will still try HTTP first on the next visit.

What’s happening

Let’s Encrypt issues 90-day certs over the ACME protocol. The free version of certbot validates domain ownership by either responding to an HTTP-01 challenge (a file Nginx serves at /.well-known/acme-challenge/) or a DNS-01 challenge (a TXT record). HTTP-01 is the default when you run certbot --nginx — it edits your config to add the certificate paths and reloads. Renewal runs from a systemd timer (certbot.timer on Debian/Ubuntu) twice daily, but only does work if a cert is within 30 days of expiry.

OCSP stapling is the small but useful trick where Nginx fetches the cert’s revocation status from Let’s Encrypt once and serves it inline with the TLS handshake, removing the client’s need to do the OCSP lookup. HSTS tells browsers to never speak HTTP to your domain again, which closes the strip-down-to-http MITM window after the first visit.

The procedure

  1. Install certbot:
    sudo apt update
    sudo apt install -y certbot python3-certbot-nginx
  2. Make sure Nginx has a server block answering port 80 for the name you want to certify — certbot will use it for the HTTP-01 challenge. The DNS A/AAAA record must already point to the server.
  3. Issue the certificate:
    sudo certbot --nginx -d example.com -d www.example.com --redirect --hsts \
        --agree-tos --no-eff-email -m ops@example.com
    --redirect adds the 80→443 redirect; --hsts adds the HSTS header. Inspect the resulting site config before trusting the auto-edits.
  4. Confirm the modern ssl_ settings are in /etc/letsencrypt/options-ssl-nginx.conf (certbot includes it in every block):
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:le_nginx_SSL:10m;
    For tighter control, replace with a Mozilla “intermediate” template and reference it from each server block.
  5. Enable OCSP stapling. Add inside the SSL server block:
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    resolver 1.1.1.1 8.8.8.8 valid=300s ipv6=on;
  6. Test a dry-run renewal — this confirms the validation path still works without rate-limiting you:
    sudo certbot renew --dry-run
  7. Monitor renewal explicitly. The default certbot.timer will fail silently if port 80 is blocked or DNS no longer points at you. Set up a Prometheus probe or a script that calls openssl s_client … | openssl x509 -checkend $((30*86400)) and pages if the cert is inside 30 days.
  8. For wildcard certs, you need DNS-01. See Let’s Encrypt DNS-01 wildcard for the DNS plugin setup.

Common pitfalls

  • Issuing a cert before DNS propagated. The validation hits the wrong host and 404s. Wait for dig +short example.com to show your IP before running certbot.
  • Adding HSTS with a long max-age before you are sure every subdomain serves TLS. Once browsers cache it, you cannot serve HTTP on that name for the duration.
  • includeSubDomains on HSTS without first checking every subdomain has a valid cert. A single broken subdomain becomes unreachable to anyone who has visited the parent.
  • certbot --nginx leaves a backup of your config next to the original. Over a year you accumulate ten .conf.save files; tidy them.
  • A reverse-proxy block that proxies /.well-known/acme-challenge/ to the upstream — Nginx never sees the file certbot wrote, validation fails. The well-known path must be served by Nginx itself.

In the engagements we run, the most common Let’s Encrypt incident is a renewal that has been failing for two months, not a manual one going wrong. The fix is monitoring the cert age, not the renewal job — what matters is “is the cert valid”, not “did the script run”. Stack Harbor wraps this into managed operations alongside the other Nginx care-and-feeding.