basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ManualHomelabnginx

    HTTPS, TLS 1.3, HTTP/2, and HTTP/3 in nginx

    Issue and auto-renew Let's Encrypt certificates with certbot --webroot without letting it edit your configs, configure TLS 1.3, redirects, and HSTS, stop certificate leaks by IP with ssl_reject_handshake, and enable HTTP/2 and HTTP/3 over QUIC.

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

    1. Install certbot and prepare the challenge directory

      sudo apt update
      sudo apt install -y certbot
      sudo mkdir -p /var/www/certbot

      In your site config on port 80 (before adding the 443 block), 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.

    2. Issue the certificate with a reload hook

      Request a single certificate covering both names (apex and www) and pass --deploy-hook so 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) and privkey.pem (private key). --deploy-hook is saved in /etc/letsencrypt/renewal/example.org.conf and runs automatically after every successful renewal.

    3. Verify the auto-renewal timer

      The Ubuntu certbot package 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-run

      Once --dry-run finishes 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.

    /etc/nginx/snippets/tls.conf
    ssl_protocols TLSv1.2 TLSv1.3;
    Key part

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

    /etc/nginx/conf.d/00-default-ssl.conf and example.org.conf
    server {
        listen 443 ssl default_server;
        server_name _;
        ssl_reject_handshake on;
    }
    Key part

    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;
    Key part

    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;
        }
    }
    Key part

    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

    1. Validate syntax and reload:
      sudo nginx -t && sudo systemctl reload nginx
    2. Confirm nginx listens on UDP 443 for HTTP/3 (quic reuseport opens 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),...)
    3. Check the TLS version, HTTP/2 ALPN negotiation, HSTS, and Alt-Svc headers:
      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
    4. Verify ssl_reject_handshake refuses 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 reuseport flag in listen 443 quic reuseport; appears in two or more server blocks bound to the same IP and port.
    Fix
    Keep listen 443 quic reuseport; in one server block on port 443 (for instance, your first site) and write listen 443 quic; without reuseport in 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 root directory in location doesn’t match certbot’s -w path, or port 80 is blocked by a firewall.
    Fix
    Verify port 80 is open (sudo ufw status), place location ^~ /.well-known/acme-challenge/ in the listen 80; block pointing to /var/www/certbot, and drop a test file to verify with curl 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/udp plus 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 certbot updated files on disk without reloading the service.
    Fix
    Run sudo systemctl reload nginx and add renew_hook = systemctl reload nginx to /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;, and ssl_trusted_certificate directives from site configs using Let's Encrypt certificates.