14

反向代理_系统设计笔记6

 4 years ago
source link: http://www.ayqy.net/blog/reverse-proxy/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一.什么是代理?

当然,这里的代理指的是 Web 代理服务,在客户端资源请求和提供这些资源的 Web 服务之间充当中介的角色:

In computer networking, a proxy server is a server application or appliance that acts as an intermediary for requests from clients seeking resources from servers that provide those resources.

代理服务可以实现在客户端,或者从客户端到目标服务器中间的任意环节:

A proxy server may reside on the user’s local computer, or at any point between the user’s computer and destination servers on the Internet.

例如,客户端不直接向提供目标资源的 Web 服务发起请求,而是带上目标资源的完整 URL 去请求代理服务:

GET http://en.wikipedia.org/wiki/Proxy_server HTTP/1.1
Accept: text/html

代理服务收到之后,(由代理服务来)请求指定资源并将响应结果转发给客户端:

HTTP/1.1 200 OK
Content-Type: text/html; charset UTF-8

二.什么是反向代理?

“反向”自然是相对“正向”来说的,那么, 首先要知道什么是正向代理?

正向代理

UvARFnM.png!web

正向代理是对外的,面向外部资源 ,用来从网络上获取各种数据:

A forward proxy is an Internet-facing proxy used to retrieve data from a wide range of sources (in most cases anywhere on the Internet).

代客户端发出资源请求,并将响应结果返回给对应的客户端。所以, 正向代理更靠近客户端,与客户端的关系更密切 (跟服务器关系一般,不熟)

反向代理

zYz2MfF.png!web

反向代理负责把流量根据配置规则重定向到内部服务器,外部请求并不知道内网的存在:

A reverse proxy taking requests from the Internet and forwarding them to servers in an internal network. Those making requests to the proxy may not be aware of the internal network.

反向代理是对内的,面向内部资源 ,用作对私有网络上的服务器进行访问控制和保护的前端:

A reverse proxy is usually an internal-facing proxy used as a front-end to control and protect access to a server on a private network.

所以, 反向代理更靠近服务器,与服务器的关系不一般 ,并且只提供有限的一些资源(不像正向代理能从网络获取各种资源):

The reverse proxy sits closer to the web server and serves only a restricted set of websites.

并且这种代理关系对用户来说是一定是透明的,因为用户不知道连接的是源服务还是代理服务:

A reverse proxy (or surrogate) is a proxy server that appears to clients to be an ordinary server.

而用户对正向代理通常是有感知的,明确知道自己正在通过代理访问网络资源

P.S.除正向、反向代理之外,还有:

  • 开放代理(Open proxy):面向公众(任何人都可以访问)的正向代理

  • 匿名代理(Anonymous proxy):不公开客户端原始 IP 地址的代理服务

  • 透明代理(Transparent proxy):透传请求和响应,不做任何修改,用作网关(Gateway)和路由器

三.如何理解“反向”?

反向代理与正向代理的工作原理完全一样,以致于很难区分出来:

YryuaiZ.png!web

其实,关键区别在于, 正向代理是其关联的客户端与所有服务器联系的中介,而反向代理则是其关联的服务器与所有客户端联系的中介

Unlike a forward proxy, which is an intermediary for its associated clients to contact any server, a reverse proxy is an intermediary for its associated servers to be contacted by any client.

也就是说, 正向代理代表客户端,而反向代理代表服务器

A proxy acts on behalf of the client(s), while a reverse proxy acts on behalf of the server(s).

从方向上看,正向代理代表客户端请求资源,反向代理代表服务器提供资源:

6bMjIzv.png!web

why reverse proxy named as reverse

四.反向代理有什么作用?

反向代理常用于以下场景:

  • 加密/SSL 加速:将 SSL 加密的工作交由配备了 SSL 硬件加速器的反向代理来完成

  • 负载均衡:将流量负载分发给多个 Web 服务器

  • 托管/缓存静态内容:将静态内容(比如图片)交由反向代理提供,分担源站负载

  • 压缩:代理服务器能够对资源内容进行压缩优化,提升加载速度

  • 安全防护:能够屏蔽源服务器的存在及其特征,结合防火墙抵御常见 Web 攻击

  • 访问控制:对内容进行监控/过滤,常用于工作场所、学校等

SSL 加速

对于 HTTP 服务,可以通过一层反向代理来实现 SSL 加密,具体见nginx HTTPS 反向代理

负载均衡

反向代理还可以用来实现负载均衡机制,如下图:

jIzAfaY.png!web

根据既定转发规则(即负载均衡策略)将客户端请求分发给各个服务器,并将其响应结果返回给对应的客户端

P.S.关于负载均衡的更多信息,见 负载均衡_系统设计笔记 5

参考资料


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK