CodePark/ questions / Sockets, ports & proxies
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › Sockets, ports & proxies

What is the difference between a forward proxy and a reverse proxy? Give examples of each.

The difference is whose side the proxy is on. A forward proxy acts for clients. A reverse proxy acts for servers.

A forward proxy sits in front of clients. It sends requests to other servers for them, so those servers see the proxy, not the client. Examples:

  • Companies use one to filter or log outbound traffic.
  • An app in a locked-down network may reach external APIs only through an egress proxy (a proxy for outgoing traffic), set in HTTPS_PROXY.

A reverse proxy sits in front of servers and accepts requests for them. The client thinks it's talking to the real site. It doesn't know which backend answered. Nginx, HAProxy, Envoy, cloud load balancers and CDNs are reverse proxies. They handle:

  • TLS termination (decrypting HTTPS before passing the request on)
  • load balancing
  • caching
  • compression
  • rate limiting
  • path-based routing

Behind a reverse proxy, the app sees the proxy's IP, not the client's. So the app reads the client IP from the X-Forwarded-For header. It should trust this header only when it comes from known proxies, because clients can fake it.

  • A forward proxy acts for clients and hides them from servers.
  • A reverse proxy acts for servers and hides the backends from clients.
  • Reverse proxies handle TLS termination, load balancing, caching and routing.
  • Apps behind a reverse proxy get the client IP from the X-Forwarded-For header.