当从上游读取响应报头时,上游发送太大的报头

我遇到了这样的错误:

2014/05/24 11:49:06 [error] 8376#0: *54031 upstream sent too big header while reading response header from upstream, client: 107.21.193.210, server: aamjanata.com, request: "GET /the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https://aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-sponsored-by-gujarat-government/,%20ht 

总是这样。 一个url一遍又一遍地用逗号分隔。 无法弄清楚是什么原因造成的。 任何人有一个想法?

更新:另一个错误:

 http request count is zero while sending response to client 

这是configuration。 还有其他不相干的东西,但这部分是增加/编辑的

 fastcgi_cache_path /var/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; # Upstream to abstract backend connection(s) for PHP. upstream php { #this should match value of "listen" directive in php-fpm pool server unix:/var/run/php5-fpm.sock; } 

然后在服务器块中设置$ skip_cache 0;

  # POST requests and urls with a query string should always go to PHP if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } # Don't cache uris containing the following segments if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; } # Don't use the cache for logged in users or recent commenters if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; } location / { # This is cool because no php is touched for static content. # include the "?$args" part so non-default permalinks doesn't break when using query string try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri /index.php; include fastcgi_params; fastcgi_pass php; fastcgi_read_timeout 3000; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache WORDPRESS; fastcgi_cache_valid 60m; } location ~ /purge(/.*) { fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1"; }` 

将以下内容添加到您的conf文件中

 fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; 

如果nginx作为代理/反向代理运行

也就是说,对于ngx_http_proxy_module用户

除了fastcgiproxy模块还将请求头保存在临时缓冲区中。

所以您可能还需要增加proxy_buffer_sizeproxy_buffers ,或者完全禁用它(请阅读nginx文档 )。

代理缓冲configuration示例

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

禁用代理缓冲区的示例(推荐用于长轮询服务器)

 http { proxy_buffering off; } 

有关更多信息: Nginx代理模块文档

upstream sent too big header while reading response header from upstream是nginx的一般说法:“我不喜欢我所看到的”

  1. 您的上游服务器线程崩溃
  2. 上游服务器发回一个无效的头
  3. 从STDERR返回的通知/警告溢出了它们的缓冲区,并且它和STDOUT都closures了

3:查看消息上方的错误日志,是否在消息前面logging了stream? PHP message: PHP Notice: Undefined index:来自循环的示例代码片段我的日志文件:

 2015/11/23 10:30:02 [error] 32451#0: *580927 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: Firstname in /srv/www/classes/data_convert.php on line 1090 PHP message: PHP Notice: Undefined index: Lastname in /srv/www/classes/data_convert.php on line 1090 ... // 20 lines of same PHP message: PHP Notice: Undefined index: Firstname in /srv/www/classes/data_convert.php on line 1090 PHP message: PHP Notice: Undefined index: Lastname in /srv/www/classes/data_convert.php on line 1090 PHP message: PHP Notice: Undef 2015/11/23 10:30:02 [error] 32451#0: *580927 FastCGI sent in stderr: "ta_convert.php on line 1090 PHP message: PHP Notice: Undefined index: Firstname 

你可以从底部的第三行看到缓冲区限制被打破,并且下一个线程写在它之上。 然后Nginxclosures连接并返回502给客户端。

2:logging每个请求发送的所有标题,检查它们并确保它们符合标准(nginx不允许超过24小时的任何内容删除/过期cookie,发送无效的内容长度,因为在内容计数之前缓冲了错误消息。 ..)。 getallheaders函数调用通常可以帮助在抽象的代码情况下PHP获取所有标题

例子包括:

 <?php //expire cookie setcookie ( 'bookmark', '', strtotime('2012-01-01 00:00:00') ); // nginx will refuse this header response, too far past to accept .... ?> 

和这个:

 <?php header('Content-type: image/jpg'); ?> <?php //a space was injected into the output above this line header('Content-length: ' . filesize('image.jpg') ); echo file_get_contents('image.jpg'); // error! the response is now 1-byte longer than header!! ?> 

1:validation或创build一个脚本日志,以确保您的线程到达正确的终点,并在完成之前不退出。

Plesk说明

在Plesk 12中,我让nginx作为反向代理运行(我认为这是默认的)。 所以当前最好的答案是不正常的,因为nginx也是作为代理运行的。

我去了Subscriptions | [subscription domain] | Websites & Domains (tab) | [Virtual Host domain] | Web Server Settings Subscriptions | [subscription domain] | Websites & Domains (tab) | [Virtual Host domain] | Web Server Settings Subscriptions | [subscription domain] | Websites & Domains (tab) | [Virtual Host domain] | Web Server Settings

然后在该页面的底部,您可以设置其他nginx指令 ,我将其设置为前两个答案的组合:

 fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; 

如果您使用Symfony框架:

在搞乱Nginxconfiguration之前,先尝试先禁用ChromePHP。

1 – 打开app / config / config_dev.yml

2 – 评论这些行:

 #chromephp: #type: chromephp #level: info 

ChromePHP将debugging信息json编码在X-ChromePhp-Data标头中,这对于使用fastcgi的nginx的默认configuration来说太大了。

来源: https : //github.com/symfony/symfony/issues/8413#issuecomment-20412848

我们最终意识到我们的一台服务器遇到了这种情况,导致了错误/警告/通知,通常logging到磁盘的fpmconfiguration导致通过FCGI套接字发送。 看起来有一个parsing错误,当部分头部被分割成缓冲块时。

所以将php_admin_value[error_log]设置为实际可写并重新启动php-fpm足以解决问题。

我们可以用较小的脚本重现问题:

 <?php for ($i = 0; $i<$_GET['iterations']; $i++) error_log(str_pad("a", $_GET['size'], "a")); echo "got here\n"; 

提高缓冲区使502s更难打但不是不可能的,例如native:

 bash-4.1# for it in {30..200..3}; do for size in {100..250..3}; do echo "size=$size iterations=$it $(curl -sv "http://localhost/debug.php?size=$size&iterations=$it" 2>&1 | egrep '^< HTTP')"; done; done | grep 502 | head size=121 iterations=30 < HTTP/1.1 502 Bad Gateway size=109 iterations=33 < HTTP/1.1 502 Bad Gateway size=232 iterations=33 < HTTP/1.1 502 Bad Gateway size=241 iterations=48 < HTTP/1.1 502 Bad Gateway size=145 iterations=51 < HTTP/1.1 502 Bad Gateway size=226 iterations=51 < HTTP/1.1 502 Bad Gateway size=190 iterations=60 < HTTP/1.1 502 Bad Gateway size=115 iterations=63 < HTTP/1.1 502 Bad Gateway size=109 iterations=66 < HTTP/1.1 502 Bad Gateway size=163 iterations=69 < HTTP/1.1 502 Bad Gateway [... there would be more here, but I piped through head ...] 

fastcgi_buffers 16 16k; fastcgi_buffer_size 32k;

 bash-4.1# for it in {30..200..3}; do for size in {100..250..3}; do echo "size=$size iterations=$it $(curl -sv "http://localhost/debug.php?size=$size&iterations=$it" 2>&1 | egrep '^< HTTP')"; done; done | grep 502 | head size=223 iterations=69 < HTTP/1.1 502 Bad Gateway size=184 iterations=165 < HTTP/1.1 502 Bad Gateway size=151 iterations=198 < HTTP/1.1 502 Bad Gateway 

所以我相信正确的答案是:修复你的fpmconfiguration,以便将错误logging到磁盘。