首页 > 代码库 > Nginx 反向代理
Nginx 反向代理
原文地址:http://nginx.com/resources/admin-guide/reverse-proxy/
NGINX Reverse Proxy
Nginx 反向代理
This section describes the basic configuration of a proxy server. You will learn how to:
- pass a request from NGINX to proxied servers over different protocols
- modify client request headers that are sent to the proxied server
- configure buffering of responses coming from the proxied servers
Proxying is typically used to distribute the load among several servers, seamlessly show content from different websites, or pass requests for processing to application servers over protocols other than HTTP.
本章讨论代理服务器的基本配置,你将学到以下内容:
- 通过不同的协议发送请求给代理服务器
- 修改发送到代理服务器的请求头
- 配置来自代理服务器的响应缓冲
代理最经典的是用于把负载分布在多台服务器之间,无缝地展示来自不同网站的内容,通过除了HTTP以外的协议发送请求到各种应用服务器来处理请求。
Passing a Request to a Proxied Server
发送一个请求到代理服务器
When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client. It is possible to proxy requests to an HTTP server (another NGINX server or any other server) or a non-HTTP server (which can run an application developed with a specific framework, such as PHP or Python) using a specified protocol. Supported protocols include FastCGI, uwsgi, SCGI, and memcached.
当Nginx代理一个请求,把请求发送给指定的代理服务器,取回响应,发回给客户端。可能使用某个协议代理请求到一个HTTP服务器(另一个Nginx服务器或者任何其他主机)或者一个非HTTP服务器(采用某个框架例如PHP或Python开发的能跑应用的服务器)。支持的协议有FastCGI,uwsgi,SCGI和memecached。
To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location. For example:
为了发送一个请求给一个HTTP代理服务器,proxy_pass 指令要指定在location块里,例如:
location /some/path/ { proxy_pass http://www.example.com/link/; }
This example configuration results in passing all requests processed in this location to the proxied server at the specified address. This address can be specified as a domain name or an IP address. The address may also include a port:
上例配置实现了将所有由这个location处理的请求发送给指定地址的代理服务器。这个地址可以是一个域名也可以是一个IP地址。这个地址可能还包含一个端口:
location ~ \.php { proxy_pass http://127.0.0.1:8000; }
Note that in the first example above, the address of the proxied server is followed by a URI,/link/. If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. For example, here the request with the/some/path/page.html URI will be proxied to http://www.example.com/link/page.html. If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).
注意在上述第一个例子中,代理服务器的地址在URI后跟了一个/link/,如果URI被指定到了这个地址,这个地址将把请求URI中匹配上location参数的部分替换。例如,带着/some/path/page.html请求的URI将被代理到http://www.example.com/link/page.html。如果指定的地址没有URI,或者没法确定哪部分URI将被替换,将发送整个请求URI(可能会被修改)。
To pass a request to a non-HTTP proxied server, the appropriate **_pass directive* should be used:
- fastcgi_pass passes a request to a FastCGI server
- uwsgi_pass passes a request to a uwsgi server
- scgi_pass passes a request to a SCGI server
- memcached_pass passes a request to a memcached server
为了发送一个请求到非HTTP代理服务器,将使用如**_pass 指令 * 形式 。
- fastcgi_pass 发送一个请求给FastCGI服务器
- uwsgi_pass 发送一个请求给uwsgi服务器
- scgi_pass 发送一个语录给 SCGI 服务器
- memcached_pass 发送一个请求给memcached服务器
Note that in these cases, the rules for specifying addresses may be different. You may also need to pass additional parameters to the server (see the reference documentation for more detail).
注意这些情况,各种服务器分配地址的规则可能不同。你可能还需要发送额外的参数到服务器(到相着文档寻找更多细节)。
The proxy_pass directive can also point to a named group of servers. In this case, requests are distributed among the servers in the group according to the specified method.
proxy_pass 指令同样也可指向命名过的主机组。这种情况下,请求会通过 specified method 在组内的主机中分发。
Passing Request Headers
发送请求头
By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close.
默认的,Nginx重定义被代理请求的两个头字段,”Host”和”Connection”,并且消除值为空串的头字段。”Host”被设置为$proxy_host变量,”Connection”被设置为关闭。
To change these setting, as well as modify other header fields, use the proxy_set_header directive. This directive can be specified in a location or higher. It can also be specified in a particular server context or in the http block. For example:
要改变这些设置,以及修改其他头字段,使用proxy_set_header指令。这个指令可以使用在location或更高层里。也可以指定在一个特别的server块或者http块里。例如:
location /some/path/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://localhost:8000; }
In this configuration the “Host” field is set to the $host variable.
在这个配置中”Host”字段设置为 $host 变量。
To prevent a header field from being passed to the proxied server, set it to an empty string as follows:
要防止一个头字段被发送给代理服务器,像下面这样把它设置为空串:
location /some/path/ { proxy_set_header Accept-Encoding ""; proxy_pass http://localhost:8000; }
Configuring Buffers
配置缓冲区
By default NGINX buffers responses from proxied servers. A response is stored in the internal buffers and is not sent to the client until the whole response is received. Buffering helps to optimize performance with slow clients, which can waste proxied server time if the response is passed from NGINX to the client synchronously. However, when buffering is enabled NGINX allows the proxied server to process responses quickly, while NGINX stores the responses for as much time as the clients need to download them.
Nginx默认缓冲来自代理服务器的响应。一个响应被存储在内核缓冲区里直到整个响应接收完毕后发送给客户端。缓冲区能够通过减慢客户端帮助优化性能,如果同步地把响应从Nginx发送到客户端会浪费代理服务器的时间。不过,当缓冲开启Nginx能让代理服务器快速地处理响应,而Nginx将存储这些响应直到客户端下载完毕。
The directive that is responsible for enabling and disabling buffering is proxy_buffering. By default it is set to on and buffering is enabled.
负责启用和禁用缓冲的指令是proxy_buffering。缺省值为on,缓冲启用。
The proxy_buffers directive controls the size and the number of buffers allocated for a request. The first part of the response from a proxied server is stored in a separate buffer, the size of which is set with the proxy_buffer_size directive. This part usually contains a comparatively small response header and can be made smaller than the buffers for the rest of the response.
proxy_buffers 指令控制分配给请求的缓冲区的大小和数量。从代理服务器返回的第一部分响应存储在一个分离的缓冲区,大小由proxy_buffer_size指令设定。这部分通常包含相对小的响应头,使得其余的部分的响应比缓冲区小。
In the following example, the default number of buffers is increased and the size of the buffer for the first portion of the response is made smaller than the default.
下例中。加大了默认缓冲区的数目,减小了保存第一部分响应的缓冲区的大小。
location /some/path/ { proxy_buffers 16 4k; proxy_buffer_size 2k; proxy_pass http://localhost:8000; }
If buffering is disabled, the response is sent to the client synchronously while it is receiving it from the proxied server. This behavior may be desirable for fast interactive clients that need to start receiving the response as soon as possible.
如果缓冲区禁用了,响应一从代理服务器接收便同步地发送给客户端。这种行为可能会令需要尽可能快接收响应的快速交互客户端满意。
To disable buffering in a specific location, place the proxy_buffering directive in the location with the off parameter, as follows:
要禁用某个location的缓冲,添加proxy_buffering指令到该location并置其值为off,如下:
location /some/path/ { proxy_buffering off; proxy_pass http://localhost:8000; }
In this case NGINX uses only the buffer configured by proxy_buffer_size to store the current part of a response.
这样子Nginx只使用通过proxy_buffer_size配置的缓冲区来保存当前部分的响应。
Nginx 反向代理