proxy_pass
ngx_http_proxy_module
模块运行将请求转发到其他服务器。
配置示例
location / { proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_redirect off;
proxy_hide_header Vary;
}
指令
proxy_pass
URL
;
—
location
, if in location
, limit_except
Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http
” or “https
” can be specified. The address can be specified as a domain name or IP address, and an optional port: 设置一个代理服务器的协议和地址,以及一个可选的可以映射到location的uri。地址可以是域名或者IP地址,以及一个可选的端口:
proxy_pass http://localhost:8000/uri/;
or as a UNIX-domain socket path specified after the word “unix
” and enclosed in colons:
或者一个以unix开头用冒号连接的unix域套接字路径:
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. 如果一个域名解析到多个地址,所有这些地址都会以轮转的方式被使用。此外,一个地址也可以被指示为一个服务组。
Parameter value can contain variables. In this case, if an address is specified as a domain name, the name is searched among the described server groups, and, if not found, is determined using a resolver. 参数值可以包含变量。在这里,如果一个地址被指明为一个域名,这个域名会在所描述的服务组中查询,如果没找到,则会使用一个解析器。
A request URI is passed to the server as follows:
- If the
proxy_pass
directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive: 如果 proxy_pass 指令带了uri,那么当一个请求转到服务器时,命中 location 的请求URI部分将会被指令中所指定的uri所替换。如下, 命中 /name/部分的原uri 将被替换成 /remote/
location /name/ { proxy_pass http://127.0.0.1/remote/;
}
- If
proxy_pass
is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI: 如果 proxy_pass 没有指明uri,转发到这个服务器的请求的uri会和客户端发送的原始请求的uri一样,或者当处理已变更的uri时,将会传递完整的标准请求uri。
location /some/path/ { proxy_pass http://127.0.0.1;
}
Before version 1.1.12, if
proxy_pass
is specified without a URI, the original request URI might be passed instead of the changed URI in some cases.
In some cases, the part of a request URI to be replaced cannot be determined: 在某些情况下,将被替换掉的请求uri的部分将不会被检测到:
- When location is specified using a regular expression, and also inside named locations. In these cases,
proxy_pass
should be specified without a URI. 当 location 是使用正则表达式指明,以及内部命名的location. 在这些情况下,proxy_pass 在应该不带上uri. - When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (
break
): 当 uri 被内部的 使用rewrite指令的 转发location 改变时,同样的配置将会用来处理这个请求。
location /name/ { rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1;
}
In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server. 在这种情况下,在指令中指明的uri被忽略了,然后全部的更改后的uri被传递给了 server.
- When variables are used in
proxy_pass
: 当proxy_pass内部使用了变量
In this case, if URI is specified in the directive, it is passed to the server as is, replacing the original request URI. 在这种情况下,在指令中被指明的uri,会按这个样式转发到服务器,替代原始的请求uri.location /name/ {
proxy_pass http://127.0.0.1$request_uri;
}
WebSocket proxying requires special configuration and is supported since version 1.3.13.
proxy_buffers
number
size
;
proxy_buffers 8 4k|8k;
http
, server
, location
Sets the number
and size
of the 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. 设置用来从代理的服务器读取响应时的 缓冲 number 和 size. 默认的, 缓冲大小等于一个内存页。要么 4K 要么 8K,取决于平台。
proxy_buffer_size
size
;
proxy_buffer_size 4k|8k;
http
, server
, location
Sets the size
of the buffer used for reading the first part of the response received from the proxied server. This part usually contains a small response header. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform. It can be made smaller, however. 设置用来读取从代理服务器处接收到的响应的第一部分时的缓冲大小。这部分通常包含一个小的响应头。默认地,这个缓冲大小等于一个内存页,4K或者8K,取决于平台,也可以设得更小。
proxy_busy_buffers_size
size
;
proxy_busy_buffers_size 8k|16k;
http
, server
, location
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 meantime, the rest of the buffers can be used for reading the response and, if needed, buffering part of the response to a temporary file. By default, size
is limited by the size of two buffers set by the proxy_buffer_size and proxy_buffers directives. 当给来自代理服务器的响应缓冲被打开时,当响应还没有完全读取的时候,限制缓冲的总 size 可能忙于给客户端发送响应。同时,缓冲的剩余部分可以用来读取响应,而且,如果需要的话,将响应的一部分缓冲到一个临时文件。默认地,size 被 proxy_buffer_size 和 proxy_buffers 指令所设置的缓存所限制。
proxy_redirect
default
;proxy_redirect
off
;proxy_redirect
redirect
replacement
;
proxy_redirect default;
http
, server
, location
Sets the text that should be changed in the “Location” and “Refresh” header fields of a proxied server response. 设置 一个被代理服务器的响应的 Location 和 Refresh 头字段中应该被更改的文本。Suppose a proxied server returned the header field “Location: http://localhost:8000/two/some/uri/
”.
The directive will rewrite this string to “Location: http://frontend/one/some/uri/
”.
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
假设一个被代理服务器返回的头字段是 Location: http://localhost:8000/two/some/uri/,
上面这个指令会将它重写成: Location: http://frontend/one/some/uri/。
A server name may be omitted in the replacement
string:
proxy_redirect http://localhost:8000/two/ /;
then the primary server’s name and port, if different from 80, will be inserted.
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/ /;
The directive can be specified (1.1.11) using regular expressions. In this case, redirect
should either start with the “~
” symbol for a case-sensitive matching, or with the “~*
” symbols for case-insensitive matching. The 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 the effect of 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 / /;
proxy_hide_header
field
;
—
http
, server
, location
By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-...” from the response of a proxied server to a client. The proxy_hide_header
directive sets additional fields that will not be passed. If, on the contrary, the passing of fields needs to be permitted, the proxy_pass_header directive can be used.
proxy_set_header
field
value
;
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
http
, server
, location
Allows redefining or appending fields to the request header passed to the proxied server. The value
can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header
directives defined on the current level. By default, only two fields are redefined:
proxy_set_header Host $proxy_host;proxy_set_header Connection close;
If caching is enabled, the header fields “If-Modified-Since”, “If-Unmodified-Since”, “If-None-Match”, “If-Match”, “Range”, and “If-Range” from the original request are not passed to the proxied server.
An unchanged “Host” request header field can be passed like this:
proxy_set_header Host $http_host;
However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the $host
variable - its value equals the server name in the “Host” request header field or the primary server name if this field is not present:
proxy_set_header Host $host;
In addition, the server name can be passed together with the port of the proxied server:
proxy_set_header Host $host:$proxy_port;
If the value of a header field is an empty string then this field will not be passed to a proxied server:
proxy_set_header Accept-Encoding "";
以上是 proxy_pass 的全部内容, 来源链接: utcz.com/z/511542.html