Sub topic 10
Serving the certificate from nginx
This chapter assumes you already hold a certificate for example.com and www.example.com under /etc/letsencrypt/live/example.com/. It also assumes nginx is installed and serving plain HTTP on port 80, and that ports 80 and 443 are open. You should be comfortable editing nginx configuration files and using systemctl. Replace example.com with your own domain throughout.
What nginx needs to do
Issuing a certificate does nothing until the web server presents it. During the TLS handshake, nginx sends the client a certificate chain. It then proves it owns the matching private key by signing handshake data. nginx therefore needs two files and one instruction to listen for TLS.
- The certificate chain:
fullchain.pem. - The private key:
privkey.pem. - A
listen 443 ssl;directive, which tells nginx to speak TLS on that socket.
The browser sends the host name in the ClientHello message. This is called SNI (Server Name Indication). nginx uses it to choose which server block, and so which certificate, to present. This is how one IP address can serve many HTTPS sites.
A complete server block
Put the configuration in its own file. On Debian and Ubuntu that is /etc/nginx/sites-available/example.com, enabled with a symlink into sites-enabled.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
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;
add_header Strict-Transport-Security "max-age=31536000" always;
root /var/www/html;
}The first block and the add_header line are covered in the next chapter on redirects and HSTS. Here the focus is on the ssl_* directives and the way nginx loads them.
Directive by directive
| Directive | Purpose |
|---|---|
| `listen 443 ssl;` | Accepts TLS connections on port 443. The `[::]` line does the same for IPv6. |
| `ssl_certificate` | Path to the certificate chain nginx sends to clients. |
| `ssl_certificate_key` | Path to the matching private key. It is never sent to clients. |
| `ssl_protocols` | TLS versions nginx accepts. TLS 1.0 and 1.1 are deprecated and should stay off. |
Two details are easy to miss.
First, point ssl_certificate at fullchain.pem, not cert.pem. The cert.pem file holds only your server's own certificate. Browsers usually have the root CA certificate built in, but they often do not have the intermediate certificate that links your certificate to it. Without the intermediate, desktop browsers sometimes cope by fetching it, but tools such as curl, Java clients and mobile apps may fail with "unable to get local issuer certificate". fullchain.pem contains your certificate followed by the intermediates, so it works for every client.
Second, use the live/ symlinks, not the files in archive/. Certbot updates the symlinks on each renewal. If you hardcode archive/example.com/fullchain1.pem, nginx will keep serving the old certificate after the first renewal.
Optional tuning
The defaults in current nginx versions are acceptable for most sites. Two additions are common:
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;The session cache lets returning clients resume a session without a full handshake. A 10 MB cache holds roughly 40,000 sessions.
You do not need to hand-pick ssl_ciphers for TLS 1.3, because its cipher suites are fixed. For TLS 1.2, nginx's defaults are reasonable. Copying cipher lists from old guides is a frequent source of weakened or broken configurations.
Do not enable ssl_stapling for Let's Encrypt certificates. Let's Encrypt stopped providing OCSP responses in 2025, so nginx would log warnings and stapling would have no effect.
If your nginx is 1.25.1 or newer and you want HTTP/2, add a separate http2 on; line inside the server block. Older versions use listen 443 ssl http2; instead, and newer versions still accept that form but mark it as deprecated.
Test, then reload
Always check the syntax before applying a change.
sudo nginx -t && sudo systemctl reload nginxnginx -t parses the configuration and also opens the certificate and key files. It fails if a path is wrong, if the files are unreadable, or if the certificate and key do not match. The && means the reload only happens when the test passes.
Use reload, not restart. A reload starts new worker processes with the new configuration while old workers finish their open connections, so no requests are dropped. A reload is also what makes nginx pick up a renewed certificate, since nginx reads the files only at startup and reload time.
Permissions and the private key
The nginx master process runs as root, so it can read /etc/letsencrypt/live/ and /etc/letsencrypt/archive/ even though the private key is restricted. Worker processes, which run as www-data or nginx, never need to open the key files. Do not loosen the permissions on privkey.pem to "fix" an error. If nginx cannot read the key, the cause is almost always a wrong path, because the master process already runs as root.
Several sites on one server
Each HTTPS site gets its own server block with its own server_name and certificate paths. nginx selects the block by SNI. When a client sends no SNI, or a name that matches no block, nginx falls back to the default server for that port. By default that is the first server block defined for the port. This matters for security and tidiness: a visitor who requests your bare IP address would be shown that block's certificate, and the browser would show a name mismatch error.
To control this, mark one block as the default. You can make it reject unknown names outright:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 444;
}The 444 status is nginx-specific. It closes the connection without sending a response. The certificate lines are still required, because a TLS server block without a certificate fails the configuration test, with an error such as "no ssl_certificate is defined".
Common errors
| Symptom | Likely cause |
|---|---|
| `nginx -t` reports `cannot load certificate ... No such file` | Wrong path, or the certificate name differs from the domain. Run `sudo certbot certificates` to see the real name. |
| `key values mismatch` | The certificate and key come from different issuances. Point both at the same `live/` directory. |
| nginx starts but port 443 is refused | Missing `ssl` on the `listen` line, or a firewall blocking 443. |
| Browser shows the wrong site's certificate | The request matched a different or default `server` block. Check `server_name`. |
| Browser still shows the old expiry date after renewal | nginx has not been reloaded. See the chapter on renewal hooks. |
| Plain-HTTP error page on port 443 | `listen 443;` without `ssl`, so nginx is speaking HTTP. |
Exercise
A colleague points ssl_certificate at /etc/letsencrypt/live/example.com/cert.pem. Browsers load the site without complaint, but a curl call from an older server fails with "unable to get local issuer certificate". What is wrong, what is the fix, and what must you run for it to take effect?
Show answer
cert.pem contains only the server's own certificate and omits the intermediate certificates. Some browsers fetch the missing intermediate themselves, but curl and similar clients do not. Change ssl_certificate to /etc/letsencrypt/live/example.com/fullchain.pem, then run sudo nginx -t && sudo systemctl reload nginx so nginx reads the new file.
Exercise
You hardcoded /etc/letsencrypt/archive/example.com/fullchain1.pem in your config. Why will this break after the first renewal, even if you reload nginx?
Show answer
Certbot writes renewed certificates as new numbered files (fullchain2.pem and so on) and moves the live/ symlinks to them. A hardcoded archive path keeps pointing at the old certificate, so nginx continues to serve it until it expires. Use the live/ symlink paths instead.