Reverse proxies and edge security
TLS termination, compression and caching, web application firewalls, and rate limiting.
What you will learn
- List what a reverse proxy does in front of your servers.
- Say what a web application firewall blocks.
- Compare the token bucket and leaky bucket rate limiters.
Reverse Proxy Core Functions
A reverse proxy is a web server that sits in front of internal services and gives the public one unified interface: it forwards each request to a server that can fulfill it and returns that server's response. Useful even in front of a single server, it provides:
- Security: Hides information about backend servers, can block IP addresses, and can limit connections per client.
- Scalability and flexibility: Clients see only the proxy's address, so you can add, remove or reconfigure servers behind it.
- TLS termination: Decrypts incoming requests and encrypts responses, so backend servers do not have to do this potentially expensive work. Whether traffic is re-encrypted on the way to the backends is a separate security decision.
- Compression of responses, caching of responses it has already seen, and serving static content directly.
NGINX and HAProxy can act as both a layer 7 reverse proxy and a load balancer.
Web Application Firewall (WAF)
A web application firewall applies a set of rules to HTTP traffic. Generally these rules cover common attacks such as:
- SQL injection (SQLi): e.g.,
' OR 1=1-- - Cross-site scripting (XSS): e.g.,
<script>alert(1)</script>
Rate Limiting Algorithms
To protect backends from brute force and traffic surges, reverse proxies implement rate limiters:
Token bucket algorithm
A bucket holds up to tokens (the burst), and tokens are added at a steady rate of per second (the rate). Each request consumes token.
- Allowed: If tokens , consume token and forward the request.
- Rejected: If tokens , respond with
HTTP 429 Too Many Requests, optionally with aRetry-Afterheader telling the client how long to wait. - Advantage: Allows bursts up to requests while holding the long-run average to . AWS API Gateway, for example, throttles with a token bucket configured exactly this way.
Leaky bucket algorithm
Requests enter a queue and leave it at a constant rate ; when the queue is full, new requests are rejected. This smooths bursty traffic into a steady stream. NGINX's request-rate limiting uses the leaky bucket method (and by default rejects excess requests with 503 rather than 429).
Token bucket rate limiting and a firewall
GET /api/v1/feedHTTP 200Allowed (1 token consumed)GET /api/v1/feedHTTP 200Allowed (1 token consumed)Token bucket algorithm: Tokens refill at a fixed rate (r per second) up to the bucket's capacity (B). Each incoming request consumes 1 token. When the bucket is empty, requests are rejected with 429 Too Many Requests, optionally with a Retry-After header, protecting the services behind the proxy from sudden traffic spikes.
- Untrusted Public Internet: 1 × Clients & Users
- WAF & Reverse Proxy: 2 × Application Load Balancer
- Private Backend Microservices: 6 × App Compute Server
- Untrusted Public Internet sends requests to WAF & Reverse Proxy
- WAF & Reverse Proxy sends requests to Private Backend Microservices
Key takeaways
- A reverse proxy gives the public one address, hides and shields the backends, terminates TLS, compresses, caches and serves static files; it helps even in front of one server.
- A WAF applies rules to HTTP traffic against attacks such as SQL injection and cross-site scripting.
- A token bucket allows bursts up to and holds the average to ; refused requests get 429 Too Many Requests, optionally with Retry-After.
- A leaky bucket queues requests and lets them out at a constant rate, smoothing bursts into a steady stream.
Sources
- System Design Primer: Reverse proxy (web server)
- RFC 6585: Additional HTTP Status Codes (429 Too Many Requests)
- Amazon API Gateway: Throttle requests (token bucket)
- NGINX: ngx_http_limit_req_module (leaky bucket)
- OWASP: Web Application Firewall
Adapted from The System Design Primer by Donne Martin, licensed under CC BY 4.0. Changes were made.