Set up SSL/TLS in nginx

nginx needs only a few lines for secure HTTPS. Most mistakes happen in three places: ssl missing from listen, the chain missing from the certificate, or a name missing from server_name.

The configuration

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

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;                      # nginx 1.25.1+, otherwise: listen 443 ssl http2;
    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;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;

    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    add_header Strict-Transport-Security "max-age=63072000" always;

    root /var/www/example;
}

The values follow Mozilla’s “intermediate” recommendation. For a specific nginx and OpenSSL version, the TLS config generator creates the matching variant, with HTTP/3 if you like.

Typical mistakes

RSA and ECDSA at the same time

nginx can serve two certificates in parallel and picks the right one per client. Simply specify ssl_certificate and ssl_certificate_key twice, once for ECDSA and once for RSA. For almost all sites an ECDSA certificate is enough today.

Check and reload

sudo nginx -t
sudo systemctl reload nginx

Then check chain and protocols in the SSL check, protocols with the HTTP/2 & HTTP/3 check and headers with the security headers check. For Let's Encrypt renewals to take effect, add --deploy-hook "systemctl reload nginx" to certbot.

FAQ

Do I need ssl_dhparam?

Only if you offer DHE ciphers. With the list above (ECDHE only) no dhparam file is needed.

How do I enable HTTP/3?

With an additional listen 443 quic, the Alt-Svc header and open UDP port 443. The guide is in the article enable HTTP/3 in nginx.

Why http2 on instead of listen … http2?

Since nginx 1.25.1 http2 is a separate directive; the old syntax produces a warning. Older versions only know the old form.

Sources and documentation