Chapter 8 · Lesson 4 of 8
Web servers and proxies

Nginx HTTPS with certbot: what it edits and how to harden it

Certbot with the nginx plugin proves you own a domain, saves a Let's Encrypt certificate under /etc/letsencrypt/live and edits your server block to serve HTTPS.

By the end you will

Get a certificate with certbot --nginx, read the config it writes, tighten TLS settings and confirm automatic renewal works.

Time
25 min
Level
Intermediate
Check
4 questions
You need
  • An nginx server block that already answers on port 80 for your domain
  • A domain whose A record points at the server
  • Ports 80 and 443 open

The short path to HTTPS is in Nginx reverse proxy with free HTTPS using certbot. This lesson goes deeper on the part that lesson skips: what certbot writes into your files, which TLS settings matter, how renewal runs and what the common failures look like.

How it works#

Certbot is an ACME client for Let’s Encrypt. With the nginx plugin it finds the server block whose server_name matches your domain, answers the HTTP-01 challenge by serving a token at /.well-known/acme-challenge/ on port 80, receives the certificate, then edits that block to use it.

The certificate lives in /etc/letsencrypt/live/example.com/. Files there are links to numbered versions in /etc/letsencrypt/archive/, so renewals replace the target without changing the path in nginx. A systemd timer (or a cron entry on older setups) runs certbot renew twice a day, and it only renews certificates that are close to expiry.

Step 1: Check the starting server block#

nginx
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
}

Certbot matches on server_name, so it must list every name you want on the certificate. Run sudo nginx -t and make sure curl -I http://example.com already works. DNS must point here, or the challenge fails.

Step 2: Install certbot and run it#

Install certbot using the instructions on https://certbot.eff.org/instructions (the snap route, or the python3-certbot-nginx package from apt). Then:

bash
sudo certbot --nginx -d example.com -d www.example.com

Certbot asks for an email (used for expiry notices) and whether to redirect HTTP to HTTPS. Choose the redirect for a real site. The names after -d must all resolve to this server.

Step 3: Read what changed#

Open the file you edited. Certbot marks its lines with # managed by Certbot:

nginx
server {
    server_name example.com www.example.com;
    root /var/www/example.com/html;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

The exact lines vary by certbot version. The pieces to know:

  • ssl_certificate is the chain file, certificate plus intermediates. Use fullchain.pem, never cert.pem.
  • ssl_certificate_key is the private key. Never copy it into a repo or a chat.
  • The options-ssl-nginx.conf include carries the protocol and cipher settings.
  • The second block is the redirect from HTTP to HTTPS. 301 is cached by browsers, so make sure HTTPS works before you rely on it.

Step 4: Set the TLS options yourself if you want to#

The included file sets sensible defaults. If you manage them yourself, the key lines are:

nginx
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;

ssl_protocols drops old TLS versions. ssl_session_cache lets returning clients skip part of the handshake. See TLS versions and ciphers for what to allow.

OCSP stapling is optional, and with Let’s Encrypt you can skip it: Let’s Encrypt ended its OCSP service, so new certificates carry no OCSP responder URL and nginx logs a warning that stapling is ignored if you turn on ssl_stapling on;. Add HSTS only after HTTPS works everywhere, because browsers remember it.

Step 5: Confirm renewal#

bash
systemctl list-timers | grep certbot
sudo certbot certificates
sudo certbot renew --dry-run

The timer should be listed, certificates shows the expiry date, and the dry run should end with Congratulations, all simulated renewals succeeded. More in renew Let’s Encrypt certificates.

Check it worked#

bash
sudo nginx -t
curl -I http://example.com
curl -I https://example.com
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates

The first curl shows 301 with Location: https://example.com/, the second shows 200 with no certificate error, and openssl prints notBefore and notAfter. For a deeper look use inspect a TLS certificate with openssl.

Edge cases and gotchas#

  • Certbot edits your file, so keep it in git or backed up. A later hand edit that removes the # managed by Certbot lines still works, but renewal needs the paths to stay valid.
  • A wildcard needs DNS validation. *.example.com cannot use the HTTP challenge, so --nginx alone will not issue it.
  • Adding a name means a new certificate. Run certbot again with the full -d list, or use --expand, so the new name is on the certificate.
  • The default site can answer the challenge. If another block listens on port 80 first, validation fetches the wrong site. Match server_name exactly.
  • A firewall that allows 443 but not 80 breaks renewal. The challenge uses port 80 every time, not only at first issue.
  • IPv6 mismatch. A stale AAAA record that points at another server makes Let’s Encrypt validate there and fail.
  • Reload, not restart. After a manual certificate change run sudo nginx -t && sudo systemctl reload nginx.

Troubleshooting#

SymptomLikely causeFix
Challenge failed for domain example.comDNS points elsewhere or port 80 blockedFix the A record, open port 80
Timeout during connect (likely firewall problem)Firewall or cloud group drops port 80Open 80 and 443
Could not automatically find a matching server block for example.comNo server_name matchAdd server_name example.com;, reload, rerun
Too many certificates already issued or too many failed authorizationsRate limit after repeated attemptsWait, fix the cause, test with --dry-run
nginx: [emerg] cannot load certificate "/etc/letsencrypt/live/example.com/fullchain.pem"Wrong path or certificate removedRun sudo certbot certificates for the real name
Browser shows NET::ERR_CERT_DATE_INVALIDCertificate expired, nginx not reloadedRun sudo certbot renew, then reload nginx
ERR_TOO_MANY_REDIRECTSApp also redirects, or proxy header missingPass X-Forwarded-Proto and fix the app
certbot: command not foundSnap link missingsudo ln -s /snap/bin/certbot /usr/bin/certbot

Quick reference#

bash
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot certificates
sudo certbot renew --dry-run
systemctl list-timers | grep certbot
sudo nginx -t && sudo systemctl reload nginx
nginx
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;

Next#

Now make responses smaller and faster with gzip and caching headers.

Check what you learned4 questions
1Which certificate file should ssl_certificate point to?
2How do you confirm renewal will work without waiting 60 days?
3Certbot says the challenge failed on port 80. What do you check first?
4Why can a redirect from HTTP to HTTPS break renewal?

Frequently asked questions

What does certbot --nginx change in my config?

It adds a listen 443 ssl block (or edits yours) with ssl_certificate and ssl_certificate_key paths, an include of Certbot's TLS options file, and a redirect from port 80 if you choose one.

How do I test certbot renewal?

Run sudo certbot renew --dry-run. It goes through the renewal against the staging server without replacing your real certificate.

Where are Let's Encrypt certificates stored on Ubuntu?

Under /etc/letsencrypt/live/<domain>/. The files there, such as fullchain.pem and privkey.pem, are links to the current versions in the archive folder.

Do I need to reload nginx after a certificate renews?

Yes, nginx must load the new certificate. Certbot's nginx plugin reloads nginx after a successful renewal, and you can check it with the dry run.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios