keepcalculator

06 Numbers and Powers

Reverse Proxy Routing: How It Works

Card No. 06·72003 · Updated · Standard formula, shown below

A reverse proxy server sits in front of your backend servers and routes each incoming client request to the correct backend based on rules you define. You send a request to the proxy’s address, and it forwards that request to a backend server, then returns the backend’s response to the client. This article explains the routing mechanisms, common use cases, and how to choose and configure a reverse proxy.

What Is a Reverse Proxy?

A reverse proxy is a server that accepts client requests, forwards them to one or more backend servers, and relays the response back to the client. Unlike a forward proxy (which acts on behalf of clients, e.g., to bypass geo-blocks), a reverse proxy acts on behalf of servers. Clients only see the proxy’s address; they never interact directly with the backend. This adds a layer of abstraction that enables load balancing, caching, SSL termination, and security filtering.

How Routing Decisions Are Made

The core of reverse proxy routing is the set of rules that map an incoming request to a specific backend. These rules can be based on several parts of the HTTP request:

  • Host header – the domain name (e.g., api.example.com vs. app.example.com) can route to different backends.
  • URL path – e.g., /api/* goes to an API server, /images/* goes to a static file server.
  • HTTP method – GET requests might be cached, while POST requests are forwarded directly.
  • Headers – custom headers (e.g., X-Client-Type) can influence routing.
  • Client IP or geographic location – can be used for geo-routing or access control.
  • Weighted round-robin – when multiple backends serve the same content, the proxy distributes traffic according to weights.

Most reverse proxies (Nginx, HAProxy, Traefik, Envoy) use a configuration file or an API to define these rules. For example, in Nginx you might write:

location /api/ {
    proxy_pass http://backend_api;
}
location /static/ {
    proxy_pass http://static_server;
}

This routes any request whose path starts with /api/ to the backend_api upstream group, and /static/ to the static server.

Load Balancing and Health Checks

When you have multiple backend servers running the same application, the reverse proxy acts as a load balancer. It uses algorithms to decide which backend gets each request:

  • Round-robin – each backend gets a request in turn.
  • Least connections – sends to the backend with the fewest active connections.
  • IP hash – maps the client’s IP to a specific backend, ensuring session stickiness.
  • Weighted distribution – some backends get more traffic based on capacity.

To avoid sending requests to a down backend, the proxy performs health checks – periodic requests to a known endpoint (e.g., /health) – and removes unhealthy servers from the pool until they recover.

Common Use Cases for Reverse Proxies

  • Load balancing – distribute traffic across multiple servers to improve performance and availability.
  • SSL/TLS termination – handle HTTPS encryption at the proxy, relieving backend servers of that overhead.
  • Caching – store static or even dynamic responses to reduce backend load.
  • Security – hide backend server details, filter malicious requests, and rate-limit clients.
  • Microservices aggregation – expose a single entry point to multiple microservices, routing by path or header.
  • Canary deployments – route a small percentage of traffic to a new version of an app to test it safely.

How to Choose a Reverse Proxy

Popular options include Nginx, HAProxy, Traefik, and Envoy. The right choice depends on your needs:

  • Nginx – widely used, great for static files and simple routing, high performance.
  • HAProxy – specialized in load balancing, very stable, excellent for TCP and HTTP traffic.
  • Traefik – designed for containerized environments, auto-discovers services via Docker or Kubernetes.
  • Envoy – powerful for microservices and service meshes, but has a steeper learning curve.

Consider ease of configuration, dynamic service discovery, built-in metrics, and community support when deciding.

Common Problems and Pitfalls

  • Incorrect proxy_pass paths – if the proxy adds or strips path prefixes incorrectly, backends may return 404.
  • Header forwarding – forgetting to pass Host, X-Forwarded-For, or X-Real-IP can break logging or authentication.
  • WebSocket support – not all proxies handle WebSocket upgrades by default; you must enable them.
  • Timeouts – if the backend takes longer than the proxy’s timeout, the client sees a 504 error.
  • Sticky sessions – if your app stores session state in memory, you need session affinity; otherwise, requests may go to different backends.
  • SSL certificate mismatches – when the proxy terminates SSL, the backend may still expect HTTPS; configure the proxy to use HTTP to the backend.

Security and Performance Considerations

A reverse proxy can significantly improve security by acting as a shield. It can block traffic from suspicious IPs, limit request rates, and hide the internal network structure. However, it also becomes a single point of failure – if the proxy goes down, the entire service is unreachable. Therefore, run multiple proxy instances behind a load balancer or use DNS round-robin. Performance-wise, the proxy adds a small latency overhead (usually sub-millisecond), but it can reduce load on backends through caching and connection pooling.

Frequently Asked Questions

What is the difference between a reverse proxy and a load balancer?

A load balancer is a type of reverse proxy that distributes traffic across multiple servers. All load balancers are reverse proxies, but not all reverse proxies load balance – some simply route to a single backend or perform caching/security.

How does a reverse proxy handle WebSockets?

WebSockets require a persistent connection. Most reverse proxies (Nginx, HAProxy, Traefik) support WebSocket proxying, but you must configure them to upgrade the connection and pass the Upgrade and Connection headers. If not enabled, the WebSocket handshake fails.

Can a reverse proxy cache dynamic content?

Yes, but only if the backend sends appropriate cache headers (e.g., Cache-Control). The proxy can cache responses for a specified time, but dynamic content that changes per user should not be cached unless you use a mechanism like ESI or vary by cookie.

Does a reverse proxy hide the backend IP address?

Yes, clients only see the proxy’s IP. This hides the backend’s IP from the public, which can prevent direct attacks. However, the proxy must be configured to set the X-Forwarded-For header so the backend can see the client’s real IP.

What is sticky sessions and do I need it?

Sticky sessions (or session affinity) means the proxy always sends a given client to the same backend server. This is needed if your backend stores session data in memory (e.g., PHP sessions). If you use a shared session store like Redis, you don’t need sticky sessions.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →