简体   繁体   English

Nginx上游在从上游读取响应头时发送了太大的头

[英]Nginx upstream sent too big header while reading response header from upstream

I get error like this: 我得到这样的错误:

[error] 27544#0: *47335682 upstream sent too big header while reading response 
 header from upstream, client: 88.88.88.88, server: example..com,
 request: "POST /tool/ HTTP/1.1", upstream: "http://88.88.88.88:7080/tool/",
 host: "example.com"

Regarding to this question, it is possible to increase buffer size from nginx conf file like this: upstream sent too big header while reading response header from upstream 关于这个问题,可以从nginx conf文件增加缓冲区大小,如下所示: 上游从上游读取响应头时发送过大的头

http {
  proxy_buffer_size   128k;
  proxy_buffers   4 256k;
  proxy_busy_buffers_size   256k;
}

location
      fastcgi_buffers 16 16k; 
      fastcgi_buffer_size 32k;

(For future reference, default size for fastcgi_buffer_size and fastcgi_buffers is 4k or 8k, regarding to platform) (为了将来参考,fastcgi_buffer_size和fastcgi_buffers的默认大小是4k或8k,关于平台)

This text appears in user's browser: Nginx 502 Bad Gateway 此文本显示在用户的浏览器中: Nginx 502 Bad Gateway

I'm planning to temporarily increase buffer size. 我打算暂时增加缓冲区大小。 Then I can log when the buffers are too big. 然后我可以在缓冲区太大时记录。 Is it possible to find out headers which is too big for upstream ? 是否有可能找到对上游来说太大的标题? apache_response_headers() and headers_list() didn't give me all response headers. apache_response_headers()和headers_list()没有给我所有的响应头。 It only gave me expires, cache-control and pragma headers. 它只给了我expires,cache-control和pragma头文件。

Does changing proxy_buffer_size makes a performance problem ? 更改proxy_buffer_size会导致性能问题吗?

(nginx version: nginx/1.6.0, php 5.4.42, xcache 3.2) (nginx版本:nginx / 1.6.0,php 5.4.42,xcache 3.2)

You can easily find an answer on SO, but what really makes a difference is the single configuration option: 您可以在SO上轻松找到答案,但真正有所作为的是单一配置选项:

http {
  fastcgi_buffer_size 32k;
}

Nevertheless this recommendation is probably not what you want. 然而,这个建议可能不是你想要的。 Let's see through details why that helps to solve problem: 让我们看看为什么这有助于解决问题:

fastcgi_buffer : fastcgi_buffer

Syntax: fastcgi_buffers number size; 语法:fastcgi_buffers数字大小;

Default: fastcgi_buffers 8 4k|8k; 默认值:fastcgi_buffers 8 4k | 8k;

Context: http, server, location 上下文:http,服务器,位置

Sets the number and size of the buffers used for reading a response from the FastCGI server, for a single connection. 设置用于从FastCGI服务器读取响应的缓冲区的数量和大小,用于单个连接。 By default, the buffer size is equal to one memory page. 默认情况下,缓冲区大小等于一个内存页面。 This is either 4K or 8K, depending on a platform. 这是4K或8K,具体取决于平台。

fastcgi_buffer_size : fastcgi_buffer_size

Syntax: fastcgi_buffer_size size; 语法:fastcgi_buffer_size size;

Default: fastcgi_buffer_size 4k|8k; 默认值:fastcgi_buffer_size 4k | 8k;

Context: http, server, location 上下文:http,服务器,位置

Sets the size of the buffer used for reading the first part of the response received from the FastCGI server. 设置用于读取从FastCGI服务器接收的响应的第一部分的缓冲区的大小。 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. 这是4K或8K,具体取决于平台。 It can be made smaller, however. 然而,它可以做得更小。

So there is only fastcgi_buffer_size makes a difference because response header doesn't fit into the 4KB buffer. 因此,只有fastcgi_buffer_size有所不同,因为响应头不适合4KB缓冲区。 Most of time it happens due to large cookie size. 大部分时间都是由于大的cookie大小而发生的。 So it's strongly recommended to leave settings as is but reduce cookie size instead and have only minimal amount of data stored there like user_id, session_id, so the general idea cookie storage is for non-sensitive set of IDs. 因此强烈建议保留设置,但减少cookie大小,并且只有最小量的数据存储在那里,如user_id,session_id,因此一般的想法cookie存储是针对非敏感的ID集。 Some browsers don't treat large cookies well. 有些浏览器不能很好地处理大型cookie。

So the solution would be: 所以解决方案是:

1. Reduce cookie size

2. Get back to original settings

http {
  fastcgi_buffers 8 4k;
  fastcgi_buffer_size 4k;
}

In case of difficulties with reduce cookie size turn off buffering for certain location: 如果在减少cookie大小方面遇到困难,请关闭某个位置的缓冲:

location /request {
  fastcgi_buffering off;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM