# 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.

URL: https://levelrail.com/learn/web-servers/nginx-https-with-certbot/
Updated: 2026-11-27

The short path to HTTPS is in [Nginx reverse proxy with free HTTPS using certbot](https://levelrail.com/learn/domains-https/nginx-reverse-proxy-with-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](https://levelrail.com/learn/glossary/#certbot)) is an [ACME](https://levelrail.com/learn/glossary/#acme)) client for [Let's Encrypt](https://levelrail.com/learn/glossary/#lets-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](https://levelrail.com/learn/domains-https/redirect-http-to-https-explained/)). `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](https://levelrail.com/learn/domains-https/tls-versions-and-ciphers-explained/)) 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](https://levelrail.com/learn/domains-https/hsts-explained/)) 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](https://levelrail.com/learn/domains-https/renew-lets-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](https://levelrail.com/learn/domains-https/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

| 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

```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;
```

## Related lessons

- [Nginx reverse proxy with free HTTPS using certbot](https://levelrail.com/learn/domains-https/nginx-reverse-proxy-with-certbot/)): the compact walkthrough.
- [Renew Let's Encrypt certificates](https://levelrail.com/learn/domains-https/renew-lets-encrypt-certificates/)): timers, hooks and checks.
- [Self-host a static site with Docker](https://levelrail.com/blog/self-host-static-site-docker/)): a site you might put this in front of.
- [Fix Let's Encrypt certificate errors](https://levelrail.com/learn/troubleshooting/fix-lets-encrypt-certificate-errors/)): a longer failure guide.

## Next

Now make responses smaller and faster with [gzip and caching headers](https://levelrail.com/learn/web-servers/gzip-and-caching-headers/)).

## 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

- [Certbot instructions](https://certbot.eff.org/instructions)
- [Nginx docs, ngx_http_ssl_module](https://nginx.org/en/docs/http/ngx_http_ssl_module.html)
