先看问题

开发反馈有个接口请求一直是502,介入后查看nginx error log 展示信息如下:

upstream sent too big header while reading response header from upstream

意思是上游服务返回的响应携带的头信息太大了, 超过了配置的缓冲区,导致读取响应超时, nginx直接返回502

如何解决

  • 可以在主配置文件中添加proxy buffer 相关参数,如下
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

如果直接加大缓冲区,大概率也可以解决当前的问题,但也许不是最优的解决方案。

此时可以调用异常的接口,查看它的response header 看看是否存在异常, 是否是因为业务代码逻辑问题导致头信息异常增加

所以,上述的这种问题不应该通过增大缓冲区来处理,而是需要找到对应的业务负责人来修复这种逻辑问题才是根本的解决方案

备注:

proxy_buffer_size 用于设置 upstream的header的缓存.
proxy_buffers 用于设置 upstream的response的body(可能不准确)的缓存个数与大小.
proxy_busy_buffers_size 用于设置可用于处于发送busy状态的缓存区大小. 其值最小值必须大于等于 max(proxy_buffer_size,proxy_buffers one buffer size),且必须小于 (proxy_buffers-1)*size

记录一次nginx大量502问题处理_缓存

proxy_buffer_size

一句话总结: 用于缓冲 upstream_server 返回的第一部分, 即: Header

Syntax:

proxy_buffer_size size;

Default:

proxy_buffer_size 4k|8k;

Context:

httpserverlocation

Sets the <i>size</i> 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.

翻译:
设置缓冲区的大小用来读取从代理服务器收到的响应的第一部分。这部分通常包含一个小的响应头(Header)。默认情况下,此缓冲区大小等于1个page。这是4 k或8 k,取决于平台。然而,它可以做得更小。

proxy_buffering

一句话总结: 用于缓存upstream_server的 返回的response body的开关. 使能或者禁止.

Syntax:

proxy_buffering on | off;

Default:

proxy_buffering on;

Context:

httpserverlocation

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 the buffers set by the proxy_buffer_size and proxy_buffers directives. If the whole response does not fit into memory, a part of it can be saved to a temporary file on the disk. Writing to temporary files is controlled by the proxy_max_temp_file_size and proxy_temp_file_write_size directives.


When buffering is disabled, the 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 capability can be disabled using the proxy_ignore_headers directive.

翻译:

开启或者禁止 被代理服务器的Reponse缓存功能.

当缓冲被启用时,nginx会尽快接收到来自代理服务器的响应,并将其保存到proxy_buffer_sizeproxy_buffers指令设置的缓冲区中。如果整个响应不能完整装入内存,则可以将其中一部分保存到磁盘上的临时文件中。写入临时文件由proxy_max_temp_file_sizeproxy_temp_file_write_size指令控制。

缓冲被禁用时,response在收到后立即同步传递给客户端。Nginx不会尝试从代理服务器读取整个响应。nginx一次可以从服务器接收的数据的最大大小是由proxy_buffer_size指令设置的.


缓冲也可以通过在“x - accelerate -Buffering”响应报头字段中传递“yes”或“no”来启用或禁用。可以使用proxy_ignore_headers指令禁用此功能。

proxy_buffers

一句话总结: 用于控制 upstream_server 的response_body 的缓存配置. 且是针对每一个链接, 配置缓存数量和缓存大小.

Syntax:

proxy_buffers number size;

Default:

proxy_buffers 8 4k|8k;

Context:

httpserverlocation

Sets the <i>number</i> and <i>size</i> 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.

翻译:
为单个连接设置用于从代理服务器读取响应的缓冲区的数量和大小。默认情况下,缓冲区大小等于一个内存页。这是4K或8K,取决于平台。

proxy_busy_buffers_size

一句话总结. 用于控制用于缓存upstream_server的缓存request_body 的缓冲区的整体数量中,可同时用于发送busy的缓冲区size. 剩余的可用于接收upstream的response body.

Syntax:

proxy_busy_buffers_size size;

Default:

proxy_busy_buffers_size 8k|16k;

Context:

httpserverlocation

When buffering of responses from the proxied server is enabled, limits the total <i>size</i> 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, <i>size</i> is limited by the size of two buffers set by the proxy_buffer_size and proxy_buffers directives.

翻译:
当启用了代理服务器的响应缓冲时,限制在响应尚未完全读取时忙于向客户端发送响应的缓冲区的总大小。与此同时,其余缓冲区可用于读取响应,如果需要,还可将部分响应缓冲到临时文件中。默认情况下,大小受proxy_buffer_sizeproxy_buffers指令设置的两个缓冲区的大小限制。