Nginx proxy基本指令整理4

  categories:nginx  author:

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

本文主要针对nginx的几个主要proxy指令做一个简单的整理,另外部分指令做了实际测试,本文具体包括如下指令:

proxy_read_timeout,proxy_pass_request_body,proxy_pass_request_headers,proxy_redirect,proxy_send_lowat

22. proxy_read_timeout 

syntax:

proxy_read_timeout        time;

default:

proxy_read_timeout         60s;

context:

http, server, location

定义一个从被代理服务器读取数据的超时时间.超时时间仅仅是两次成功的读取操作的间隔时间,但是不计算整个响应的传输时间.如果被代理服务器在这个时间内没有任何响应就关闭这个连接.

Nginx原文:

Defines a timeout for reading a response from the proxied server. A timeout is only set between two successive read operations, not for the transmission of the whole response. If a proxied server does not transmit anything within this time, a connection is closed.

23. proxy_pass_request_body

syntax:

proxy_pass_request_body    on | off;

default:

proxy_pass_request_body on;

context:

http, server, location

如果禁止,原始的请求中body部分将不会转递给被代理的服务器(仅仅在一些特定场合下才有用,看下面的例子).

location /x-accel-redirect-here/ {

    proxy_method GET;

    proxy_pass_request_body off;

    proxy_set_header Content-Length "";

    proxy_pass …

}

请参阅的proxy_set_headerproxy_pass_request_headers的指令。

Nginx原文:

If disabled, the original request body will not be passed to the proxied server.

location /x-accel-redirect-here/ {

    proxy_method GET;

    proxy_pass_request_body off;

    proxy_set_header Content-Length "";

    proxy_pass …

}

See also the proxy_set_header and proxy_pass_request_headers directives.

24. proxy_pass_request_headers

syntax:

proxy_pass_request_headers    on | off;

default:

proxy_pass_request_headers on;

context:

http, server, location

如果禁止,原始的请求中header部分将不会转递给被代理的服务器(仅仅在一些特定场合下才有用,看下面的例子).

location /x-accel-redirect-here/ {

    proxy_method GET;

    proxy_pass_request_headers off;

    proxy_pass_request_body off;

    proxy_pass …

}

   请参阅的proxy_set_headerproxy_pass_request_body的指令。

Nginx原文:

If disabled, header fields of the original request will not be passed to the proxied server.

location /x-accel-redirect-here/ {

    proxy_method GET;

    proxy_pass_request_headers off;

    proxy_pass_request_body off;

    proxy_pass …

}

See also the proxy_set_header and proxy_pass_request_body directives.

25. proxy_redirect

syntax:

proxy_redirect   default;
proxy_redirect   off;
proxy_redirect   redirectreplacement;

default:

proxy_redirect default;

context:

http, server, location


设置的文本,应改为在报头字段位置刷新从代理服务器的响应。假设一个代理服务器返回的报头字段的位置:http://localhost:8000/two/some/uri/”。该指令
proxy_redirect http://localhost:8000/two/ http://frontend/one/;

设置一个应该将从被代理服务器返回数据的header域里locationrefresh中数据替换掉的文本.假设一个代理服务器返回的报头字段location:http://localhost:8000/two/some/uri/”,下面的指令

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

将改写字符串Locationhttp://frontend/one/some/uri/”这个字符串。
服务器名称替换的字符串,可以省略,例如:

proxy_redirect http://localhost:8000/two/ /;

然后主服务器的名称和端口将被替换,如果不是80端口.


本指令的默认参数下的替换字符串使用location指令(nginx指令)proxy_pass指令中响应部分.因此下面两个配置是等效的

