Skip to content

HAProxy certificate rotation

Hot-rotate TLS certificates in HAProxy via the runtime API — Let's Encrypt automation, the transactional set/commit pattern, OCSP refresh, and the cron we ship to clients.

Certificate rotation in HAProxy used to require a full reload — fine for a few certs, painful for a fleet with hundreds. The runtime API now lets you hot-swap certificates atomically: load the new bundle, validate it, commit it. The old cert keeps serving until the commit; if the new cert is malformed, the commit fails and the old one stays live. This article covers the rotation flow, the Let’s Encrypt automation pattern, OCSP refresh, and the cron we ship.

How to verify

For an existing cert, the runtime API and openssl together tell you what is loaded and when it expires:

echo "show ssl cert" | sudo socat /run/haproxy/admin.sock - | head -10
echo "show ssl cert /etc/haproxy/certs/example.com.pem" | sudo socat /run/haproxy/admin.sock -
openssl x509 -in /etc/haproxy/certs/example.com.pem -noout -dates -subject -issuer
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates

The last command is the source of truth — what the wire actually presents — versus what is on disk. They should match; if they don’t, HAProxy has the old cert cached and a runtime reload of the cert is overdue.

What’s happening

HAProxy reads PEM bundles at startup and on systemctl reload. The runtime API adds an atomic update path:

  1. Set the new PEM via set ssl cert <path> <<EOF ... EOF. HAProxy parses and validates the new bundle but doesn’t activate it.
  2. Commit with commit ssl cert <path>. HAProxy atomically swaps the in-memory reference; new TLS handshakes use the new cert, existing TLS connections keep using the old one until they close.
  3. Abort with abort ssl cert <path> if you want to discard the staged update.

The old cert keeps serving until the commit. If the staged cert fails to parse (missing intermediate, bad PEM, key/cert mismatch), the commit fails with a clear error and the old cert stays live. No race window, no downtime.

The on-disk PEM file is just the input — HAProxy keeps the parsed bundle in memory. Updating the file on disk does not update the in-memory cert; you must set + commit (or reload) for HAProxy to pick up the change.

