Nginx proxy基本指令整理1

  categories:nginx  author:

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的几个主要proxy指令做一个简单的整理,本文具体包括如下指令:

proxy_bindproxy_buffer_sizeproxy_bufferingproxy_buffersproxy_busy_buffers_sizeproxy_connect_timeoutproxy_cookie_domainproxy_cookie_path 

ngx_http_proxy_module模块允许请求传递到另一台服务器。

The ngx_http_proxy_module module allows to pass requests to another server.

Example Configuration

location / {

    proxy_pass       http://localhost:8000;

    proxy_set_header Host      $host;

    proxy_set_header X-Real-IP $remote_addr;

}

注意当使用http proxy模块(甚至FastCGI),所有的连接请求在发送到后端服务器之前nginx将缓存它们,因此,在测量从后端传送的数据时,它的进度显示可能不正确。

1. proxy_bind

syntax:

proxy_bind  address;

default:

context:

http, server, location

This directive appeared in version 0.8.22.

强制使用到一个proxy server的连接使用一个特定的本地ip地址

Nginx官方原文如下:

Forces outgoing connections to a proxied server to originate from the specified local IP address.

2. proxy_buffer_size

syntax:

proxy_buffer_size   size;

default:

proxy_buffer_size 4k | 8k;

context:

http, server, location

设置用来读取从proxy server服务器响应数据第一部分的缓冲区的大小。这部分通常包含一小的响应头(header)。默认情况,这个缓冲尺寸等于通过 proxy_buffers指令指定的buffer大小相同。但是它可以制成更小

Nginx官方原文如下:

Sets size of the buffer used for reading the first part of a response received from the proxied server. This part usually contains a small response header. By default, the buffer size is equal to the size of one buffer set by the proxy_buffers directive. It can be made smaller however.

3. proxy_buffering

syntax:

proxy_buffering   on | off;

default:

proxy_buffering on;

context:

http, server, location

打开或者关闭代理服务器响应的缓冲

当缓冲被打开时, nginx将尽可能快的接收数据从代理服务器中,保存数据到缓冲中(通过proxy_buffer_size或者proxy_buffers指令进行设置的缓冲)。如果整个响应不合适保存到内存中(超过内存缓冲大小等),部分数据将被保存到磁盘临时文件中。临时文件的读写被通过proxy_max_temp_file_size指令和proxy_temp_file_write_size指令的设置进行控制。

当缓冲被关闭,只要nginx一接收到一个响应就同步传递到客户端。Nginx不会尝试读取完整响应从代理的server中。Nginx从被代理的服务器接收到的最大数据量通过proxy_buffer_size指令进行设置。

缓冲也能被打开或者关闭通过在响应头的X-Accel-Buffering字段中设置yes或者no在来关闭或者打开缓冲。这种能力可以使用proxy_ignore_headers指令禁用

Nginx官方原文如下:

Enables or disables buffering of responses from the proxied server.

When buffering is enabled, nginx receives a response from the proxied server as soon as possible, saving it into buffers set by the proxy_buffer_size and proxy_buffers directives. If the whole response does not fit into memory, part of it can be saved to a temporary file on disk. Writes to temporary files are controlled by the proxy_max_temp_file_size and proxy_temp_file_write_size directives.

When buffering is disabled, a response is passed to a client synchronously, immediately as it is received. nginx will not try to read the whole response from the proxied server. The maximum size of the data that nginx can receive from the server at a time is set by the proxy_buffer_size directive.

Buffering can also be enabled or disabled by passing “yes” or “no” in the “X-Accel-Buffering” response header field. This ability can be disabled using the proxy_ignore_headers directive.

4. proxy_buffers 

syntax:

proxy_buffers    numbersize;

default:

proxy_buffers  8  4k | 8k;

context:

http, server, location

给每一个连接到被代理的服务器的连接设置用于读取数据缓冲的数目及大小。默认情况下缓冲尺寸等于内存页的大小。这可能是4K或者8K,这取决于具体的操作系统平台。

Nginx官方原文如下:

Sets the number and size of buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.

5. proxy_busy_buffers_size

syntax:

proxy_busy_buffers_size  size;

default:

proxy_busy_buffers_size 8k|16k;

context:

http, server, location

当被代理server的响应启用了缓冲时,在被代理服务器的响应还没完全读取时,用于工作在给客户端发送响应的buffer尺寸的总体限制值可以通过这个指令进行设置。在大多数情况下,剩余的缓冲能用于读取响应,部分响应可能被读取缓冲到临时文件。默认情况,大小被proxy_buffer_sizeproxy_buffers两个指令限制。

Nginx官方原文如下:

When buffering of responses from the proxied server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read. In the mean time, the rest of the buffers can be used for reading a response and, if needed, buffering part of a response to a temporary file. By default, size is limited by two buffers set by the proxy_buffer_size and proxy_buffers directives.

6. proxy_connect_timeout

syntax:

proxy_connect_timeout    time;

default:

proxy_connect_timeout  60s;

context:

http, server, location

定义同被代理服务器建立连接超时时间。应当指出,这个超时时间可以通常不超过75秒。

Nginx官方原文如下:

Defines a timeout for establishing a connection with the proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.


