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.
- Updated Nov 27, 2026
- Markdown
Get a certificate with certbot --nginx, read the config it writes, tighten TLS settings and confirm automatic renewal works.
- 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#
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:
sudo certbot --nginx -d example.com -d www.example.comCertbot 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:
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_certificateis the chain file, certificate plus intermediates. Usefullchain.pem, nevercert.pem.ssl_certificate_keyis the private key. Never copy it into a repo or a chat.- The
options-ssl-nginx.confinclude carries the protocol and cipher settings. - The second block is the redirect from HTTP to HTTPS.
301is 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:
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#
systemctl list-timers | grep certbot
sudo certbot certificates
sudo certbot renew --dry-runThe 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#
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 -datesThe 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 Certbotlines still works, but renewal needs the paths to stay valid. - A wildcard needs DNS validation.
*.example.comcannot use the HTTP challenge, so--nginxalone will not issue it. - Adding a name means a new certificate. Run certbot again with the full
-dlist, 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_nameexactly. - 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#
| Symptom | Likely cause | Fix |
|---|---|---|
Challenge failed for domain example.com | DNS points elsewhere or port 80 blocked | Fix the A record, open port 80 |
Timeout during connect (likely firewall problem) | Firewall or cloud group drops port 80 | Open 80 and 443 |
Could not automatically find a matching server block for example.com | No server_name match | Add server_name example.com;, reload, rerun |
Too many certificates already issued or too many failed authorizations | Rate limit after repeated attempts | Wait, fix the cause, test with --dry-run |
nginx: [emerg] cannot load certificate "/etc/letsencrypt/live/example.com/fullchain.pem" | Wrong path or certificate removed | Run sudo certbot certificates for the real name |
Browser shows NET::ERR_CERT_DATE_INVALID | Certificate expired, nginx not reloaded | Run sudo certbot renew, then reload nginx |
ERR_TOO_MANY_REDIRECTS | App also redirects, or proxy header missing | Pass X-Forwarded-Proto and fix the app |
certbot: command not found | Snap link missing | sudo ln -s /snap/bin/certbot /usr/bin/certbot |
Quick reference#
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 nginxssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;Related lessons#
- Nginx reverse proxy with free HTTPS using certbot: the compact walkthrough.
- Renew Let’s Encrypt certificates: timers, hooks and checks.
- Self-host a static site with Docker: a site you might put this in front of.
- Fix Let’s Encrypt certificate errors: a longer failure guide.
Next#
Now make responses smaller and faster with gzip and caching headers.
Check what you learned
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
Keep going: guides and tutorials
- Self-Hosted Static Site Hosting: Deploy a Folder With Levelrail Self-hosted static site hosting from a plain folder: one deploy command, _redirects, _headers and a custom 404 honored, plus a one command rollback.
- Deploy Next.js on a VPS With Docker, Git Push and HTTPS Deploy a Next.js app to your own VPS with Docker, a health check, HTTPS and rollback. Includes the NEXT_PUBLIC build trap that leaves variables empty.