Forward Proxy vs Reverse Proxy: Differences Explained with Examples
A proxy is a server that sits between a client and another server and passes messages between them. There are two main kinds. A forward proxy works on behalf of the clients, and a reverse proxy works on behalf of the servers. They look alike on a diagram, but they solve very different problems, and nearly every large web system uses at least one of them.
This tutorial explains what each proxy does, how a request flows through it, where each one is used, and how they compare side by side. You will also see Java code that sends traffic through a forward proxy, a small reverse proxy built with Spring Boot, and an Nginx configuration you can use as a starting point.
1. What Is a Proxy Server?
The word proxy means "someone who acts on behalf of another person". If you cannot attend a meeting, you might send a colleague as your proxy to vote for you. In networking, a proxy server does the same job: it receives a request, sends it on to the real destination for someone else, and returns the answer.
Because every message passes through it, a proxy is a natural place to add extra behaviour, such as:
- Hiding identity: the other side sees the proxy's address instead of the real sender or receiver.
- Filtering: blocking requests that break a rule.
- Caching: keeping copies of responses so repeat requests are answered faster.
- Logging and monitoring: recording who accessed what, and when.
- Security: inspecting traffic and stopping attacks before they reach their target.
The key question that separates the two proxy types is simple: whose side is the proxy on? A forward proxy belongs to the clients. A reverse proxy belongs to the servers.
2. What Is a Forward Proxy?
A forward proxy sits in front of a group of clients, such as the computers in an office or a school. When any of those clients wants to reach a website, the request goes to the forward proxy first. The proxy then contacts the website on the client's behalf and passes the response back.
From the website's point of view, every request comes from the proxy. The website does not know which laptop or phone actually asked for the page.
3. How a Forward Proxy Works
Suppose an employee opens https://news-site.com on a company laptop that is configured to use a forward proxy:
- The client sends the request to the proxy. The browser knows the proxy's address because it was set in the operating system, the browser settings, or a configuration script provided by the company.
- The proxy checks its rules. It may confirm that the user is logged in, that the website is not on a block list, and that the request is allowed at this time.
- The proxy may answer from its cache. If someone in the office requested the same public file a moment ago and it can be reused, the proxy returns its stored copy.
- Otherwise, the proxy contacts the website. It opens its own connection to
news-site.com. The website sees the proxy's IP address as the sender. - The response travels back the same way. The website replies to the proxy, the proxy may log or scan the response, and then it passes the response to the laptop.
For HTTPS sites, the browser usually asks the proxy to open a tunnel with the HTTP CONNECT method. The proxy then relays encrypted bytes without reading them. It knows which website the client is visiting, but not what is inside the pages, unless the company has installed its own certificate on every device to inspect the traffic.
4. Common Uses of a Forward Proxy
- Content filtering: schools and companies block categories of websites, such as gambling or malware sites.
- Privacy: the destination sees the proxy's address instead of the user's real IP address. Anonymous proxies and many VPN-style services rely on this idea.
- Shared caching: when hundreds of people download the same software update, the proxy can fetch it once and serve the rest from its cache, saving internet bandwidth.
- Controlled outbound access: servers in a locked-down data centre are often not allowed to reach the internet directly. They must go through an approved forward proxy, so every outbound call is logged and checked.
- Accessing region-specific content: a proxy located in another country makes the request look as if it came from that country. Testers use this to check how a site behaves in different regions.
- Auditing: security teams can see which external services each machine talks to.
5. What Is a Reverse Proxy?
A reverse proxy sits in front of a group of servers. Users on the internet think they are talking directly to your website, but they are really talking to the reverse proxy. The proxy decides which backend server should handle each request, forwards it, and returns the response to the user.
This time it is the servers that are hidden. Users never learn how many servers you run, what their internal addresses are, or which one handled their request.
6. How a Reverse Proxy Works
Imagine a user opens https://www.shop.com/products/42, and the domain points to a reverse proxy:
- DNS points to the proxy. The domain name resolves to the public IP address of the reverse proxy, not to any application server.
- The proxy terminates HTTPS. It holds the website's TLS certificate, decrypts the request, and can now read the URL and headers.
- The proxy applies its rules. It may reject a request from a blocked IP, enforce a rate limit, or return a cached copy of the page if one is still fresh.
- The proxy picks a backend. Based on the path (
/productsgoes to the product service), the load-balancing algorithm, and the health of each server, it chooses where to send the request. - The proxy forwards the request. It usually adds headers such as
X-Forwarded-Forso the backend still knows the user's real IP address. - The response returns through the proxy. The proxy can compress it, add security headers, store it in its cache, and then send it to the user.
The user's browser only ever talks to the proxy. If App Server 2 crashes, the proxy stops sending traffic to it, and users do not notice.
7. Common Uses of a Reverse Proxy
- Load balancing: spreading requests across several servers so no single server is overwhelmed.
- SSL/TLS termination: handling the costly encryption work in one place, so backend servers can focus on business logic and certificates are managed centrally.
- Caching: storing responses for pages and files that many users request, which lowers load on the application.
- Compression: shrinking responses with gzip or Brotli before they leave the data centre.
- Security shield: backend servers have no public IP, so attackers cannot reach them directly. The proxy can also run a Web Application Firewall (WAF) and absorb some denial-of-service traffic.
- Path-based routing: sending
/apito one service,/imagesto object storage and everything else to the web front end, all under one domain. - Zero-downtime deployments: moving traffic gradually from the old version of an application to the new one.
Popular reverse proxies include Nginx, HAProxy, Envoy, Traefik, Apache HTTP Server, and the edge servers of CDN providers.
8. Forward vs Reverse Proxy: The One-Picture Difference
Both proxies sit in the middle of a client-to-server conversation. The difference is which side owns the proxy, and therefore who gets hidden.
A handy way to remember it: a forward proxy pushes your requests forward out into the internet; a reverse proxy receives requests coming in the reverse direction, from the internet into your servers.
9. Comparison Table
| Aspect | Forward Proxy | Reverse Proxy |
|---|---|---|
| Works on behalf of | Clients (users, devices, internal servers making outbound calls) | Servers (your website or backend services) |
| Who is hidden | The client's identity and IP address | The backend servers' identity and internal layout |
| Who configures it | The client side: browser, OS, or app settings point to it | The server side: DNS points users to it, clients do nothing special |
| Does the client know about it? | Usually yes, it is configured on the client | Usually no, it looks like the real website |
| Traffic direction | Outbound: from a private network to the internet | Inbound: from the internet to a private network |
| Typical goals | Filtering, privacy, outbound control, shared caching, auditing | Load balancing, TLS termination, caching, security, routing |
| Typical owner | A company IT team, a school, a privacy service | The team running the website or API |
| Common software | Squid, corporate secure web gateways, browser proxy services | Nginx, HAProxy, Envoy, Traefik, CDN edge servers |
| Main risk if misused | Hiding malicious users, bypassing restrictions | Becoming a single point of failure if not made redundant |
10. Calling the Internet Through a Forward Proxy in Java
Many companies require backend services to reach external APIs only through an approved forward proxy. Java's built-in HttpClient (Java 11+) supports this with a ProxySelector. The example below sends every request through a proxy at proxy.company.local:3128.
package com.javabytechie.proxy;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class ForwardProxyClient {
public static void main(String[] args) throws Exception {
// All requests made by this client go to the forward proxy first
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("proxy.company.local", 3128)))
.connectTimeout(Duration.ofSeconds(5))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/status"))
.timeout(Duration.ofSeconds(10))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status: " + response.statusCode());
System.out.println("Body: " + response.body());
}
}
Status: 200
Body: {"service":"example-api","state":"UP"}
The external API sees the proxy's IP address, not your server's. If you prefer not to change code, you can set the proxy for the whole JVM with system properties instead. The nonProxyHosts list makes internal addresses bypass the proxy:
java -Dhttps.proxyHost=proxy.company.local -Dhttps.proxyPort=3128 \
-Dhttp.proxyHost=proxy.company.local -Dhttp.proxyPort=3128 \
-Dhttp.nonProxyHosts="localhost|*.internal.company.local" \
-jar order-service.jar
Java's HttpClient picks up these JVM-wide properties automatically when no proxy is set on the builder, because it falls back to ProxySelector.getDefault(). Some third-party libraries behave differently. For example, Apache HttpClient 5 ignores them unless you build the client with useSystemProperties(), so always check how your HTTP library finds its proxy.
11. Building a Tiny Reverse Proxy with Spring Boot
To see what a reverse proxy really does, here is a minimal one written with Spring Boot 3. It accepts requests on port 8080 and forwards anything under /api/ to a backend running on port 8081, adding the standard forwarding headers. This is for learning only. In production you would use Nginx, HAProxy, Envoy or Spring Cloud Gateway, which handle streaming, timeouts, retries and errors properly.
package com.javabytechie.proxy;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;
@RestController
public class ReverseProxyController {
// The hidden backend: users never see this address
private final RestClient backend = RestClient.create("http://localhost:8081");
@GetMapping("/api/**")
public ResponseEntity<String> forward(HttpServletRequest request) {
String path = request.getRequestURI();
String query = request.getQueryString();
String target = (query == null) ? path : path + "?" + query;
return backend.get()
.uri(target)
// Tell the backend who the real client is
.header("X-Forwarded-For", request.getRemoteAddr())
.header("X-Forwarded-Proto", request.getScheme())
.header("X-Forwarded-Host", request.getServerName())
.retrieve()
.toEntity(String.class);
}
}
The backend service can read those headers to learn the original client address:
package com.javabytechie.backend;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@GetMapping("/api/products/{id}")
public String product(@PathVariable long id,
@RequestHeader(value = "X-Forwarded-For", required = false) String clientIp) {
return "Product " + id + " served by backend on port 8081 for client " + clientIp;
}
}
Product 42 served by backend on port 8081 for client 203.0.113.25
In real Spring Boot applications that sit behind a proxy, set server.forward-headers-strategy=framework (or native when running on Tomcat, Jetty or Undertow). Spring then uses the forwarded headers when building redirect URLs and when you call request.getRemoteAddr(), so links and logs show the user's details instead of the proxy's. Only trust these headers when they come from your own proxy, because any client can send a fake X-Forwarded-For.
12. Reverse Proxy Configuration Example with Nginx
Nginx is one of the most widely used reverse proxies. This configuration terminates HTTPS, balances traffic across two Spring Boot instances, and passes the forwarding headers:
upstream shop_backend {
server 10.0.1.11:8080;
server 10.0.1.12:8080;
}
server {
listen 443 ssl;
server_name www.shop.com;
ssl_certificate /etc/ssl/shop/fullchain.pem;
ssl_certificate_key /etc/ssl/shop/privkey.pem;
location /api/ {
proxy_pass http://shop_backend;
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;
proxy_connect_timeout 3s;
proxy_read_timeout 30s;
}
location /static/ {
root /var/www/shop;
expires 7d;
}
}
Requests for /static/ are served straight from disk by Nginx, while /api/ requests are spread across the two application servers. The backends at 10.0.1.11 and 10.0.1.12 live on a private network and have no public IP address.
13. Reverse Proxy vs Load Balancer vs API Gateway
These three terms overlap, which often confuses beginners. They are best understood as layers of features:
| Component | Core job | Relationship |
|---|---|---|
| Reverse proxy | Receives requests for servers and forwards them | The general concept. It can work with only one backend. |
| Load balancer | Spreads requests across many identical servers | A reverse proxy whose main focus is distributing load and checking server health. |
| API gateway | Single entry point for many APIs and microservices | A reverse proxy with API features: authentication, rate limits per client, request transformation, API keys and analytics. |
In practice, a single product such as Nginx or Envoy can play all three roles. Network load balancers that work at the TCP level (layer 4) are a partial exception: they forward packets or connections without reading HTTP, so they cannot route by URL path.
14. Other Proxy Types You May Hear About
- Transparent proxy: a forward proxy that intercepts traffic without any client configuration. Public Wi-Fi networks and some internet providers use them. The client may not even know it exists.
- Anonymous proxy: a forward proxy that deliberately removes identifying headers so the destination cannot find the user's real IP address.
- SOCKS proxy: works at a lower level than HTTP and can relay any kind of TCP traffic, such as database or email connections.
- Sidecar proxy: in a service mesh, a small proxy such as Envoy runs next to each microservice and handles both its inbound traffic (reverse proxy role) and its outbound calls (forward proxy role).
- Egress gateway: a cloud or Kubernetes component that acts as a forward proxy for all outbound traffic leaving a cluster.
15. Common Mistakes to Avoid
- Trusting
X-Forwarded-Forfrom anyone. A user can send this header themselves. Only accept it when the request came from your own proxy, otherwise rate limits and IP blocks can be bypassed. - Running a single reverse proxy. If every request flows through one proxy and it fails, the whole site is down. Run at least two instances behind a floating IP or a cloud load balancer.
- Leaving backends reachable from the internet. If the application servers still have public IPs, attackers can skip the proxy and its protections entirely.
- Creating an open forward proxy. A forward proxy that accepts requests from anyone on the internet will quickly be abused to hide attacks and spam. Always restrict who can use it.
- Caching private data. A reverse proxy that caches responses must never store pages containing one user's personal data and serve them to another user.
- Forgetting timeouts. Without connect and read timeouts, slow backends tie up proxy connections until the proxy itself stops responding.
16. Best Practices
- Decide the purpose first: use a forward proxy to control outbound traffic and a reverse proxy to protect and scale inbound traffic.
- Run reverse proxies in pairs or clusters, and use health checks so failed backends are removed automatically.
- Terminate TLS at the reverse proxy, and use TLS again between the proxy and backends when the internal network is not fully trusted.
- Configure your application framework to understand forwarded headers, and restrict which proxy addresses are trusted.
- Set sensible timeouts, request size limits and rate limits on the proxy.
- Add security headers such as
Strict-Transport-Securityin one place at the reverse proxy. - Log and monitor proxy metrics: request rate, error rate, latency and upstream health.
- For forward proxies, require authentication and keep allow lists of approved external destinations.
17. Summary
- A proxy is a middle server that passes requests and responses on behalf of someone else.
- A forward proxy works for clients. It controls and hides outbound traffic, and destinations only see the proxy.
- A reverse proxy works for servers. It receives inbound traffic, hides the backends, and adds load balancing, TLS termination, caching and security.
- Clients usually configure a forward proxy themselves, while a reverse proxy is invisible to them because DNS points straight to it.
- Load balancers and API gateways are specialised reverse proxies.
Once you can answer "whose side is the proxy on?", you can tell the two apart in any architecture diagram. Open the FAQ tab for answers to common questions about forward and reverse proxies.
Frequently Asked Questions (FAQ)
Q1. What is the main difference between a forward proxy and a reverse proxy?
A forward proxy acts on behalf of clients and hides them from the servers they visit. A reverse proxy acts on behalf of servers and hides them from the clients that visit them.
In short, the forward proxy belongs to the user's side of the network, and the reverse proxy belongs to the website's side.
Q2. Is a VPN the same as a forward proxy?
They are similar but not the same. Both hide your IP address from the websites you visit. A typical forward proxy handles traffic from specific applications, such as a browser, and does not necessarily encrypt it. A VPN creates an encrypted tunnel for all network traffic from your device to the VPN server.
Q3. Is a load balancer a reverse proxy?
An HTTP (layer 7) load balancer is a type of reverse proxy whose main job is spreading requests across several servers. A reverse proxy is the broader idea: it can forward requests even to a single server, purely for caching, TLS termination or security.
Some layer 4 load balancers just forward TCP connections or packets without reading HTTP, so they behave less like a full reverse proxy.
Q4. Is an API gateway a reverse proxy?
Yes. An API gateway is a reverse proxy with extra features designed for APIs, such as authentication, API keys, per-client rate limits, request and response transformation, and usage analytics. It acts as the single front door to a group of microservices.
Q5. Do users need to configure anything to use a reverse proxy?
No. The website's DNS record points to the reverse proxy, so users reach it automatically when they type the domain name. To the user, the reverse proxy simply is the website.
Q6. How do clients start using a forward proxy?
The client must be told about it. This can be done in several ways:
- Manual proxy settings in the browser or operating system.
- A proxy auto-configuration (PAC) file supplied by the network.
- Application settings, such as JVM properties like
https.proxyHost. - A transparent proxy, where the network redirects traffic without any client setup.
Q7. Can a forward proxy read my HTTPS traffic?
Normally, no. For HTTPS, the browser asks the proxy to open a tunnel with the CONNECT method, and the encrypted data passes through unread. The proxy can see which domain you are connecting to, but not the page content.
The exception is TLS inspection, used by some companies: they install their own root certificate on employee devices so the proxy can decrypt, scan and re-encrypt traffic.
Q8. What is SSL or TLS termination at a reverse proxy?
TLS termination means the reverse proxy holds the website's certificate and decrypts incoming HTTPS requests. It then forwards them to backend servers, either as plain HTTP on a trusted private network, or re-encrypted with TLS. This centralises certificate management and removes encryption work from the application servers.
Q9. What is the X-Forwarded-For header?
X-Forwarded-For is an HTTP header that a proxy adds to tell the backend the original client's IP address. Without it, the backend would see every request as coming from the proxy.
When a request passes through several proxies, each one appends an address, so the header can contain a list such as 203.0.113.25, 10.0.0.5. The standard Forwarded header carries the same information in a newer format.
Q10. How does a reverse proxy improve security?
- Backend servers can live on a private network with no public IP address.
- Attackers cannot learn your internal server layout.
- The proxy can filter malicious requests with a Web Application Firewall.
- Rate limiting and connection limits blunt brute-force and flooding attacks.
- Security headers and TLS settings are enforced in one place.
Q11. Can one server act as both a forward and a reverse proxy?
Yes. Software such as Nginx, Envoy and Squid can be configured for either role. In a service mesh, the sidecar proxy next to each microservice does both: it handles incoming requests for its service like a reverse proxy and outgoing calls from its service like a forward proxy.
Q12. Does a reverse proxy add latency?
It adds an extra network hop, usually a fraction of a millisecond to a few milliseconds inside the same data centre. In practice it often makes sites faster overall, because it caches responses, compresses data, reuses connections to backends and serves static files very efficiently.
Q13. Is a CDN a forward proxy or a reverse proxy?
A CDN is a reverse proxy. Its edge servers act on behalf of your website: users connect to the nearest edge server, which serves cached content or fetches it from your origin server. Users do not configure anything, and your origin stays hidden behind the CDN.
Q14. What are some popular forward and reverse proxy tools?
- Forward proxies: Squid, corporate secure web gateways, cloud egress gateways and SOCKS proxies.
- Reverse proxies: Nginx, HAProxy, Envoy, Traefik, Apache HTTP Server, Caddy and cloud load balancers.
- Gateways for Java teams: Spring Cloud Gateway.
Q15. What is a transparent proxy?
A transparent proxy is a forward proxy that intercepts traffic at the network level, so clients do not need any settings and may not know it exists. It is often used on public Wi-Fi, in schools and by some internet providers for filtering or caching. It does not hide the client's identity from the destination in any meaningful way.
Q16. Why should backend servers sit behind a reverse proxy in production?
Placing backends behind a reverse proxy lets you scale them, replace them and deploy new versions without changing the public address. It also gives you one place for TLS, caching, compression, logging and security rules, and it keeps the backends off the public internet.
Q17. How do I make a Spring Boot application work correctly behind a reverse proxy?
Set server.forward-headers-strategy=framework or native in application.properties. Spring Boot will then use the X-Forwarded-* or Forwarded headers so that redirects use the public host name and HTTPS scheme, and the client's real IP address is available. Make sure only your own proxy can set these headers.
Q18. What happens if the reverse proxy goes down?
If there is only one reverse proxy, the whole website becomes unreachable, even if every backend is healthy. That is why production systems run several proxy instances, use a floating virtual IP or a managed cloud load balancer in front of them, and monitor them closely.
Q19. Can a forward proxy cache content?
Yes. A caching forward proxy keeps copies of public resources, such as software downloads or images, so other users on the same network get them without a new internet download. This was very common when internet links were slow and is still used for large shared files and package repositories.
Q20. Which proxy should I learn first as a backend developer?
Start with the reverse proxy. Almost every web application you build will run behind one, so understanding routing, TLS termination, timeouts and forwarded headers is essential. Learn forward proxies when your services need to call external APIs from a restricted corporate or cloud network.