The procedure

  1. Standard rotation flow. Given a new PEM at /tmp/new-example.com.pem:

    sudo cp /tmp/new-example.com.pem /etc/haproxy/certs/example.com.pem
    echo "set ssl cert /etc/haproxy/certs/example.com.pem <<\nEND" | sudo socat /run/haproxy/admin.sock -
    # In practice, the cert payload must be sent as part of the socat input:
    {
      printf 'set ssl cert /etc/haproxy/certs/example.com.pem <<\n'
      cat /etc/haproxy/certs/example.com.pem
      printf '\n\n'
    } | sudo socat /run/haproxy/admin.sock -
    echo "commit ssl cert /etc/haproxy/certs/example.com.pem" | sudo socat /run/haproxy/admin.sock -

    The << here-doc syntax in the runtime API is whitespace-sensitive — an empty line ends the payload. In a script, use socat with EXEC:cat or feed the payload as one stream.

  2. Production-grade rotation script. A shell helper that handles the choreography:

    #!/bin/bash
    set -euo pipefail
    CERT_PATH=$1
    SOCKET=/run/haproxy/admin.sock
    
    # validate locally before staging
    openssl x509 -in "$CERT_PATH" -noout -checkend 86400 || { echo "cert expires within 24h"; exit 1; }
    
    (
        echo "set ssl cert $CERT_PATH <<"
        cat "$CERT_PATH"
        echo
        echo
    ) | sudo socat "$SOCKET" -
    
    echo "commit ssl cert $CERT_PATH" | sudo socat "$SOCKET" - | grep -q 'Success' \
      || { echo "commit failed"; echo "abort ssl cert $CERT_PATH" | sudo socat "$SOCKET" -; exit 1; }
    echo "rotated $CERT_PATH"
  3. Let’s Encrypt automation with certbot. Use a deploy-hook so certbot pushes the new cert to HAProxy after renewal:

    sudo certbot certonly --webroot -w /var/www/acme-challenge -d example.com --deploy-hook /usr/local/sbin/hap-cert-deploy
    cat > /usr/local/sbin/hap-cert-deploy <<'EOF'
    #!/bin/bash
    set -euo pipefail
    for domain in $RENEWED_DOMAINS; do
        LIVE=/etc/letsencrypt/live/$domain
        TARGET=/etc/haproxy/certs/$domain.pem
        cat "$LIVE/fullchain.pem" "$LIVE/privkey.pem" > "$TARGET.new"
        mv "$TARGET.new" "$TARGET"
        /usr/local/sbin/hap-rotate-cert "$TARGET"
    done
    EOF
    sudo chmod +x /usr/local/sbin/hap-cert-deploy
  4. TLS-ALPN-01 vs HTTP-01. For HAProxy on port 443, the TLS-ALPN-01 challenge is the cleanest because it does not require port 80 to be open or a webroot. With certbot, use:

    sudo certbot certonly --standalone --preferred-challenges tls-alpn-01 -d example.com

    Certbot needs to listen on 443 briefly during the challenge. The pattern: stop HAProxy on 443, run certbot, restart HAProxy. Or use a HAProxy ACME helper that proxies the challenge.

    For HTTP-01 (the simpler default), put HAProxy on 80 with a route that serves /.well-known/acme-challenge/ from disk:

    frontend fe_http
        bind *:80
        acl is_acme path_beg /.well-known/acme-challenge/
        use_backend be_acme if is_acme
        http-request redirect scheme https code 301 unless is_acme
        default_backend be_app
    
    backend be_acme
        server acme 127.0.0.1:8080

    Run a tiny static file server on 127.0.0.1:8080 pointing at /var/www/acme-challenge.

  5. OCSP refresh in lockstep. Stapling requires the OCSP response file. Refresh it on the same cadence as the cert and push via the runtime API:

    CERT=/etc/haproxy/certs/example.com.pem
    ISSUER_URL=$(openssl x509 -in "$CERT" -noout -ocsp_uri)
    ISSUER=$(curl -s "$ISSUER_URL" || true) # we need the issuer cert
    openssl ocsp -issuer chain.pem -cert "$CERT" -url "$ISSUER_URL" -no_nonce -respout "$CERT.ocsp"
    echo "set ssl ocsp-response $(base64 -w0 $CERT.ocsp)" | sudo socat /run/haproxy/admin.sock -

    Refresh every 12-24 hours. The OCSP response is typically valid for 5-7 days but you want refresh well within that window.

  6. Crontab for the full cycle. Hourly check on certbot, daily OCSP refresh:

    17 */12 * * * root certbot renew --quiet
    42 6 * * * root /usr/local/sbin/refresh-ocsp-all

    certbot renew is a no-op unless a cert is within 30 days of expiry; safe to run on a fast schedule.

  7. Verify after rotation. A spot check from outside:

    echo | openssl s_client -connect example.com:443 -servername example.com -status 2>&1 | grep -E 'notBefore|notAfter|OCSP'
    curl -sI https://example.com/ | head -5

    The new notAfter should be 90 days out (Let’s Encrypt) or whatever the issuer set. OCSP status should read “successful”.

Common pitfalls

  • The PEM file on disk and the in-memory cert can drift. We have seen on-call engineers update the file, restart their cron, walk away, and miss that HAProxy was still serving the old cert because no set ssl cert ran. Always verify with s_client after rotation.
  • set ssl cert requires expose-fd listeners on the stats socket line. Without it, the command silently fails on some HAProxy versions or returns an unhelpful error.
  • The bundle must include both certificate and private key. Let’s Encrypt’s fullchain.pem and privkey.pem are separate; concatenate them. Missing the key fails at set.
  • certbot --standalone on port 443 collides with HAProxy. Either use --webroot with HTTP-01 (HAProxy serves /.well-known/acme-challenge/), TLS-ALPN-01 with HAProxy momentarily stopped, or a dedicated ACME daemon like Caddy or lego.
  • OCSP staples are tied to the cert; rotate the staple after rotating the cert. An old staple against a new cert is silently ignored by clients and you lose stapling without an obvious error.

Stack Harbor runs cert rotation as automated infrastructure for clients — Let’s Encrypt for public hostnames, internal PKI for service-to-service, monthly expiry report for any cert we did not auto-renew. The rotation script is idempotent and runs from the orchestration runner with audit logging. This is part of the day-2 operations we maintain as part of Managed Operations, so expiring certificates never wake anyone up.