Sub topic 7
TLS termination and passthrough
This chapter assumes you know how an HTTPS connection is set up (TCP handshake, then TLS handshake), what a certificate and a certificate authority are, and the Layer 4 versus Layer 7 distinction from the earlier chapter. You should also be comfortable editing NGINX or HAProxy configuration files.
A load balancer that sits in front of HTTPS traffic has to decide who ends the encrypted session. If the load balancer ends it, that is TLS termination. If it forwards the encrypted bytes untouched, that is TLS passthrough. The choice decides which load balancer features you can use, where your certificates live, and which network segments carry plaintext.
The three models
| Model | Client to LB | LB to backend | LB can read HTTP? | Certificate lives on |
|---|---|---|---|---|
| Termination (offload) | TLS | Plain HTTP | Yes | Load balancer |
| Re-encryption (bridging) | TLS | New TLS session | Yes | Load balancer and backends |
| Passthrough | TLS | Same TLS session | No | Backends |
In termination, the load balancer completes the TLS handshake with the client, decrypts the request, and forwards it to a backend. In re-encryption, it decrypts, inspects or modifies the request, then opens a second, separate TLS connection to the backend. In passthrough, the load balancer never holds a private key. It copies TCP bytes in both directions, and the TLS session runs end to end between client and backend.
Why terminate at the load balancer
Termination is the default in most deployments, for these reasons.
- Layer 7 features work. Routing by Host header or URL path, header rewriting, HTTP-aware health checks, cookie-based persistence, caching and compression all need to read the decrypted request. Without decryption, the load balancer only sees IP addresses, ports and a few unencrypted handshake fields.
- Certificates are managed in one place. You renew and rotate certificates on a few load balancers instead of on every backend. With automation such as ACME clients, this is one renewal job instead of many.
- Backends do less work. The expensive part of TLS is the handshake, which uses public-key cryptography. Terminating once at the edge keeps that CPU cost off the application servers. On modern CPUs with AES instructions, bulk encryption is cheap, so the handshake is the main saving. It matters most for many short-lived connections.
- Session resumption is centralized. Clients that reconnect can resume a previous session with a ticket or session ID, which skips most of the handshake. This works well when one tier holds the ticket keys.
- Policy is consistent. You set the minimum TLS version, cipher suites and HSTS header once.
Costs and risks of termination
- Plaintext inside your network. Traffic between the load balancer and the backends is unencrypted unless you re-encrypt. If that segment is a shared or untrusted network, anyone who can sniff it sees everything. Compliance regimes such as PCI DSS often require encryption in transit across internal hops too.
- The load balancer holds your private keys. Compromising it exposes the keys for every site it serves. Restrict file permissions, consider a hardware security module or a cloud key service, and limit who can log in.
- The backend loses client details. The backend sees a connection from the load balancer's IP over plain HTTP. It cannot tell whether the original request was HTTPS or what the client IP was. You must pass these on in headers such as
X-Forwarded-ForandX-Forwarded-Proto, and the application must trust them only from the load balancer. - The load balancer is a bigger target. It does all the TLS work, so it needs enough CPU for handshakes. A burst of new connections can saturate it before the backends notice.
NGINX: termination with re-encryption
This configuration terminates TLS for clients and opens a new, verified TLS connection to each backend. The http2 on; directive needs NGINX 1.25.1 or later. On older versions, write listen 443 ssl http2; instead.
upstream app_tls {
least_conn;
server 10.0.0.11:8443 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8443 max_fails=3 fail_timeout=30s;
}
server {
listen 443 ssl;
http2 on;
server_name app.example.com;
ssl_certificate /etc/nginx/certs/app.example.com.fullchain.pem;
ssl_certificate_key /etc/nginx/certs/app.example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
location / {
proxy_pass https://app_tls;
proxy_ssl_server_name on;
proxy_ssl_name app.internal.example.com;
proxy_ssl_verify on;
proxy_ssl_trusted_certificate /etc/nginx/certs/internal-ca.pem;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Key points:
ssl_session_cache shared:SSL:10mstores resumable sessions in memory shared by all worker processes. Without it, resumption only works within a single worker.proxy_ssl_verify onmakes NGINX check the backend's certificate againstinternal-ca.pem. By default NGINX does not verify upstream certificates, so re-encryption without this line protects against passive sniffing but not against an impostor backend.proxy_ssl_namesets the name NGINX expects in the backend certificate and sends as SNI whenproxy_ssl_server_name onis set.- To terminate without re-encryption, change
proxy_passtohttp://and delete theproxy_ssl_*lines.
How passthrough works
In passthrough the load balancer works at Layer 4. It accepts a TCP connection, picks a backend, and relays bytes. It cannot read cookies, paths or headers, because they are encrypted.
It can still read one thing: the TLS ClientHello, the first message of the handshake, is sent in the clear. It includes the SNI (Server Name Indication) extension, which names the host the client wants, for example api.example.com. A passthrough load balancer can read SNI and use it to choose a backend pool without decrypting anything. This lets one IP address serve several TLS sites with different certificates.
Limits to remember:
- SNI is visible only in the ClientHello. With TLS 1.3 and Encrypted Client Hello (ECH), the name can be hidden, and an SNI-based router then has nothing to route on.
- Persistence can only use the client IP or the TLS session, not cookies.
- Health checks are limited to TCP connects or TLS handshakes, not HTTP status checks, unless the load balancer is allowed to do its own TLS to the backend.
- Backends see the load balancer's IP as the source. To preserve the client address, use the PROXY protocol, which prepends a small header carrying the original source address. The backend must be configured to expect it, or it will treat the header as garbage and fail the handshake.
NGINX stream passthrough
The stream block goes at the top level of nginx.conf, next to http. It requires the ngx_stream_ssl_preread_module, which is included in most distribution packages.
stream {
map $ssl_preread_server_name $pool {
api.example.com tls_api;
default tls_web;
}
upstream tls_api {
server 10.0.0.21:8443;
server 10.0.0.22:8443;
}
upstream tls_web {
server 10.0.0.11:8443;
server 10.0.0.12:8443;
}
server {
listen 443;
ssl_preread on;
proxy_pass $pool;
proxy_connect_timeout 5s;
}
}ssl_preread on makes NGINX read the ClientHello without terminating TLS and expose the SNI as $ssl_preread_server_name. The map turns that name into an upstream group name, and proxy_pass $pool uses it. Clients that send no SNI fall through to default.
HAProxy TCP passthrough
defaults
mode tcp
timeout connect 5s
timeout client 1m
timeout server 1m
frontend tls_in
bind *:443
tcp-request inspect-delay 5s
tcp-request content accept if { req.ssl_hello_type 1 }
use_backend tls_api if { req.ssl_sni -i api.example.com }
default_backend tls_web
backend tls_api
balance leastconn
server api1 10.0.0.21:8443 check
server api2 10.0.0.22:8443 check
backend tls_web
balance roundrobin
server web1 10.0.0.11:8443 check
server web2 10.0.0.12:8443 checkThe inspect-delay gives HAProxy time to receive the ClientHello before it evaluates the SNI rule. Without it, the rule may run before any bytes arrive and always miss. The check keyword here is a plain TCP connect check. To send the client address to backends, add send-proxy-v2 to each server line, and enable PROXY protocol on the backend.
Choosing a model
Use termination or re-encryption when you need any of these:
- Path, host-header or cookie routing
- HTTP health checks that look at status codes
- WAF rules, rate limiting by URL, caching or compression
- Central certificate management
Choose re-encryption over plain termination when the internal network is not fully trusted or compliance requires encryption in transit everywhere.
Use passthrough when:
- Policy says the load balancer must never hold the private key or see plaintext
- Clients authenticate with certificates (mutual TLS) and the backend must see the client certificate itself
- The protocol is not HTTP, for example a database or message broker over TLS
- You want the simplest possible Layer 4 forwarding for maximum throughput
Mutual TLS deserves a note. If you terminate TLS at the load balancer, the backend cannot verify the client certificate. The load balancer must verify it and pass the result along, for example in a header such as X-Client-Cert-DN. The backend then trusts that header, which is safe only if the load balancer strips any client-supplied copy first.
Common errors
- Incomplete certificate chain. Browsers may work (they cache intermediates), but
curland API clients fail. Serve the full chain: leaf certificate first, then intermediates. - Redirect loops. The application sees plain HTTP from the load balancer and redirects to HTTPS forever. Fix it by having the application honor
X-Forwarded-Proto. - Mixed content. The app generates
http://links because it believes it is serving HTTP. The sameX-Forwarded-Protofix usually applies. - Wrong SNI to the backend. On re-encryption, the backend may host several names and return a default certificate. Set the SNI name explicitly, as
proxy_ssl_namedoes above. - SNI rules never match in HAProxy. This is usually a missing
inspect-delay. - Handshake failures after enabling PROXY protocol. One side sends the header and the other does not expect it. Both must agree.
- Expired certificates. Passthrough moves the problem to every backend. Monitor expiry on each one, not just on the load balancer.
You can test each model from a client machine:
openssl s_client -connect lb.example.com:443 -servername app.example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -datesWith termination, this prints the load balancer's certificate. With passthrough, it prints the backend's certificate. Changing -servername shows whether SNI routing sends you to a different pool.
Exercise
Your load balancer must route /api/* requests to one pool and everything else to another pool, and your security team requires that traffic between the load balancer and backends is encrypted. Which model do you choose, and which NGINX setting makes sure a rogue backend cannot impersonate a real one?
Show answer
Use TLS termination with re-encryption. Path-based routing needs the decrypted request, so passthrough is not possible, and encrypting the second hop rules out plain termination. Set proxy_ssl_verify on together with proxy_ssl_trusted_certificate pointing at the CA that signed the backend certificates. NGINX does not verify upstream certificates by default.
Exercise
A passthrough HAProxy frontend routes by req.ssl_sni, but every request goes to the default backend even though clients send the correct hostname. What is the most likely missing configuration, and why?
Show answer
The frontend lacks tcp-request inspect-delay (with tcp-request content accept if { req.ssl_hello_type 1 }). Without it, HAProxy evaluates the SNI rule before the ClientHello has arrived, so no name is available and the rule does not match. The inspect delay makes HAProxy wait for the handshake bytes first.