The correction is integrated into the TP (See the green boxes)
What you will learn in this TP
- Terminate TLS and force HTTPS
- Route with ACLs
- Reload without downtime
HTTPS and ACLs #
What you will learn in this section
- Terminate TLS and force HTTPS
- Route with ACLs
- Reload without downtime
Context. A site must move to HTTPS, redirect legacy HTTP and separate API traffic from the rest, while restricting access to an admin interface. HAProxy terminates TLS and applies ACLs to route and filter.
Prerequisites: Previous lab, OpenSSL · Estimated time: 45 min
A self-signed certificate and routing rules.1. Generate a certificate
Why? HAProxy expects a PEM file with certificate and key: generating it yourself makes the lab self-contained, with no external authority.
Create the PEM file HAProxy expects.See the correction
mkdir -p certs openssl req -x509 -newkey rsa:2048 -nodes -days 30 -keyout certs/k.key -out certs/c.crt -subj '/CN=app.localhost' cat certs/c.crt certs/k.key > certs/app.pem2. HTTPS frontend and redirect
Why? TLS termination offloads encryption from servers and centralises certificates; redirect and HSTS enforce secure use.
Listen on 443, redirect 80 to HTTPS and add HSTS.See the correction
frontend fe bind *:80 bind *:443 ssl crt /usr/local/etc/haproxy/certs/app.pem alpn h2,http/1.1 http-request redirect scheme https unless { ssl_fc } http-response set-header Strict-Transport-Security "max-age=31536000" acl is_api path_beg /api acl is_admin hdr(host) -i admin.localhost acl lan src 127.0.0.1 172.16.0.0/12 http-request deny if is_admin !lan use_backend api if is_api default_backend web backend api server api1 api:80 check backend web server web1 web:80 check3. Test the routing
Why? Testing each path and the certificate checks the ACLs route correctly and the client sees what you expect.
Call/and/apiand check the certificate.See the correction
curl -sk https://localhost/ | head -3 curl -sk https://localhost/api | head -3 curl -sI http://localhost | grep -i location openssl s_client -connect localhost:443 -servername app.localhost </dev/null 2>/dev/null | openssl x509 -noout -subject -dates4. Validate and reload without downtime
Why? Validating before reloading and using a reload signal avoids interrupting ongoing connections.
Change a rule then reload cleanly.See the correction
docker exec lb haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg docker kill -s HUP lb # rechargement sans coupure
Difficulty level : ●●●○○ (3/5)
My progress
🧪 Launch the lab environment ↗
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Mastering ACLs in HAProxy
A complete guide to HAProxy ACLs: learn how to configure smart routing, IP-based access control, ...
HAProxy and SSL: TLS termination, SSL passthrough and hardening
HAProxy and SSL/TLS: SSL termination, SSL passthrough in TCP mode with SNI routing, re-encryption...
Mastering ACLs in HAProxy: the complete guide
A complete guide to HAProxy ACLs: learn how to configure smart routing, IP-based access control, ...
HAProxy: a detailed guide to configuration directives
A complete exploration of HAProxy's essential directives: from bind to http-check, learn how to c...
HAProxy as a Kubernetes Ingress Controller: the production guide
Learn how to use HAProxy as an Ingress Controller in Kubernetes: direct-to-pod architecture, TLS ...
The ACME protocol: ACME server, http-01 and dns-01 challenges, TLS automation
Understand the ACME protocol: the role of the ACME server (Let's Encrypt, ZeroSSL, step-ca), the ...
Managing SSL/TLS certificates with Let's Encrypt and ACME
Learn how to automate the generation and renewal of your SSL/TLS certificates with Let's Encrypt....
Health checks and high availability
How HAProxy detects failures and handles automatic failover to healthy servers.
HAProxy in Kubernetes: the high-performance Ingress Controller
Discover in depth how HAProxy integrates with Kubernetes: direct-to-pod architecture, hot update ...