Sub topic 10
TLS termination and re-encryption
This chapter assumes you know what HTTP headers are, what TLS provides (encryption, server authentication, integrity), and what a reverse proxy does. It covers where a reverse proxy ends the TLS session and what that means for the backends.
What TLS termination is
TLS termination means the reverse proxy performs the TLS handshake with the client, decrypts the request, and then forwards it to a backend. The backend never talks TLS with the client.
Termination has practical benefits:
- Certificates and private keys live in one place instead of on every backend.
- Backends spend no CPU on handshakes and encryption.
- The proxy can read the decrypted request. This is required for routing by path, caching, and adding headers.
Terminating at the proxy
The proxy listens on port 443 with a certificate and key, then forwards plain HTTP to the backends.
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;
}
}This block assumes an upstream app_servers block like the one from the load balancing chapter. The $scheme variable is https here, because the client connected over TLS. The proxy passes that value on as X-Forwarded-Proto.
Why the backend needs X-Forwarded-Proto
The backend receives plain HTTP, so on its own it believes every request is insecure. That causes real bugs:
- The app redirects to
http://URLs after login. - Cookies marked
Secureare not set, or links in pages point tohttp://. - An app that forces HTTPS may redirect forever, because the proxy always connects over HTTP.
The fix is to have the app read X-Forwarded-Proto and treat the request as HTTPS when it says so. Trust this header only from your own proxy. A client can send its own copy, so the proxy should overwrite it, as the proxy_set_header line does.
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 three TLS patterns
| Pattern | Client to proxy | Proxy to backend | Proxy can read traffic |
|---|---|---|---|
| Termination | TLS | Plain HTTP | Yes |
| Re-encryption | TLS | New TLS session | Yes |
| Passthrough | TLS, ends at backend | Same TLS stream | No |
Re-encryption is termination followed by a second TLS connection to the backend. Use it when the network between proxy and backend is not trusted, or when a compliance rule requires encryption inside the network. In nginx, you change the proxy_pass scheme to https:
location / {
proxy_pass https://app_servers;
proxy_ssl_verify on;
proxy_ssl_trusted_certificate /etc/nginx/certs/internal-ca.crt;
}Setting proxy_ssl_verify on matters. Without it, nginx does not check the backend certificate, so the second connection is encrypted but could be answered by an impostor.
Passthrough does not decrypt anything. The proxy works at the TCP level, so it cannot route by path, cache, or add headers. Choose it when the backend must see the client certificate itself or when the proxy must never hold the traffic in plaintext.
Trade-offs
- Plain HTTP behind the proxy exposes traffic to anyone who can observe that network segment. Keep that segment private, or re-encrypt.
- The proxy holds the private key, so a compromised proxy exposes all traffic. Restrict access to the key file.
- Re-encryption adds CPU cost and a second set of certificates to renew.
- Certificate expiry at the proxy takes down every site it serves. Monitor expiry dates.
Exercise
An application behind an nginx proxy that terminates TLS keeps redirecting users to http:// URLs after login. The proxy sends plain HTTP to the backend. What is the cause, and what two changes fix it?
Show answer
The backend sees a plain HTTP connection and assumes the client used HTTP. Fix it by having the proxy send proxy_set_header X-Forwarded-Proto $scheme; and configuring the application to read that header (from the trusted proxy only) when deciding whether the request was secure.
Exercise
Your security policy requires traffic to be encrypted between the proxy and the backends, and you still want the proxy to route by URL path. Which pattern do you choose, and which nginx setting makes sure the backend is really who it claims to be?
Show answer
Choose re-encryption: terminate TLS at the proxy so it can read the path, then open a new TLS connection to the backend with proxy_pass https://.... Set proxy_ssl_verify on with a trusted CA certificate so the proxy verifies the backend certificate.