location /one/ {

    proxy_pass     http://upstream:port/two/;

    proxy_redirect default;

location /one/ {

    proxy_pass     http://upstream:port/two/;

    proxy_redirect http://upstream:port/two/ /one/;

默认的参数是不允许的,如果proxy_pass指定使用变量。
    替换字符串可以包含变量:

proxy_redirect http://localhost:8000/ http://$host:$server_port/;

一个重定向,也可以包含变量(1.1.11):(通上面区别是变量出现的地方不一样)

proxy_redirect http://$proxy_host:8000/ /;

一个指令,可以指定使用正则表达式(1.1.11)。在这种情况下,重定向应该符号开始表示区分大小写的匹配,或*”符号开始表示不区分大小写的匹配。正则表达式可以包含命名和捕获位置,更换可以引用这些命名的捕获等
proxy_redirect ~^(http://[^:]+):\d+(/.+)$ $1$2;

proxy_redirect ~*/user/([^/]+)/(.+)$      http://$1.example.com/$2;

这里有几个可能的 proxy_redirect 指令

proxy_redirect default;

proxy_redirect http://localhost:8000/  /;

proxy_redirect http://www.example.com/ /;

Off参数取消全部的proxy_redirect指令设置在当前级别的设置中

proxy_redirect off;

proxy_redirect default;

proxy_redirect http://localhost:8000/  /;

proxy_redirect http://www.example.com/ /;

这个指令也可以用于添加主机名到一个相对跳转的情况下.
    proxy_redirect / /;

Nginx官方原文:

Sets a text that should be changed in the header fields “Location” and “Refresh” of a response from the proxied server. Suppose a proxied server returned the header field “Location: http://localhost:8000/two/some/uri/”. The directive

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

will rewrite this string to “Location: http://frontend/one/some/uri/”.

A server name may be omitted from the replacement string:

proxy_redirect http://localhost:8000/two/ /;

then the primary server’s name and a port, if different from 80, will be substituted.

The default replacement specified by the default parameter uses the parameters of the location and proxy_pass directives. Hence, the two configurations below are equivalent:

location /one/ {

    proxy_pass     http://upstream:port/two/;

    proxy_redirect default;

location /one/ {

    proxy_pass     http://upstream:port/two/;

    proxy_redirect http://upstream:port/two/ /one/;

The default parameter is not permitted if proxy_pass is specified using variables.

A replacement string can contain variables:

proxy_redirect http://localhost:8000/ http://$host:$server_port/;

A redirect can also contain (1.1.11) variables:

proxy_redirect http://$proxy_host:8000/ /;

A directive can be specified (1.1.11) using regular expressions. In this case, redirect 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_redirect ~^(http://[^:]+):\d+(/.+)$ $1$2;

proxy_redirect ~*/user/([^/]+)/(.+)$      http://$1.example.com/$2;

There could be several proxy_redirect directives:

proxy_redirect default;

proxy_redirect http://localhost:8000/  /;

proxy_redirect http://www.example.com/ /;

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

proxy_redirect off;

proxy_redirect default;

proxy_redirect http://localhost:8000/  /;

proxy_redirect http://www.example.com/ /;

Using this directive it is also possible to add host names to relative redirects issued by a proxied server:

proxy_redirect / /;

26. proxy_send_lowat 

syntax:

proxy_send_lowat   size;

default:

proxy_send_lowat 0;

context:

http, server, location

如果设置为非零值, nginx将尽力减少到被代理服务器的send请求,通过使用 NOTE_LOWATkqueue 方式下,或者 SO_SNDLOWAT放回在 socket选项下.

这个指令在linux,solariswindows下将被忽略

    Nginx官方原文:

If set to a non-zero value, nginx will try to minimize the number of send operations on outgoing connections to a proxied server by using either NOTE_LOWAT flag of the kqueue method, or the SO_SNDLOWAT socket option, with the specified size.

This directive is ignored on Linux, Solaris, and Windows.

关于SO_SNDLOWAT的网络上的解释如下:

Specify the minimum number of bytes in the buffer until the socket layer will pass the data to the protocol (SO_SNDLOWAT) or the user on receiving (SO_RCVLOWAT). These two values are not changeable in Linux and their argument size is always fixed to 1 byte. getsockopt is able to read them; setsockopt will always return ENOPROTOOPT.



快乐成长 每天进步一点点      京ICP备18032580号-1