Nginx proxy基本指令整理3

  categories:nginx  author:

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

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

proxy_max_temp_file_size,proxy_method,proxy_next_upstream,proxy_pass,proxy_pass_header

17. proxy_max_temp_file_size

syntax:

proxy_max_temp_file_size    size;

default:

proxy_max_temp_file_size 1024m;

context:

http, server, location

当从被代理的服务器接收的响应的缓冲被允许的时侯,当整个响应不合适存储到内存中时,有一部分数据将被存储到磁盘文件中,内存存储buffer的设置是根据proxy_buffer_sizeproxy_buffers指令进行设置。proxy_max_temp_file_size指令设置最大的临时文件存储的尺寸。数据在写入临时文件中,一次写入多少数据是通过 proxy_temp_file_write_size 进行设置的。当proxy_max_temp_file_size指令的数值是0时关闭响应数据存储到临时文件的功能。

When buffering of responses from the proxied server is enabled, and the whole response does not fit into memory buffers set by the proxy_buffer_size and proxy_buffers directives, part of a response can be saved to a temporary file. This directive sets the maximum size of a temporary file. The size of data written to a temporary file at a time is set by the proxy_temp_file_write_size directive.

Value of zero disables buffering of responses to temporary files.

18. proxy_method

syntax:

proxy_method   method;

default:

context:

http, server, location

指定的HTTP请求转发到代理服务器的方法代替客户端请求中使用的方法。

Specifies the HTTP method to use in requests forwarded to the proxied server instead of the method from the client request.

19. proxy_next_upstream

syntax:

proxy_next_upstream  error | timeout | invalid_header

                     | http_500 | http_502 | http_503

                    | http_504 | http_404 | off …;

default:

proxy_next_upstream error timeout;

context:

http, server, location

指定在那种情况下请求应该被传递到下一个服务器上,包括以下情况:

Error

nginx同被代理的服务器建立连接,传送请求,或者读取一个响应过程中发生错误

Timeout

nginx同被代理的服务器建立连接,传送请求,或者读取一个响应过程中发生超时事件

invalid_header

当被代理的服务器返回空的或者非法的响应时

http_500

当被代理的服务器返回500响应码时

http_503

当被代理的服务器返回503响应码时

http_504

当被代理的服务器返回504响应码时

http_404

当被代理的服务器返回404响应码时

off

禁止传递请求到下一个服务器

需要注意的是:在nginx还没有传递数据给客户端的过程中发生错误,nginx可以通过这个指令将请求等转发给下一个服务器。但是如果错误发生在nginx发送响应给客户端过程中,这个转发是不可能的。

It should be understood that passing a request to the next server is only possible if a client was not sent anything yet. That is, if an error or a timeout occurs in the middle of transferring a response, fixing this is impossible.

Specifies in which cases a request should be passed to the next server:

error

an error occurred while establishing a connection with the server, passing it a request, or reading the response header;

timeout

a timeout has occurred while establishing a connection with the server, passing it a request, or reading the response header;

invalid_header

a server returned empty or invalid response;

http_500

a server returned a response with the code 500;

http_502

a server returned a response with the code 502;

http_503

a server returned a response with the code 503;

http_504

a server returned a response with the code 504;

http_404

a server returned a response with the code 404;

off

disables passing a request to the next server.

It should be understood that passing a request to the next server is only possible if a client was not sent anything yet. That is, if an error or a timeout occurs in the middle of transferring a response, fixing this is impossible.

20. proxy_pass 

syntax:

proxy_pass  URL;

default:

context:

location, if in location, limit_except

设置被代理服务器的协议和地址,一个可选的uri来确定那个location应该被选中。协议可以被指定为http或者https。地址能被指定为域名或者ip地址,端口号可选。

proxy_pass http://localhost:8000/uri/;

或者采用unixdomain socket规范的地址,

proxy_pass http://unix:/tmp/backend.socket:/uri/;

