> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/system-design/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/system-design/chapter/proxies/reverse_proxy.md).

# Reverse proxy

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvHzpublmUWisvz%2Fsync%2Fdb73e447ce8c4f250d17262a5944d0e3e8e3f482.png?generation=1591680794460561\&alt=media)

> * A **reverse proxy** accepts a request from a client, forwards it to a server that can fulfill it, and returns the server’s response to the client.
> * A **load balancer** distributes incoming client requests among a group of servers, in each case returning the response from the selected server to the appropriate client.

A reverse proxy is a **web server** that **centralizes internal services** and provides **unified interfaces** to the public. Requests from clients are forwarded to a server that can fulfill it before the reverse proxy returns the server's response to the client.

> reverse proxy 是一个集中式的 web server，它集合了内部的 services 和 interfaces，然后由它发给 backend servers 处理。

Additional benefits include:

* **Increased security** - Hide information about backend servers, blacklist IPs, limit number of connections per client
* **Increased scalability and flexibility** - Clients only see the reverse proxy's IP, allowing you to scale servers or change their configuration
* **SSL termination** - Decrypt incoming requests and encrypt server responses so backend servers do not have to perform these potentially expensive operations
  * Removes the need to install [X.509 certificates](https://en.wikipedia.org/wiki/X.509) on each server
* **Compression** - Compress server responses
* **Caching** - Return the response for cached requests
* **Static content** - Serve static content directly
  * HTML/CSS/JS
  * Photos
  * Videos
  * Etc

## Load balancer vs reverse proxy

* Deploying a load balancer is useful when you have multiple servers. Often, load balancers route traffic to a set of servers serving the same function.
* Reverse proxies can be **useful even with just one web server or application server**, opening up the benefits described in the previous section.
* Solutions such as **NGINX** and **HAProxy** can support both **layer 7 reverse proxying and load balancing**.

> reverse proxy 更多的是在有一个 web server 和一个 application server 的时候也有用。load balancer 是对多个，同类型的 server 有用。

## Disadvantage(s): reverse proxy

* Introducing a reverse proxy results in increased complexity.
* A single reverse proxy is a **single point of failure**, configuring multiple reverse proxies (ie a failover) further increases complexity.

## More read

* [Reverse proxy vs load balancer](https://www.nginx.com/resources/glossary/reverse-proxy-vs-load-balancer/)
* [NGINX architecture](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/)
* [HAProxy architecture guide](http://www.haproxy.org/download/1.2/doc/architecture.txt)
* [Wikipedia](https://en.wikipedia.org/wiki/Reverse_proxy)
