Sub topic 7
TLS termination and passthrough
This chapter assumes you know how Layer 4 and Layer 7 balancing differ, and that you have seen a basic NGINX or HAProxy pool. It covers where the TLS connection ends: at the load balancer or at the backend.
Two ways to handle encrypted traffic
HTTPS traffic is encrypted with TLS. A load balancer has two choices.
- TLS termination (also called TLS offload): the load balancer holds the certificate and private key, decrypts the traffic, and forwards it to backends.
- TLS passthrough: the load balancer forwards the encrypted bytes untouched. Each backend holds the certificate and completes the handshake itself.
The choice follows from the layer you learned earlier. To read an HTTP path, header, or cookie, the balancer must decrypt the request. A balancer that only forwards TCP connections cannot see any of that.
TLS termination
With termination, the load balancer completes the handshake and reads the plain HTTP request. This gives you:
- Layer 7 features such as path routing, header rewriting, cookie persistence, and HTTP health checks.
- One place to manage certificates, instead of one copy per backend.
- Less CPU work on backends, because they skip the handshake and encryption.
The HAProxy example from the plan terminates TLS in the frontend:
frontend web
mode http
bind *:443 ssl crt /etc/haproxy/certs/site.pem
default_backend app
backend app
mode http
balance roundrobin
option httpchk GET /healthz
http-check expect status 200
cookie SRV insert indirect nocache
server app1 10.0.0.11:8080 check cookie app1
server app2 10.0.0.12:8080 check cookie app2The ssl crt option on bind loads the certificate and key. Traffic to 10.0.0.11:8080 is plain HTTP. The cookie persistence in this configuration only works because the balancer can read and set HTTP cookies after decryption.
Keeping the client's details
After termination, the backend sees the balancer's IP address and a plain HTTP request. It no longer knows the client's address or whether the original request used HTTPS. Pass this information in headers. The NGINX example uses X-Forwarded-For for the client address. Many setups also add X-Forwarded-Proto so the application can build correct https:// links and redirects.
Re-encryption
Plain HTTP between the balancer and backends is acceptable only on a network you trust. If policy or compliance requires encryption inside the network, use re-encryption (also called TLS bridging). The balancer decrypts the client's traffic, then opens a new TLS connection to the backend. In HAProxy this means adding ssl to the server lines, usually with verify required ca-file ... so the balancer checks the backend's certificate. Re-encryption costs CPU on both sides, but you keep Layer 7 features.
TLS passthrough
With passthrough, the balancer works at Layer 4. It cannot read the request, so it chooses a backend from connection-level data: source IP, port, and sometimes the SNI (Server Name Indication) field. SNI is the hostname the client sends, unencrypted, at the start of the handshake. Some balancers use it to route different hostnames to different pools without decrypting anything.
Passthrough fits these cases:
- The client must verify the backend's own certificate, or mutual TLS (client certificates) must end at the backend.
- Policy forbids private keys on a shared balancer.
- The protocol is not HTTP, and you only need to spread TCP connections.
The costs are concrete. You lose cookie persistence, header changes, and HTTP health checks. You also cannot add X-Forwarded-For, so the backend sees only the balancer's address unless you use the PROXY protocol. Every backend needs the certificate and key, which means more copies to renew and protect.
Comparing the two
| Aspect | Termination | Passthrough |
|---|---|---|
| Layer | 7 | 4 |
| Certificate location | Load balancer | Every backend |
| Can inspect HTTP | Yes | No |
| Backend CPU cost | Low | Higher |
| Client IP visible to backend | Via headers | Needs PROXY protocol |
Common errors
- Redirect loops. The app sees plain HTTP, decides the request is insecure, and redirects to HTTPS again. Configure the app to trust
X-Forwarded-Protofrom the balancer. - Expired certificate on one backend. In passthrough, one stale copy fails only some requests, which is hard to spot. Automate renewal.
- Wrong client IP in logs. Make sure the app reads the forwarded header, and accepts it only from the balancer.
Exercise
A team needs cookie-based session persistence and path-based routing, and their compliance policy requires that traffic inside the data center is also encrypted. Which TLS approach should they use, and why?
Show answer
TLS termination with re-encryption. Cookie persistence and path routing need the balancer to read HTTP, so it must decrypt the request (termination). Because internal traffic must be encrypted too, the balancer opens a new TLS connection to each backend. Passthrough would satisfy the encryption rule but would block both Layer 7 features.
Exercise
After you move TLS termination onto the load balancer, every request in the application logs shows the balancer's IP address. What is the cause, and what is the fix?
Show answer
The backend now receives a new connection from the balancer, so the source address is the balancer's. Have the balancer add X-Forwarded-For (as the NGINX example does with $proxy_add_x_forwarded_for), and configure the application to read it, trusting it only from the balancer.