Skip to content

Commit 02a900c

Browse files
committed
Update proxy module
1 parent 06ca95b commit 02a900c

File tree

1 file changed

+311
-10
lines changed

1 file changed

+311
-10
lines changed

模块参考/http/ngx_http_proxy_module.md

Lines changed: 311 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,6 @@ server {
410410

411411
要在填充新缓存元素时最大化减少对代理服务器的访问次数,可以使用 [proxy_cache_lock](#proxy_cache_lock) 指令。
412412

413-
414-
415-
416-
417-
418-
419-
420-
421-
422-
423413
### proxy_cache_valid
424414

425415
|\-|说明|
@@ -516,6 +506,317 @@ proxy_cookie_path /two/ /;
516506
proxy_cookie_path ~*^/user/([^/]+) /u/$1;
517507
```
518508

509+
### proxy_force_ranges
510+
511+
|\-|说明|
512+
|------:|------|
513+
|**语法**|**proxy_force_ranges** `on` | `off`;|
514+
|**默认**|proxy_force_ranges off;|
515+
|**上下文**|http、server、location|
516+
|**提示**|该指令在 1.7.7 版本中出现|
517+
518+
无论代理服务器中的 **Accept-Ranges** 字段如何,对代理服务器的缓存和未缓存响应都启用字节范围(byte-range)支持。
519+
520+
### proxy_force_ranges
521+
522+
|\-|说明|
523+
|------:|------|
524+
|**语法**|**proxy_headers_hash_bucket_size** `size`;|
525+
|**默认**|proxy_headers_hash_bucket_size 64;|
526+
|**上下文**|http、server、location|
527+
528+
设置 [proxy_hide_header](#proxy_hide_header)[proxy_set_header](#proxy_set_header) 指令使用的哈希表的桶 `size`。设置哈希表的详细介绍在[单独的文档](../../介绍/设置哈希.md)中提供。
529+
530+
### proxy_headers_hash_max_size
531+
532+
|\-|说明|
533+
|------:|------|
534+
|**语法**|**proxy_headers_hash_max_size** `size`;|
535+
|**默认**|proxy_headers_hash_max_size 512;|
536+
|**上下文**|http、server、location|
537+
538+
设置 [proxy_hide_header](#proxy_hide_header)[proxy_set_header](#proxy_set_header) 指令使用的哈希表的最大 `size`。设置哈希表的详细介绍在[单独的文档](../../介绍/设置哈希.md)中提供。
539+
540+
### proxy_hide_header
541+
542+
|\-|说明|
543+
|------:|------|
544+
|**语法**|**proxy_hide_header** `field`;|
545+
|**默认**|——|
546+
|**上下文**|http、server、location|
547+
548+
默认情况下,nginx 不会从代理服务器向客户端的响应中传递头字段 **Date****Server****X-Pad****X-Accel-...**`proxy_hide_header` 指令设置不传递的其他字段。相反,如果需要允许传递字段,则可以使用 [proxy_pass_header](#proxy_pass_header) 指令设置。
549+
550+
### proxy_http_version
551+
552+
|\-|说明|
553+
|------:|------|
554+
|**语法**|**proxy_http_version** `1.0` | `1.1`;|
555+
|**默认**|proxy_http_version 1.0;|
556+
|**上下文**|http、server、location|
557+
|**提示**|该指令在 1.1.4 版本中出现|
558+
559+
设置代理的 HTTP 协议版本。默认情况下,使用 1.0 版本。建议将 1.1 版与 [keepalive](ngx_http_upstream_module.md#keepalive) 连接和 [NTLM 身份验证](ngx_http_upstream_module.md#ntlm)配合使用。
560+
561+
### proxy_ignore_client_abort
562+
563+
|\-|说明|
564+
|------:|------|
565+
|**语法**|**proxy_ignore_client_abort** `on` | `off`;|
566+
|**默认**|proxy_ignore_client_abort off;|
567+
|**上下文**|http、server、location|
568+
569+
确定在客户端关闭连接不等待响应时是否应关闭与代理服务器的连接。
570+
571+
### proxy_ignore_headers
572+
573+
|\-|说明|
574+
|------:|------|
575+
|**语法**|**proxy_ignore_headers** `field ...`;|
576+
|**默认**|——|
577+
|**上下文**|http、server、location|
578+
579+
禁用处理某些来自代理服务器的响应头字段。可以忽略以下字段:**X-Accel-Redirect****X-Accel-Expires****X-Accel-Limit-Rate**(1.1.6)、**X-Accel-Buffering**(1.1.6)、**X-Accel-Charset**(1.1.6)、**Expires****Cache-Control****Set-Cookie**(0.8.44)和 **Vary**(1.7.7)。
580+
581+
如果未禁用,处理这些头字段将会产生以下影响:
582+
583+
- **X-Accel-Expires****Expires****Cache-Control****Set-Cookie****Vary** 设置响应[缓存](#proxy_cache_valid)的参数
584+
- **X-Accel-Redirect** 执行[内部重定](ngx_http_core_module.md#internal)向到指定的 URI
585+
- **X-Accel-Limit-Rate** 设置向客户端传输响应的[速率限制](ngx_http_core_module.md#limit_rate)
586+
- **X-Accel-Buffering** 启用或禁用响应[缓冲](#proxy_buffering)
587+
- **X-Accel-Charset** 设置了所需的响应[字符集](ngx_http_charset_module.md#charset)
588+
589+
### proxy_intercept_errors
590+
591+
|\-|说明|
592+
|------:|------|
593+
|**语法**|**proxy_intercept_errors** `on` | `off`;|
594+
|**默认**|proxy_intercept_errors off;|
595+
|**上下文**|http、server、location|
596+
597+
确定状态码大于或等于 300 的代理响应是应该传递给客户端还是拦截并重定向到 nginx 以便使用 [error_page](ngx_http_core_module.md#error_page) 指令进行处理。
598+
599+
### proxy_limit_rate
600+
601+
|\-|说明|
602+
|------:|------|
603+
|**语法**|**proxy_limit_rate** `rate`;|
604+
|**默认**|proxy_limit_rate 0;|
605+
|**上下文**|http、server、location|
606+
|**提示**|该指令在 1.7.7 版本中出现|
607+
608+
限制从代理服务器读取响应的速率。`rate` 以每秒字节数指定。零值则禁用速率限制。限制是针对每个请求,因此如果 nginx 同时打开两个到代理服务器的连接,则总速率将是指定限制的两倍。仅当启用了代理服务器的响应[缓冲](#proxy_buffering)时,此限制才生效。
609+
610+
### proxy_max_temp_file_size
611+
612+
|\-|说明|
613+
|------:|------|
614+
|**语法**|**proxy_max_temp_file_size** `size`;|
615+
|**默认**|proxy_max_temp_file_size 1024m;|
616+
|**上下文**|http、server、location|
617+
618+
当启用[缓冲](#proxy_buffering)来自代理服务器的响应,并且整个响应不符合 [proxy_buffer_size](#proxy_buffer_size)[proxy_buffers](#proxy_buffers) 指令设置的缓冲时,部分响应可以保存到临时文件中。该指令设置临时文件的最大 `size`。一次写入临时文件的数据大小由 [proxy_temp_file_write_size](#proxy_temp_file_write_size) 指令设置。
619+
620+
零值则禁止将缓冲响应写入临时文件。
621+
622+
> 此限制不适用于将要[缓存](#proxy_cache)[存储](#proxy_store)在磁盘上的响应。
623+
624+
### proxy_method
625+
626+
|\-|说明|
627+
|------:|------|
628+
|**语法**|**proxy_method** `method`;|
629+
|**默认**|——|
630+
|**上下文**|http、server、location|
631+
632+
指定转发到代理服务器的请求使用的 HTTP 方法(`method`),而不是使用来自客户端请求的方法。参数值可以包含变量(1.11.6)。
633+
634+
### proxy_next_upstream
635+
636+
|\-|说明|
637+
|------:|------|
638+
|**语法**|**proxy_next_upstream** `error` | `timeout` | `invalid_header` | `http_500` | `http_502` | `http_503` | `http_504` | `http_403` | `http_404` | `http_429` | `non_idempotent` | `off ...`;|
639+
|**默认**|proxy_intercept_errors off;|
640+
|**上下文**|http、server、location|
641+
642+
指定应将请求传递到下一个服务器的条件:
643+
644+
- `error`
645+
646+
与服务器建立连接,向其传递请求或读取响应头时发生错误
647+
648+
- `timeout`
649+
650+
在与服务器建立连接,向其传递请求或读取响应头时发生超时
651+
652+
- `invalid_header`
653+
654+
服务器返回空或无效响应
655+
656+
- `http_500`
657+
658+
服务器返回代码为 500 的响应
659+
660+
- `http_502`
661+
662+
服务器返回代码为 502 的响应
663+
664+
- `http_503`
665+
666+
服务器返回代码为 503 的响应
667+
668+
- `http_504`
669+
670+
服务器返回代码 504 的响应
671+
672+
- `http_403`
673+
674+
服务器返回代码为 403 的响应
675+
676+
- `http_404`
677+
678+
服务器返回代码为 404 的响应
679+
680+
- `http_429`
681+
682+
服务器返回代码为 429 的响应(1.11.13)
683+
684+
- `non_idempotent`
685+
686+
通常,如果请求已发送到上游服务器,则使用[非幂等](https://tools.ietf.org/html/rfc7231#section-4.2.2)方法(`POST``LOCK``PATCH`)的请求不会传递给下一个服务器(1.9.13),启用此选项显式允许重试此类请求
687+
688+
- `off`
689+
690+
禁止将请求传递给下一个服务器
691+
692+
应该记住,只有在尚未向客户端发送任何内容的情况下,才能将请求传递给下一个服务器。也就是说,如果在传输响应的过程中发生错误或超时,则无法修复此问题。
693+
694+
该指令还定义了与服务器通信的[失败尝试](ngx_http_upstream_module.md#max_fails)`error``timeout``invalid_header` 的情况始终被视为失败尝试,即使它们未在指令中指定。`http_500``http_502``http_503``http_504``http_429` 的情况仅在指令中指定时才被视为失败尝试。`http_403``http_404` 的情况不会被视为失败尝试。
695+
696+
将请求传递到下一个服务器可能会受到[尝试次数](#proxy_next_upstream_tries)[时间](#proxy_next_upstream_timeout)的限制。
697+
698+
### proxy_next_upstream_timeout
699+
700+
|\-|说明|
701+
|------:|------|
702+
|**语法**|**proxy_next_upstream_timeout** `time`;|
703+
|**默认**|proxy_next_upstream_timeout 0;|
704+
|**上下文**|http、server、location|
705+
|**提示**|该指令在 1.7.5 版本中出现|
706+
707+
限制请求可以传递到[下一个服务器](#proxy_next_upstream)的时间。`0` 值关闭此限制。
708+
709+
### proxy_next_upstream_tries
710+
711+
|\-|说明|
712+
|------:|------|
713+
|**语法**|**proxy_next_upstream_tries** `number`;|
714+
|**默认**|proxy_next_upstream_tries 0;|
715+
|**上下文**|http、server、location|
716+
|**提示**|该指令在 1.7.5 版本中出现|
717+
718+
限制将请求传递到[下一个服务器](#proxy_next_upstream)的可能尝试次数。`0` 值关闭此限制。
719+
720+
### proxy_no_cache
721+
722+
|\-|说明|
723+
|------:|------|
724+
|**语法**|**proxy_no_cache** `string ...`;|
725+
|**默认**|——|
726+
|**上下文**|http、server、location|
727+
728+
定义不将响应保存到缓存的条件。如果字符串参数有一个值不为空且不等于 `0`,则不会保存响应:
729+
730+
```nginx
731+
proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;
732+
proxy_no_cache $http_pragma $http_authorization;
733+
```
734+
735+
可以与 [proxy_cache_bypass](#proxy_cache_bypass) 指令一起使用。
736+
737+
### proxy_pass
738+
739+
|\-|说明|
740+
|------:|------|
741+
|**语法**|**proxy_pass** `URL`;|
742+
|**默认**|——|
743+
|**上下文**|http、server、location|
744+
745+
设置代理服务器的协议、地址以及应映射位置的可选 URI。协议可以指定 `http``https`。可以将地址指定为域名或 IP 地址,以及一个可选端口号:
746+
747+
```nginx
748+
proxy_pass http://localhost:8000/uri/;
749+
```
750+
751+
或者在 `unix` 单词后面指定使用冒号括起来的 UNIX 域套接字路径:
752+
753+
```nginx
754+
proxy_pass http://unix:/tmp/backend.socket:/uri/;
755+
```
756+
757+
如果域名解析为多个地址,则所有这些地址将以轮询的方式使用。此外,可以将地址指定为[服务器组](ngx_http_upstream_module.md)
758+
759+
参数值可以包含变量。在这种情况下,如果将地址指定为域名,则在所描述的服务器组中搜索名称,如果未找到,则使用[解析器](ngx_http_core_module.md#resolver)确定。
760+
761+
请求 URI 按如下方式传递给服务器:
762+
763+
- 如果指定了带有 URI 的 `proxy_pass`,那么当请求传递给服务器时,与该位置(location)匹配的[规范化](ngx_http_core_module.md#location)请求 URI 的部分将被指令中指定的 URI 替换:
764+
765+
766+
```nginx
767+
location /name/ {
768+
proxy_pass http://127.0.0.1/remote/;
769+
}
770+
```
771+
772+
- 如果指定了没有 URI 的 `proxy_pass`,则请求 URI 将以与处理原始请求时客户端发送的格式相同的形式传递给服务器,或者在处理更改的 URI 时传递完整的规范化请求 URI:
773+
774+
```nginx
775+
location /some/path/ {
776+
proxy_pass http://127.0.0.1;
777+
}
778+
```
779+
780+
> 在 1.1.12 版本之前,如果指定了没有 URI 的 `proxy_pass`,则在某些情况下可能会传递原始请求 URI 而不是更改 URI。
781+
782+
在某些情况下,无法确定请求 URI 要替换的部分:
783+
784+
- 使用正则表达式指定 location (位置)时,以及在命名位置内指定位置。
785+
在这些情况下,应指定 `proxy_pass` 而不使用 URI。
786+
787+
- 使用 [rewrite](ngx_http_rewrite_module.md#rewrite) 指令在代理位置内更改 URI 时,将使用相同的配置来处理请求(`break`):
788+
789+
```nginx
790+
location /name/ {
791+
rewrite /name/([^/]+) /users?name=$1 break;
792+
proxy_pass http://127.0.0.1;
793+
}
794+
```
795+
796+
在这种情况下,将忽略指令中指定的 URI,并将完整更改的请求 URI 传递给服务器。
797+
798+
- 在 proxy_pass 中使用变量时:
799+
800+
```nginx
801+
location /name/ {
802+
proxy_pass http://127.0.0.1$request_uri;
803+
}
804+
```
805+
806+
在这种情况下,如果在指令中指定了 URI,则将其原样传递给服务器,替换原始请求 URI。
807+
808+
[WebSocket](../../How-To/WebSocket代理.md) 代理需要特殊配置,从 1.3.13 版本开始支持。
809+
810+
### proxy_pass_header
811+
812+
|\-|说明|
813+
|------:|------|
814+
|**语法**|**proxy_pass_header** `field`;|
815+
|**默认**|——|
816+
|**上下文**|http、server、location|
817+
818+
允许将[已禁用](#proxy_hide_header)的头字段从代理服务器传递到客户端。
819+
519820
**待续……**
520821
521822
## 原文档

0 commit comments

Comments
 (0)