Lesson 3.4• 5 min read

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 BBwhat its symbols mean tokens (the burst), and tokens are added at a steady rate of rrwhat its symbols mean per second (the rate). Each request consumes 11 token.

  • Allowed: If tokens ≥1\ge 1, consume 11 token and forward the request.
  • Rejected: If tokens <1< 1, respond with HTTP 429 Too Many Requests, optionally with a Retry-After header telling the client how long to wait.
  • Advantage: Allows bursts up to BB requests while holding the long-run average to rr. 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 rr; 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

Tokens in bucket
10 / 10
Refill rate
+2 tokens/sec
Burst tolerance
Max 10 requests
Edge rate limiter & WAF decision log:
203.0.113.19GET /api/v1/feedHTTP 200Allowed (1 token consumed)
203.0.113.19GET /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.

Clients on the internet1× instanceReverse proxy2× instancesBackend servers6× instancesClients on the internet1× instanceReverse proxy2× instancesBackend servers6× instancesClients on the internet1× instanceReverse proxy2× instancesBackend servers6× instances
Figure 4

A reverse proxy in front of private servers.

Follow a request from the internet: it reaches the reverse proxy, the only address the public sees, which terminates TLS, checks the request against its firewall rules and its rate limit, and only then forwards it to a backend server. Requests that break a rule or exceed the limit stop at the proxy, so the backends see only what passed.

  • Clients on the internet: 1 × Users
  • Reverse proxy: 2 × Load balancer
  • Backend servers: 6 × App server
  • Clients on the internet sends requests to Reverse proxy
  • Reverse proxy sends requests to Backend servers

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 BBwhat its symbols mean and holds the average to rrwhat its symbols mean; 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.
Sources6