Why certbot --webroot instead of --nginx
certbot ships a --nginx plugin that rewrites files under /etc/nginx/conf.d/ and injects blocks stamped with # managed by Certbot. When your configs live in git and follow a clean snippet layout, automatic edits break structure and clutter git diff.
--webroot leaves nginx configs untouched and never stops the server (unlike --standalone, which needs a free port 80): certbot writes the challenge file to /var/www/certbot/.well-known/acme-challenge/, Let’s Encrypt fetches it through your running nginx, and after issuance certbot runs your --deploy-hook "systemctl reload nginx".
Issuing a certificate and auto-renewal
Before issuing, point the DNS A record (and AAAA if you use IPv6) to your server’s IP and open ports 80/tcp, 443/tcp, and 443/udp in the firewall (The ufw firewall).
Install certbot and prepare the challenge directory
sudo apt update sudo apt install -y certbot sudo mkdir -p /var/www/certbotIn your site config on port 80 (before adding the
443block), serve the ACME challenge path and redirect all other traffic to HTTPS:# /etc/nginx/conf.d/example.org.conf (before the first certificate) server { listen 80; server_name example.org www.example.org; location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; } location / { return 301 https://example.org$request_uri; } }Test syntax and apply:
sudo nginx -t && sudo systemctl reload nginx.Issue the certificate with a reload hook
Request a single certificate covering both names (apex and
www) and pass--deploy-hookso nginx picks up renewed files automatically:sudo certbot certonly --webroot -w /var/www/certbot \ -d example.org -d www.example.org \ -m [email protected] --agree-tos --no-eff-email \ --deploy-hook "systemctl reload nginx"Symlinks appear in
/etc/letsencrypt/live/example.org/:fullchain.pem(certificate plus chain) andprivkey.pem(private key).--deploy-hookis saved in/etc/letsencrypt/renewal/example.org.confand runs automatically after every successful renewal.Verify the auto-renewal timer
The Ubuntu
certbotpackage installs a systemd timer that runs twice a day and renews any certificate with fewer than 30 days left:systemctl list-timers certbot.timer sudo certbot renew --dry-runOnce
--dry-runfinishes with no errors, renewals run unattended. Wildcard certificates (*.example.org) can’t use HTTP validation and require a DNS challenge (see Traefik and Let’s Encrypt).
Shared TLS snippet
TLS settings are identical across every site on the host, so put them in a shared file at /etc/nginx/snippets/tls.conf.
ssl_protocols TLSv1.2 TLSv1.3;Allow only TLS 1.2 and TLS 1.3 (dropping legacy TLS 1.0 and 1.1). On OpenSSL 3.0+, TLS 1.3 ciphers (TLS_AES_256_GCM_SHA384 and siblings) and the X25519 curve default to modern choices.
default:ssl_protocols TLSv1.2 TLSv1.3 (since nginx 1.27.3)
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;Shared TLS session cache across all workers: 10 MB holds roughly 40,000 sessions and speeds up repeat connections for 24 hours. ssl_session_tickets off disables session tickets whose unrotated key in nginx persists until restart, preserving Forward Secrecy.
default:ssl_session_cache nonessl_session_timeout 5mssl_session_tickets on
ssl_stapling on; and ssl_stapling_verify on; are intentionally omitted: Let’s Encrypt shut down its OCSP responders in August 2025 and moved to CRLs. Only enable OCSP stapling for commercial CAs that still publish an OCSP URL.
Complete HTTPS config: catch-all, redirects, HTTP/2, and HTTP/3
Here is the full config for example.org on nginx 1.30: block IP-based certificate leaks, redirect http:// and www. to the canonical URL, and turn on HTTP/2 plus HTTP/3 (quic).
server {
listen 443 ssl default_server;
server_name _;
ssl_reject_handshake on;
}The default_server catch-all on port 443. If a client connects by server IP or an unknown SNI domain, ssl_reject_handshake on aborts the TLS handshake with unrecognized name instead of revealing any hosted site’s certificate. Keep one such block per server.
server {
listen 80;
server_name example.org www.example.org;
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://example.org$request_uri;
}
}Port 80: serves Let's Encrypt challenge files from /var/www/certbot and sends everything else to the canonical HTTPS URL with a permanent 301 redirect.
server {
listen 443 ssl;
http2 on;
server_name www.example.org;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
include snippets/tls.conf;
return 301 https://example.org$request_uri;
}Dedicated redirect from https://www.example.org to https://example.org. Requires a valid certificate covering both hostnames (which is why both -d flags were passed during issuance).
server {
listen 443 ssl;
listen 443 quic reuseport;
http2 on;
server_name example.org;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
include snippets/tls.conf;Primary server on 443. In nginx 1.25.1+, enable HTTP/2 with the standalone http2 on; directive (listen 443 ssl http2; is deprecated). listen 443 quic reuseport; opens UDP 443 for HTTP/3.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Alt-Svc 'h3=":443"; ma=86400' always;
root /var/www/example.org;
location / {
try_files $uri $uri/ =404;
}
}Alt-Svc advertises HTTP/3 on UDP 443 to browsers (cached for 24 hours). Strict-Transport-Security (HSTS) pins the site to HTTPS. Both headers carry always; if a child location defines its own add_header, re-include them via a snippet (Config anatomy).
Rolling out HSTS safely
Strict-Transport-Security forces browsers to use HTTPS for the full max-age window with no “Proceed anyway” bypass. If you jump straight to max-age=31536000; includeSubDomains while old.example.org lacks HTTPS, that subdomain becomes unreachable for visitors for a year.
Roll it out in two steps: start with max-age=300 without includeSubDomains, check every hostname and redirect, then raise max-age to a year and add includeSubDomains.
How to check
- Validate syntax and reload:
sudo nginx -t && sudo systemctl reload nginx - Confirm nginx listens on UDP 443 for HTTP/3 (
quic reuseportopens one socket per worker):sudo ss -lunp 'sport = :443' # UNCONN 0 0 0.0.0.0:443 0.0.0.0:* users:(("nginx",pid=41049,fd=15),...) - Check the TLS version, HTTP/2 ALPN negotiation, HSTS, and
Alt-Svcheaders:curl -svI --http2 https://example.org/ 2>&1 | grep -iE 'SSL connection using|ALPN|HTTP/|strict-transport|alt-svc' # * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / id-ecPublicKey # * ALPN: server accepted h2 # HTTP/2 200 # strict-transport-security: max-age=31536000; includeSubDomains # alt-svc: h3=":443"; ma=86400 - Verify
ssl_reject_handshakerefuses to serve a certificate for an unknown domain:openssl s_client -connect 203.0.113.10:443 -servername unknown.test </dev/null 2>&1 \ | grep -iE 'unrecognized|no peer' # ...:tlsv1 unrecognized name:...:SSL alert number 112 # no peer certificate available
If it doesn’t work
sudo nginx -t fails with duplicate listen options for 0.0.0.0:443
- Cause
- The
reuseportflag inlisten 443 quic reuseport;appears in two or moreserverblocks bound to the same IP and port. - Fix
- Keep
listen 443 quic reuseport;in oneserverblock on port 443 (for instance, your first site) and writelisten 443 quic;withoutreuseportin every other block.
certbot certonly --webroot fails with 404 or 403 on /.well-known/acme-challenge/…
- Cause
- The HTTP-to-HTTPS redirect intercepted the validation request, the
rootdirectory inlocationdoesn’t match certbot’s-wpath, or port 80 is blocked by a firewall. - Fix
- Verify port 80 is open (
sudo ufw status), placelocation ^~ /.well-known/acme-challenge/in thelisten 80;block pointing to/var/www/certbot, and drop a test file to verify withcurl http://example.org/.well-known/acme-challenge/test.
The Alt-Svc: h3=":443" header arrives, yet the browser stays on HTTP/2
- Cause
- Only TCP 443 is open in the host firewall or cloud security group; UDP 443 for QUIC is closed.
- Fix
- Open UDP 443 (
sudo ufw allow 443/udpplus the cloud provider firewall rule) and confirm nginx listens on UDP:sudo ss -lunp 'sport = :443'.
The certificate renewed under /etc/letsencrypt/live/, yet browsers still see the expired one
- Cause
- nginx workers hold the certificate in memory from startup, and
certbotupdated files on disk without reloading the service. - Fix
- Run
sudo systemctl reload nginxand addrenew_hook = systemctl reload nginxto/etc/letsencrypt/renewal/example.org.conf(or reissue with--deploy-hook "systemctl reload nginx").
/var/log/nginx/error.log logs "ssl_stapling" ignored, no OCSP responder URL
- Cause
- Let's Encrypt dropped OCSP URLs from new certificates in May 2025 and shut down its OCSP responders in August 2025, moving to CRLs.
- Fix
- Remove the obsolete
ssl_stapling on;,ssl_stapling_verify on;, andssl_trusted_certificatedirectives from site configs using Let's Encrypt certificates.