What Is a Reverse Proxy? How It Works
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
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:
- DNS resolves
shop.example.comto the reverse proxy's public IP. - The proxy completes the TLS handshake with its certificate and decrypts the request.
- It checks rules: rate limits, blocked IPs, allowed methods. It then matches the path
/api/to the API service. - 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-Forso the backend knows the real client. - 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
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:
server:
forward-headers-strategy: native # use X-Forwarded-* headers from the proxy
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
| Component | Acts for | Main job |
|---|---|---|
| Reverse proxy | Servers | Single entry point: TLS, caching, routing, security |
| Forward proxy | Clients | Controls and hides outbound client traffic |
| Load balancer | Servers | Spreads traffic across instances; often a reverse proxy |
| API gateway | APIs | Reverse proxy plus auth, rate limits and API management |
| CDN | Content owners | Globally 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,/shopand/apiserved 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-ForandX-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.
Reverse Proxy: Top Interview Questions
These are the reverse proxy questions most often asked in system design interviews, with short answers you can explain in your own words.
Q1. What is a reverse proxy?
A server that receives client requests on behalf of backend servers, forwards them to a suitable backend, and returns the response. Clients see only the proxy, so the backends stay hidden.
Q2. Why is it called a "reverse" proxy?
A forward proxy acts for clients when they reach out to the internet. A reverse proxy works in the opposite direction: it acts for servers when the internet reaches in.
Q3. What does TLS termination at the reverse proxy mean?
The proxy decrypts HTTPS using the site certificate and forwards plain or re-encrypted traffic to the backends. Certificates are managed in one place, and the proxy can read requests to route, cache or filter them.
Q4. How does a backend know the real client IP behind a reverse proxy?
The proxy adds it to X-Forwarded-For (or the standard Forwarded header), and the backend reads that header. The backend must trust the header only when the request came from its own proxy.
Q5. How do you avoid the reverse proxy becoming a single point of failure?
Run two or more proxy instances in active-passive or active-active mode, behind a floating IP, DNS records or a managed cloud load balancer, ideally across availability zones.