Sub topic 3
TLS Termination and Re-encryption
Assume you know HTTP methods and headers, DNS, ports, and the basic purpose of TLS. A reverse proxy can accept encrypted client traffic, decrypt it, and send a request to a backend. This is TLS termination. The backend connection may use plain HTTP or a second TLS connection called re-encryption.
sequenceDiagram
participant C as Client
participant P as Reverse proxy
participant A as Backend
C->>P: HTTPS request
P->>P: Decrypt TLS
P->>A: HTTPS request
A-->>P: HTTPS response
P-->>C: HTTPS responseRe-encryption protects traffic inside the network as well as traffic from clients to the proxy. It also lets the proxy inspect HTTP for routing, authentication, or caching. The proxy must trust the backend certificate, and the backend must receive the original scheme through X-Forwarded-Proto.
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 https://app_servers;
proxy_ssl_server_name on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Caching responses at the proxy
A proxy can cache responses and serve repeated requests without contacting the backend. Cache only responses that are safe to share. Public static files are common candidates. Do not cache private responses unless the cache key includes the required identity and authorization context.
From reverse proxy to API gateway
An API gateway is a reverse proxy with API-focused policies. It can route by path, authenticate tokens, enforce rate limits, transform requests, and record metrics. Use a gateway when these policies must be centralized across services. Avoid adding gateway features that are not needed, because each policy adds latency and operational complexity.
Keeping the proxy tier available and scalable
Run multiple proxy instances across failure domains. Put a health-aware load balancer or DNS policy in front of them. Give each proxy enough capacity for normal traffic and failover traffic. Backends also need health checks and connection limits. A single proxy instance can become a single point of failure even when every backend is replicated.
Trade-offs and common pitfalls
- TLS termination reduces backend CPU work but increases the proxy's security responsibility.
- Forwarded headers are client-controlled unless the proxy removes and rewrites them.
- Incorrect
X-Forwarded-Forhandling hides the client address or enables spoofing. - Caching can expose private data or serve stale content.
- Re-encryption improves isolation but adds certificate management and handshake cost.
- Proxy timeouts, retries, and buffering can change application behavior.
Exercise
When should a proxy use re-encryption instead of plain HTTP to a backend?
Show answer
Use re-encryption when traffic must remain confidential or authenticated across the internal network, especially across hosts, zones, or networks you do not fully trust.