To configure an SSL certificate in HAProxy, you combine your certificate and private key into a single PEM file and reference it in a frontend or listen block that uses the ssl option. This article explains exactly how to do that, including how to format the PEM, set up HTTP-to-HTTPS redirects, and troubleshoot common certificate errors.
What You Need Before Starting
HAProxy terminates TLS (the modern name for SSL) by acting as the server side of the encrypted connection. It presents your certificate to clients, decrypts incoming traffic, and forwards plain HTTP to your backend servers. To do this, you need:
- A valid certificate file (usually
yourdomain.crt) and its private key (yourdomain.key). - Any intermediate certificates from your certificate authority (CA) in a separate file, often called
ca-bundle.crt. - HAProxy 1.5 or newer (most current distributions ship 2.x, which works the same for basic setup).
You do not need to convert the certificate to a different format like PKCS#12. HAProxy only accepts PEM format, which is plain text with -----BEGIN CERTIFICATE----- and -----BEGIN PRIVATE KEY----- blocks.
Step 1: Combine Certificate and Key into a PEM File
HAProxy expects a single file containing the certificate chain and the private key. The order matters: first the server certificate, then any intermediate certificates, and finally the private key. Use the cat command to combine them:
cat yourdomain.crt ca-bundle.crt yourdomain.key > /etc/haproxy/yourdomain.pem chmod 600 /etc/haproxy/yourdomain.pem
The chmod 600 is important because the file contains a private key. If HAProxy runs as a different user (like haproxy), make sure that user can read the file. On Debian/Ubuntu, the HAProxy process typically runs as the haproxy user, so you might need to adjust ownership:
chown haproxy:haproxy /etc/haproxy/yourdomain.pem
If you only have the server certificate and key (no intermediates), the PEM file can just contain those two blocks. Most public CAs, however, issue intermediate certificates that must be included for browsers to trust your site.
Step 2: Configure the Frontend to Use SSL
In your HAProxy configuration file (usually /etc/haproxy/haproxy.cfg), modify the frontend section that listens on port 443. Here is a minimal example:
frontend https_frontend
bind *:443 ssl crt /etc/haproxy/yourdomain.pem
default_backend web_servers
The ssl keyword enables TLS on that bind, and crt points to your PEM file. You can also specify multiple certificates by placing them in a directory and using crt /etc/haproxy/certs/ – HAProxy will load all .pem files in that directory and choose the right one based on the Server Name Indication (SNI) sent by the client.
Step 3: Redirect HTTP to HTTPS
Most sites want to force all traffic to use HTTPS. Add a second frontend on port 80 that redirects to the HTTPS version:
frontend http_frontend
bind *:80
http-request redirect scheme https unless { ssl_fc }
This sends a 301 redirect for any request that arrives over plain HTTP. The unless { ssl_fc } condition is a safety check – it ensures that if the request already came in over TLS (which shouldn’t happen on port 80, but just in case), it won’t loop.
Step 4: Test and Reload
Before applying changes, check the configuration syntax:
haproxy -c -f /etc/haproxy/haproxy.cfg
If it returns Configuration file is valid, reload HAProxy gracefully:
systemctl reload haproxy
On older systems without systemd, use service haproxy reload. A reload does not drop existing connections – it spawns a new worker process and hands over new connections to it.
Common Problems and How to Fix Them
Here are the most frequent issues you’ll encounter:
- “No SSL certificate specified” error: This means the
crtpath is wrong or the file is empty. Check that the path exists and the file has content. - “Permission denied” on the PEM file: HAProxy runs as a non-root user, so it cannot read a file owned by root with mode 600. Change ownership to
haproxyor set mode to 644 (less secure but works). - Certificate chain incomplete: Browsers show a warning if the intermediate certificate is missing. Re-download the full chain from your CA and recombine the PEM.
- Key mismatch: If the private key does not match the certificate, HAProxy will fail to start with a “key does not match” message. Verify with
openssl x509 -noout -modulus -in yourdomain.crt | openssl md5andopenssl rsa -noout -modulus -in yourdomain.key | openssl md5– the hashes must match.
Renewing Certificates
When your certificate expires, you need to update the PEM file. The process is the same as the initial setup: get the new certificate and key, combine them, and reload HAProxy. If you use Let’s Encrypt with certbot, the certificate is typically at /etc/letsencrypt/live/yourdomain/fullchain.pem and the key at /etc/letsencrypt/live/yourdomain/privkey.pem. You can create the PEM with:
cat /etc/letsencrypt/live/yourdomain/fullchain.pem /etc/letsencrypt/live/yourdomain/privkey.pem > /etc/haproxy/yourdomain.pem systemctl reload haproxy
Many users automate this with a deploy hook in certbot that runs the combine and reload commands after each renewal.
Frequently Asked Questions
Can I use a PFX or PKCS#12 certificate with HAProxy?
No, HAProxy only reads PEM format. You can convert a PFX file using OpenSSL: openssl pkcs12 -in yourfile.pfx -out yourdomain.pem -nodes. This extracts the certificate and key into a single PEM file. Make sure the PFX contains the full chain, or add intermediates separately.
How do I configure multiple domains on one HAProxy?
Place all your PEM files in a directory and use bind *:443 ssl crt /etc/haproxy/certs/. HAProxy will automatically select the correct certificate based on the SNI hostname from the client. Each PEM file should be named something like example.com.pem – the filename does not matter, but the certificate’s Common Name or SAN must match the domain.
Why do I get a “SSL handshake failure” in the logs?
This usually means the client and server could not agree on a cipher suite or the certificate is invalid. Check that your HAProxy version supports the TLS version your clients use (modern browsers require TLS 1.2 or higher). You can add ssl-default-bind-options no-sslv3 to disable old protocols. Also verify that your certificate is not expired.
Does HAProxy support HTTP/2 over TLS?
Yes, since version 1.9. Add the alpn h2,http/1.1 option to your bind line: bind *:443 ssl crt /etc/haproxy/yourdomain.pem alpn h2,http/1.1. This advertises HTTP/2 support during the TLS handshake.
Can I use a wildcard certificate?
Yes, a wildcard certificate for *.example.com works fine. Just put it in a PEM file and reference it as usual. It will match any subdomain, but not the bare domain (example.com) unless the certificate also includes that as a SAN.
What is the difference between crt and crt-list?
crt specifies a single PEM file or a directory. crt-list is a text file that lists multiple PEM files with optional SNI filters and cipher suites. Use crt-list when you need per-certificate settings, like different OCSP stapling or algorithms.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Handwriting vs Typing for Digital Note Taking2,400 words
- Can a Ford Sedan Beat a Mustang? Facts
- Toyota RAV4 Most Expensive Trim: Worth It?
- Ryobi Hybrid Tools: Smart Buy? Here’s Why
- M4 vs M3: Apple Silicon Compared
- Red Flags: Avoid a Social Media Platform?