00:00

What Is a Reverse Proxy? How It Works

Last updated:

When you open a large website, your browser rarely talks to the application server directly. It talks to a reverse proxy, a server that receives the request on behalf of the backend, forwards it, and sends the response back as if it had produced it itself.

Reverse proxies sit in front of almost every production web system. They hide internal servers, handle HTTPS, cache responses and spread load. This tutorial explains how they work and how to configure one.

1. What Is a Reverse Proxy?

A reverse proxy is a server that accepts client requests for one or more backend servers, forwards each request to a suitable backend, and returns the backend's response to the client. The client sees only the proxy's address and usually does not know that other servers exist.

It is called "reverse" because it works for the servers. A forward proxy works for the clients. You can read about that in Forward Proxy.

2. Reverse Proxy Architecture

Reverse proxy architecture Clients on the internet send requests to one public reverse proxy. The proxy terminates HTTPS, serves cached responses, adds security checks, and forwards requests over a private network to the web app, the API service and the static file server. The servers are never exposed to the internet. PRIVATE NETWORK INTERNET Browser Mobile app Reverse ProxyTLS · cache · securityone public address Web app:8080 API service:8081 Static files:8082 HTTPS in, HTTP or HTTPS out
A reverse proxy is the single public entry point. The servers behind it stay on the private network.

Only the proxy has a public IP address. The application servers listen on private addresses, so attackers cannot reach them directly, and the team can move or replace them without changing anything that clients see.

3. How a Reverse Proxy Works

Suppose a user requests https://shop.example.com/api/cart:

  1. DNS resolves shop.example.com to the reverse proxy's public IP.
  2. The proxy completes the TLS handshake with its certificate and decrypts the request.
  3. It checks rules: rate limits, blocked IPs, allowed methods. It then matches the path /api/ to the API service.
  4. If a fresh cached response exists, it returns it immediately. Otherwise it forwards the request to the API service, adding headers such as X-Forwarded-For so the backend knows the real client.
  5. The backend responds; the proxy may compress or cache the response, then sends it to the browser.

Two details make this efficient. First, the proxy keeps a pool of keep-alive connections to the backends, so it does not pay for a new TCP and TLS handshake on every request. Second, it buffers responses: it reads the backend's response quickly, frees the backend, and then delivers the data to the client at the client's own pace. Slow mobile networks therefore do not tie up application threads.

4. Key Functions of a Reverse Proxy

  • TLS termination: one place to manage certificates and HTTPS settings.
  • Load balancing: spread requests across several instances with an algorithm such as Round Robin.
  • Caching: store responses and serve repeat requests without touching the backend; see caching.
  • Compression: gzip or Brotli responses to save bandwidth.
  • Security: hide backends, filter bad requests, limit request rates and body sizes.
  • Routing: send different paths or hostnames to different applications.
  • Observability: one central access log for all traffic.

Popular reverse proxies include Nginx, HAProxy, Envoy, Traefik, Caddy and Apache httpd. In Java microservices, Spring Cloud Gateway often plays this role.

5. Practical Implementation

Nginx reverse proxy with TLS, caching and headers

nginx.conf
http {
    proxy_cache_path /var/cache/nginx keys_zone=api_cache:10m max_size=1g inactive=10m;

    upstream app_servers {
        server 10.0.1.11:8080;
        server 10.0.1.12:8080;
    }

    server {
        listen 443 ssl;
        server_name shop.example.com;
        ssl_certificate     /etc/nginx/certs/shop.crt;
        ssl_certificate_key /etc/nginx/certs/shop.key;

        client_max_body_size 5m;
        gzip on;

        location / {
            proxy_pass http://app_servers;
            proxy_set_header Host              $host;
            proxy_set_header X-Real-IP         $remote_addr;
            proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location /api/products {
            proxy_cache api_cache;
            proxy_cache_valid 200 1m;
            proxy_pass http://app_servers;
        }
    }
}

Spring Boot behind a reverse proxy

A Spring Boot application must trust the forwarded headers to build correct redirect URLs and to see the client IP. One property enables this:

application.yml
server:
  forward-headers-strategy: native   # use X-Forwarded-* headers from the proxy
Note: Only enable forwarded headers when the application is reachable through your proxy alone. Otherwise a client could send fake X-Forwarded-For values.

6. Advantages and Disadvantages

Advantages

  • Backends are hidden from the internet.
  • Certificates and HTTPS settings live in one place.
  • Caching and compression reduce load and latency.
  • Servers can be added, removed or upgraded without clients noticing.

Disadvantages

  • An extra network hop adds a little latency.
  • A single proxy is a single point of failure unless it is made redundant.
  • Misconfigured headers can leak or spoof client information.
  • It becomes one more component to secure, patch and monitor.

7. Reverse Proxy Compared with Related Components

ComponentActs forMain job
Reverse proxyServersSingle entry point: TLS, caching, routing, security
Forward proxyClientsControls and hides outbound client traffic
Load balancerServersSpreads traffic across instances; often a reverse proxy
API gatewayAPIsReverse proxy plus auth, rate limits and API management
CDNContent ownersGlobally distributed reverse proxy cache near users

For detailed comparisons, see Reverse Proxy vs Load Balancer and Forward Proxy vs Reverse Proxy.

8. Real-World Use Cases

  • Single domain, many apps: /blog, /shop and /api served by different services behind one hostname.
  • Kubernetes Ingress: the ingress controller is a reverse proxy that routes traffic to Services.
  • Legacy modernisation: route some paths to a new microservice and the rest to the old monolith while you migrate.
  • Protecting slow backends: buffer slow client uploads at the proxy so application threads are not tied up.

9. Best Practices

  • Security: use modern TLS settings, hide the server version, limit body sizes and request rates, and block direct access to backends with firewall rules.
  • Reliability: run at least two proxy instances behind a floating IP or a cloud load balancer, and enable health checks.
  • Performance: enable keep-alive to backends, caching for safe GET responses, and compression for text.
  • Correct headers: always set Host, X-Forwarded-For and X-Forwarded-Proto, and make backends trust them only from the proxy.
  • Timeouts: set connect and read timeouts so a hung backend does not tie up proxy connections.

10. Summary

  • A reverse proxy receives requests for backend servers and forwards them, hiding the servers from clients.
  • It centralises TLS, caching, compression, routing and security.
  • Nginx, HAProxy, Envoy and Spring Cloud Gateway are common reverse proxies.
  • Backends must read forwarded headers to know the real client and protocol.
  • Run proxies redundantly to avoid a single point of failure.

Next, learn the client-side counterpart in Forward Proxy.