Sub topic 10
TLS Termination and Re-encryption
This chapter assumes you know how HTTP requests and headers work, what TLS provides (confidentiality, integrity, server identity), and the basics of ports and DNS. It covers where a reverse proxy ends the TLS session, what that means for the traffic behind it, and the mistakes that usually follow.
What TLS termination means
TLS termination means the reverse proxy completes the TLS handshake with the client, decrypts the request, and then handles it as ordinary HTTP. The proxy holds the certificate and private key for your domain. The backends never see the client's TLS session.
Terminating at the proxy has these effects:
- Certificates and keys live in one place instead of on every backend.
- Backends skip the CPU cost of handshakes and encryption.
- The proxy can read the plain HTTP request. That makes routing by path or header, caching, and logging possible. None of these work on encrypted bytes.
- The hop from proxy to backend is only as safe as the network it crosses.
The last point is the main trade-off, and the rest of the chapter returns to it.
A minimal terminating configuration
The nginx server block below listens on port 443 with TLS enabled. It forwards plain HTTP to the app_servers upstream defined in the earlier load balancing chapter.
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/certs/example.com.crt;
ssl_certificate_key /etc/nginx/certs/example.com.key;
location / {
proxy_pass http://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Three details matter here:
listen 443 sslturns on TLS for that port.ssl_certificateis the certificate, and normally includes any intermediate certificates the client needs to build a chain to a trusted root.ssl_certificate_keyis the matching private key. Protect that file with strict permissions.proxy_pass http://...uses thehttpscheme, so the proxy-to-backend hop is unencrypted.
Telling the backend what the client used
After termination, every request reaches the backend over plain HTTP. The backend sees http even though the client used https. Applications that build absolute URLs, set Secure cookies, or redirect to HTTPS can then behave wrongly. A common symptom is an endless redirect loop: the client requests HTTPS, the proxy forwards HTTP, the application answers "please use HTTPS", and the cycle repeats.
The proxy fixes this by passing the original scheme in a header. X-Forwarded-Proto: https is the common form. The standardized Forwarded header carries the same information:
X-Forwarded-For: 203.0.113.7, 10.0.0.5
X-Forwarded-Proto: https
Forwarded: for=203.0.113.7;proto=https;host=example.comThe application, or its framework, must be configured to read these headers. Most frameworks have a setting for this, often called "trusted proxies".
Re-encryption: TLS on the second hop
Plain HTTP between proxy and backend is acceptable only when that network is trusted. Many designs do not meet that bar. Examples are a proxy in a different network segment, traffic crossing a shared or cloud network, and compliance rules that require encryption in transit everywhere.
Re-encryption (also called TLS bridging or end-to-end TLS) means the proxy decrypts the client's connection, inspects or routes the request, and then opens a new TLS connection to the backend. There are two separate TLS sessions with two separate handshakes.
location / {
proxy_pass https://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_verify on;
proxy_ssl_trusted_certificate /etc/nginx/certs/internal-ca.crt;
proxy_ssl_server_name on;
}Here proxy_pass https://... makes nginx speak TLS to the backend. The other directives matter just as much:
proxy_ssl_verify onmakes nginx check the backend's certificate. In nginx this is off by default. Without verification, the encryption protects against passive eavesdropping but not against an attacker who impersonates a backend.proxy_ssl_trusted_certificatenames the certificate authority (CA) file used to validate the backend certificate. Internal backends often use certificates from a private CA.proxy_ssl_server_name onsends the server name (SNI) in the handshake, which backends that host several names need.
Note that nginx does not check that the certificate's name matches the backend unless you also set proxy_ssl_name or enable name verification where your version supports it. Check your version's documentation, because this is a frequent gap.
Three deployment patterns
| Pattern | Client to proxy | Proxy to backend | Proxy can read traffic | Typical use |
|---|---|---|---|---|
| Termination | TLS | Plain HTTP | Yes | Trusted private network, simplest operations |
| Re-encryption | TLS | TLS (new session) | Yes | Untrusted or regulated internal networks |
| Passthrough | TLS | The same TLS session, forwarded as bytes | No | Backend must hold the key, or end-to-end client certificates are needed |
Passthrough works at the TCP level. The proxy forwards encrypted bytes and cannot inspect them. It can still choose a backend using the SNI name sent in the clear at the start of the handshake. It cannot route by URL path, cache, or add headers such as X-Forwarded-For. In nginx this uses the stream module rather than the http module.
Certificates, performance and operations
Managing certificates is the main operational task at the termination point.
- Renewal. Certificates expire. Automate issuance and renewal, for example with an ACME client, and make the proxy reload its configuration after a renewal. An expired certificate on the proxy takes down every site behind it.
- Multiple names. One proxy often serves several domains. SNI lets the proxy select the right certificate from the name the client sends in the handshake.
- Session reuse. A full handshake costs round trips and CPU. TLS session resumption lets returning clients skip most of that work. Keep-alive connections between client and proxy have a similar effect.
- Upstream keep-alive. With re-encryption, opening a new TLS connection per request is expensive. Reuse backend connections with an
upstreamkeep-alive pool so one handshake serves many requests. - Protocol versions. Allow modern TLS versions only. Disable obsolete protocol versions and weak cipher suites, and follow a current, maintained configuration guide.
- HTTP/2 and HTTP/3. The proxy can speak HTTP/2 to clients and HTTP/1.1 to backends. Clients get the newer protocol's benefits without changing the application.
Common pitfalls
- Redirect loops. The application does not know the client used HTTPS. Pass
X-Forwarded-Protoand configure the framework to honor it. - Wrong client address. Backends log the proxy's IP unless they read
X-Forwarded-FororForwarded. Rate limits and audit logs then apply to the proxy as a whole instead of to each client. - Unverified backend certificates. Re-encryption without verification gives a false sense of security.
- Broken certificate chain. If the proxy omits an intermediate certificate, some clients fail while others, which have cached the intermediate, succeed. Test with a clean client.
- Plaintext secrets on the internal hop. Cookies, tokens and passwords travel unencrypted from proxy to backend under plain termination. Anyone who can capture traffic on that segment can read them.
- Private key exposure. The proxy holds the private key for every domain it serves. A compromised proxy exposes all of them. Restrict file permissions, limit who can log in, and consider a hardware or managed key store for high-value keys.
- Mutual TLS lost at the proxy. If backends require client certificates, termination removes the client's certificate from the connection. The proxy must either pass the certificate details in a header the backend trusts, or use passthrough.
When to choose each pattern
Choose termination alone when the proxy and backends share a network you control and trust, and you want the lowest operating cost. Choose re-encryption when the internal network is shared or regulated, or when you want defense in depth. Choose passthrough only when the backend must handle TLS itself, since you lose caching, header rewriting and path routing.
Exercise
A site terminates TLS at an nginx proxy and forwards plain HTTP to its backends. Users report an endless redirect loop on the login page. The application redirects any request it sees as http to https. What is the likely cause, and what two changes fix it?
Show answer
The backend receives plain HTTP from the proxy, so it thinks every request is insecure and keeps redirecting. The proxy should send the original scheme with proxy_set_header X-Forwarded-Proto $scheme;, and the application must be configured to trust that header from the proxy's address and use it to decide whether the request was secure.
Exercise
You switch proxy_pass from http://app_servers to https://app_servers to encrypt the internal hop, but add no other directives. Name one risk that remains, and the directives that address it.
Show answer
nginx does not verify the backend certificate by default, so an attacker who can impersonate a backend on the internal network would not be detected. Add proxy_ssl_verify on; and proxy_ssl_trusted_certificate pointing to the CA that signed the backend certificates. Also enable proxy_ssl_server_name on; if backends rely on SNI.
Exercise
Why can a proxy using TLS passthrough not cache responses or add an X-Forwarded-For header?
Show answer
In passthrough the proxy forwards the encrypted bytes without decrypting them, so it cannot read or change the HTTP request or response. Caching and header changes both require reading and modifying plain HTTP.