如果一个域名被解析成多个ip地址,则这些ip地址将被循环使用,另外还可以使用server组(反向代理的方式)作为代理地址。

如果一个请求的URI被传递给server(被代理的服务器),将遵照下面规则:

如果proxy_pass指令携带有URI,当请求传递给被代理的服务器时,那些同location指令匹配的经过规范化处理后的uri将被指令中的相关部分替代。

location /name/ {

    proxy_pass http://127.0.0.1/remote/;

}

(若一个请求中 /name/部分 将被 /remote/替代)

如果proxy_pass指令没有带有uri, 那么一个请求的uri将保留客户端原始(没有经过任何修改的,包括规格化,(这个没有去验证))请求中相同形式传递给被代理的server。或者当处理变化后的uri时,一个完整的规范化的uri将被传递给被代理的服务器。

location /some/path/ {

    proxy_pass http://127.0.0.1;

}

1.1.12版本以前, 如果proxy_pass 指令没有包括uri, 在某些情况下一个原始请求的uri可能代替变换后的uri而传送给服务器。

在某些情况下,请求uri的一部分被替换了但却不能比检测到。

当一个location使用正则表达式制定的时侯,在这个情况下本指令应该不使用带有uri形式。

uri在代理指令出现的location中时,使用rewrite指令,这种情况下相同配置将被用于处理一个请求,例如:

location /name/ {

    rewrite    /name/([^/]+) /users?name=$1 break;

    proxy_pass http://127.0.0.1;

}

(例子中将传递给server的是 :/users?name=xxxx)这个参数。

在这种情况下,该指令中指定的URI被忽略,并且一个完整改变后的请求URI传递给服务器。

一个server名称,端口号以及uri也能被通过变量指定,例如:proxy_pass http://$host$uri;

甚至:proxy_pass $request;

在这种情况下,服务器名称搜索在配置的服务器中间进行, 如果没有找到确定使用解析器。

Sets the protocol and address of a proxied server, and an optional URI to which a location should be mapped. A protocol can be specified as “http” or “https”. An address can be specified as a domain name or IP address, and an optional port:

proxy_pass http://localhost:8000/uri/;

or as a UNIX-domain socket path specified after the word “unix” and enclosed in colons:

proxy_pass http://unix:/tmp/backend.socket:/uri/;

If a domain name resolves to several addresses, all of them will be used in a round-robin fashion. In addition, an address can be specified as a server group.

A request URI is passed to the server as follows:

If proxy_pass is specified with URI, when passing a request to the server, part of a normalized request URI matching the location is replaced by a URI specified in the directive:

location /name/ {

    proxy_pass http://127.0.0.1/remote/;

}


If proxy_pass is specified without URI, a request URI is passed to the server in the same form as sent by a client when processing an original request, or the full normalized request URI is passed when processing the changed URI:

location /some/path/ {

    proxy_pass http://127.0.0.1;

}

Before version 1.1.12, if proxy_pass is specified without a URI, an original request URI might be passed instead of the changed URI in some cases.

In some cases, part of a request URI to be replaced cannot be determined:

When location is specified using a regular expression.

In this case, the directive should be specified without URI.

When URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break):

location /name/ {

    rewrite    /name/([^/]+) /users?name=$1 break;

    proxy_pass http://127.0.0.1;

}

In this case, a URI specified in the directive is ignored and the full changed request URI is passed to the server.

A server name, its port and passed URI can also be specified using variables:

proxy_pass http://$host$uri;

or even like this:

proxy_pass $request;

In this case the server name is searched among the described server groups, and if not found is determined using a resolver.

21. proxy_pass_header 

syntax:

proxy_pass_header   field;

default:

context:

http, server, location

设定允许通过从代理服务器到客户端的报头字段。

Permits to pass otherwise disabled header fields from a proxied server to a client.



快乐成长 每天进步一点点