这个时间并不是指服务器传回页面的时间(这个时间由proxy_read_timeout声明)。如果你的前端代理服务器是正常运行的,但是遇到一些状况(例如没有足够的线程去处理请求,请求将被放在一个连接池中延迟处理),那么这个声明无助于服务器去建立连接。
可以通过指定时间单位以免引起混乱,支持的时间单位有”s”(), “ms”(毫秒), “y”(), “M”(), “w”(), “d”(), “h”(小时),“m”(分钟)
这个值不能大于597小时。

7. proxy_cookie_domain

syntax:

proxy_cookie_domain  off;
proxy_cookie_domain   domainreplacement;

default:

proxy_cookie_domain off;

context:

http, server, location

该指令出现在1.1.15版本。

将一个被代理的服务器响应头中cookie字段(set-cookie)中doamin属性部分文本用指定文本替换。指令支持被代理服务器返回的带有“domain=localhost"属性的"set-cookie"形式的header头。下面的指令

proxy_cookie_domain localhost example.org;

将重写domain=example.org属性

当英文句点开始(.xxxx)域名及替换字符串,这域名属性被忽略,匹配不区分大小写

域名及替换字符串可以包括变量,例如:

proxy_cookie_domain www.$host $host;

指令也能包括正则表达式,在这个情况下域名应该用"~"符号开始。正则表达式可以包含命名和位置捕获,并更换可以参考:

proxy_cookie_domain ~\.(?P<sl_domain>[-0-9a-z]+\.[a-z]+)$ $sl_domain;


可能有几个proxy_cookie_domain指令同时存在

proxy_cookie_domain localhost example.org;

proxy_cookie_domain ~\.([a-z]+\.[a-z]+)$ $1;


off参数在当前基本的设置中取消所有proxy_cookie_domain的指令
proxy_cookie_domain off;

proxy_cookie_domain localhost example.org;

proxy_cookie_domain www.example.org example.org;

Nginx官方原文如下:

This directive appeared in version 1.1.15.

Sets a text that should be changed in the domain attribute of the “Set-Cookie” header fields of a proxied server response. Suppose a proxied server returned the header field “Set-Cookie” with the attribute “domain=localhost”. The directive

proxy_cookie_domain localhost example.org;

will rewrite this attribute to “domain=example.org”.

A dot at the beginning of the domain and replacement strings, and the domain attribute is ignored. Matching is case-insensitive.

The strings domain and replacement can contain variables:

proxy_cookie_domain www.$host $host;

A directive can also be specified using regular expressions. In this case, domain should start from the “~” symbol. A regular expression can contain named and positional captures, and replacement can reference them:

proxy_cookie_domain ~\.(?P<sl_domain>[-0-9a-z]+\.[a-z]+)$ $sl_domain;

There could be several proxy_cookie_domain directives:

proxy_cookie_domain localhost example.org;

proxy_cookie_domain ~\.([a-z]+\.[a-z]+)$ $1;

The off parameter cancels all proxy_cookie_domain directives on the current level:

proxy_cookie_domain off;

proxy_cookie_domain localhost example.org;

proxy_cookie_domain www.example.org example.org;

8. proxy_cookie_path 

syntax:

proxy_cookie_path off;
proxy_cookie_path pathreplacement;

default:

proxy_cookie_path off;

context:

http, server, location

该指令出现在1.1.15版本。

将一个被代理的服务器响应头中cookie字段(set-cookie)中path属性部分文本用指定文本替换。指令支持被代理服务器返回的带有“path=/two/some/uri/"属性的"set-cookie"形式的header头。下面的指令

proxy_cookie_path /two/ /;

将重写“path=/some/uri/”属性

path及替换字符串可以包括变量,例如:

proxy_cookie_path $uri /some$uri;

指令也能包括正则表达式,在这个情况下域名应该用"~"符号开始。正则表达式可以包含命名和位置捕获,并更换可以参考:

proxy_cookie_path ~*^/user/([^/]+) /u/$1;

    可能有几个proxy_cookie_path指令同时存在

proxy_cookie_path /one/ /;

proxy_cookie_path / /two/;

off参数在当前基本的设置中取消所有proxy_cookie_path的指令
    proxy_cookie_path off;

proxy_cookie_path /two/ /;

proxy_cookie_path ~*^/user/([^/]+) /u/$1;

Nginx官方原文如下:

This directive appeared in version 1.1.15.

Sets a text that should be changed in the path attribute of the “Set-Cookie” header fields of a proxied server response. Suppose a proxied server returned the header field “Set-Cookie” with the attribute “path=/two/some/uri/”. The directive

proxy_cookie_path /two/ /;

will rewrite this attribute to “path=/some/uri/”.

The strings path and replacement can contain variables:

proxy_cookie_path $uri /some$uri;

A directive can also be specified using regular expressions. In this case, path should either start from the “~” symbol for a case-sensitive matching, or from the “~*” symbols for case-insensitive matching. A regular expression can contain named and positional captures, and replacement can reference them:

proxy_cookie_path ~*^/user/([^/]+) /u/$1;

There could be several proxy_cookie_path directives:

proxy_cookie_path /one/ /;

proxy_cookie_path / /two/;

The off parameter cancels all proxy_cookie_path directives on the current level:

proxy_cookie_path off;

proxy_cookie_path /two/ /;

proxy_cookie_path ~*^/user/([^/]+) /u/$1;



快乐成长 每天进步一点点