upstream sent too big header while reading response header from upstream
我得到的是这类错误:
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
总是一样的。一个网址反复出现,中间用逗号隔开。我不知道是什么原因造成的。有谁知道吗?
更新:又出了一个错误:
http request count is zero while sending response to client
这里是配置。还有其他不相关的东西,但这部分被添加/编辑了
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;
/etc/nginx/nginx.conf
,其值应该在http {...}内。
- Mario 2018-02-26
如果nginx是作为代理/反向代理运行的
也就是说,对于ngx_http_proxy_module
的用户来说,
除了fastcgi
之外,proxy
模块还将请求头保存在一个临时缓冲区中。
因此,你可能还需要增加proxy_buffer_size
和proxy_buffers
,或者完全禁用它(请阅读nginx文档)。
代理缓冲配置的例子
http {
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
禁用代理缓冲区的示例(推荐用于长轮询服务器)
http {
proxy_buffering off;
}
欲了解更多信息:Nginx代理模块文档
writev() failed (104: Connection reset by peer) while sending to client
那些代理设置可能会解决这个错误,嘿嘿,是在上游服务器上还是在代理上?
- Adam Patterson 2019-08-15
proxy_buffers 4 ...
?因为默认值似乎是8
- adrianTNT 2020-01-30
Plesk的说明
我在这里把前两个答案合并起来
在Plesk 12中,我把nginx作为反向代理运行(我想这是默认的)。所以现在的top answer不起作用,因为nginx也作为代理运行。
我去了一趟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;
Subscriptions | [subscription domain] | Websites & Domains (tab) | [Virtual Host domain] | Web Server Settings
找到它,那么我不确定你的意思是什么?
- icc97 2017-11-13
/etc/nginx/conf.d/proxy.conf
,并重新启动了nginx,它工作得很好,谢谢!
- rubo77 2019-07-10
upstream sent too big header while reading response header from upstream
是nginx表示 "我不喜欢我所看到的 "的通用方式。
- 你的上游服务器的线程崩溃了
- 上游服务器发送了一个无效的标头回来
- 从STDERR发回的通知/警告溢出了它们的缓冲区,它和STDOUT都被关闭了。
3、看一下消息上面的错误日志,是不是在消息前面有记录的行流?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
你可以看到,从底部第三行开始,缓冲区的限制被打破了,下一个线程的写入超过了它。然后,Nginx关闭了连接,并返回502给客户端。
2:记录每个请求发送的所有头信息,审查它们并确保它们符合标准(nginx不允许任何超过24小时的东西删除/过期cookie,发送无效的内容长度,因为错误信息在内容计算之前被缓冲了......)。getallheaders函数调用通常可以帮助解决抽象代码的情况 php get all headers。
例子包括:
<?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、验证,或者做一个脚本日志,以确保你的线程到达正确的终点,而不是在完成之前就退出。
我有一个django应用程序部署在EBS上,我使用Python 3.8运行在64位的Amazon Linux 2。下面的方法对我有用(注意如果你使用以前的Linux版本,文件夹结构可能是不同的。更多信息请见官方文档。
制作.platform
文件夹和它的子目录,如下图所示:
|-- .ebextensions # Don't put nginx config here
| |-- django.config
|-- .platform # Make ".platform" folder and its subfolders
|-- nginx
| -- conf.d
| -- proxy.conf
请注意,proxy.conf文件应该放在.platform文件夹内,而不是.ebextensions文件夹或.elasticbeanstalk文件夹。扩展名应以.conf结尾,而不是.config。
在proxy.conf文件中,复制&直接粘贴这些行:
client_max_body_size 50M;
large_client_header_buffers 4 32k;
fastcgi_buffers 16 32k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
不需要发布命令来重启nginx(适用于Amazon Linux 2)。
再次将源代码部署到elastic beanstalk上。
加:
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
在nginx.conf中对server{}进行说明
对我来说是可行的。
server {}
段不到location {}
...
- Constantine Kurbatov 2022-10-07
在docker容器中用php-fpm和nginx运行Symfony应用时,也遇到了同样的问题。
经过一些研究发现,这是由php-fpm的stderr写入nginx日志造成的。也就是说,php警告(在Symfony调试模式下密集产生)在docker logs php-fpm
中重复出现:
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: "NOTICE: PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\DisallowRobotsIndexingListener::onResponse"."
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: "NOTICE: PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse"."
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: "NOTICE: PHP message: [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelFinishRequest"."
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: "NOTICE: PHP message: [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelFinishRequest"."
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: "NOTICE: PHP message: [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleAwareListener::onKernelFinishRequest"."
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: "NOTICE: PHP message: [debug] Notified event "kernel.terminate" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelTerminate"."
[09-Jul-2021 12:25:46] WARNING: [pool www] child 38 said into stderr: ""
和docker logs nginx
:
2021/07/09 12:25:46 [error] 30#30: *2 FastCGI sent in stderr: "ller" to listener "OblgazAPI\API\Common\Infrastructure\EventSubscriber\LegalAuthenticationChecker::checkAuthentication".
PHP message: [debug] Notified event "kernel.controller_arguments" to listener "Symfony\Component\HttpKernel\EventListener\ErrorListener::onControllerArguments".
PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse".
PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelResponse".
PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse".
PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse".
PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\DisallowRobotsIndexingListener::onResponse".
PHP message: [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse".
PHP message: [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelFinishRequest".
PHP message: [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelFinishRequest".
PHP message: [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleAwareListener::onKernelFinishRequest".
PHP message: [debug] Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ErrorListener::logKernelException".
PHP message: [debug] Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException".
然后nginx日志的结尾是
2021/07/09 12:25:46 [error] 30#30: *2 upstream sent too big header while reading response header from upstream ...
而我得到的是502错误。
增加nginx配置中的fastcgi_buffer_size
有帮助,但这似乎更像是对问题的抑制,而不是治疗。
一个更好的解决方案是禁用 php-fpm 通过 FastCGI 发送日志。我发现可以通过在php.ini中设置fastcgi.logging=0
(默认为1)来实现。php docs.
把它改为0后,问题就消失了,nginx的日志看起来干净多了docker logs nginx
:
172.18.0.1 - - [09/Jul/2021:12:36:02 +0300] "GET /my/symfony/app HTTP/1.1" 401 73 "-" "PostmanRuntime/7.26.8"
172.18.0.1 - - [09/Jul/2021:12:36:04 +0300] "GET /my/symfony/app HTTP/1.1" 401 73 "-" "PostmanRuntime/7.26.8"
而所有的php-fpm日志仍然在php-fpm日志中的位置。
我们最终意识到,我们的一台服务器遇到了这种情况,它的fpm配置出了问题,导致通常被记录在磁盘上的php错误/警告/通知被通过FCGI套接字发送。看起来有一个解析错误,当头的一部分被分割到缓冲区中时。
因此,将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更难击中,但也不是不可能,例如原生的:
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
因此,我相信正确的答案是:修复你的fpm配置,使其将错误记录到磁盘上。
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
fastcgi_buffers 16 512k;
当我增加数字时,它对我来说是有效的
如果你使用的是Symfony框架:在乱用Nginx配置之前,先试着禁用ChromePHP吧。
1 - 打开app/config/config_dev.yml。
2 - 对这几行进行注释:
#chromephp:
#type: chromephp
#level: info
ChromePHP在X-ChromePhp-Data头中打包了调试信息的json编码,这对于使用fastcgi的nginx的默认配置来说太过庞大。
来源:https://github.com/symfony/symfony/issues/8413#issuecomment-20412848
在搜索此错误时,这仍然是谷歌上最高的 SO 问题,所以让我们解决它。
当遇到这个错误时,如果不想立即深入研究NGINX的设置,你可能想检查一下你对调试控制台的输出。 在我的例子中,我向FirePHP/Chromelogger控制台输出了大量的文本,由于这些都是作为头文件发送的,所以造成了溢出。
如果这个错误只是因为发送了大量的日志信息而造成的,那么可能不需要改变网络服务器的设置。
我不确定这个问题是否与php发送的头信息有关。 请确定缓冲功能已经启用。简单的方法是创建一个proxy.conf文件:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
还有一个fascgi.conf文件:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_buffers 128 4096k;
fastcgi_buffer_size 4096k;
fastcgi_index index.php;
fastcgi_param REDIRECT_STATUS 200;
接下来,你需要在你的默认配置服务器中这样调用它们:
http {
include /etc/nginx/mime.types;
include /etc/nginx/proxy.conf;
include /etc/nginx/fastcgi.conf;
index index.html index.htm index.php;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log /logs/access.log main;
sendfile on;
tcp_nopush on;
# ........
}
在我们的案例中,我们得到了这个nginx错误,因为我们的后端生成了一个很长的URL的重定向响应:
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/?manyParamsHere...
只是为了好奇,我们把那个大的URL保存到一个文件中,它的大小是4.4Kb。
在配置文件中添加两行/etc/nginx/conf.d/some_site.conf
,帮助我们解决了这个错误:
server {
# ...
location ~ ^/index\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Add these lines:
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
}
}
在官方的nginx 文档中阅读有关这些参数的更多信息。
对于Symfony项目,可以尝试在你的.env文件中加入这一行:
SHELL_VERBOSITY=0
https://symfony.com/doc/current/console/verbosity.html
我就把它放在这里,因为我在我的项目中花了大量的时间来调试这个问题,只有这个特殊的解决方案对我来说是100%有效的(原因是正确的),而且我从来没有发现这个答案与这个主题有关。也许有人会发现它有帮助。
我遇到了这个错误,我找到了3种方法来解决这个问题:
- 在.env中设置
SHELL_VERBOSITY=0
或其他值< 3:https://stackoverflow.com/a/69321273/13653732 在这种情况下,你禁用了你的PHP日志,但它们对开发和调试很有用。 - 在
php.ini
中设置fastcgi.logging=0
。这和上面的结果是一样的。 - 把Symfony从5.2更新到5.3。我认为旧版本有一个问题。
所有的PHP Symfony日志都认为是Nginx的错误,但PHP却正常工作。
我有Nginx 1.17、PHP 8.0.2、PHP-FPM、Symfony 5.2、Xdebug、Docker。
我试过新版本的Nginx 1.21,PHP 8.0.14,但没有任何结果。这个问题不在Apache上。
我改变了Nginx的配置,但也没有任何结果。
我正在使用 Symfony,它有一个很好的异常页面(当使用 ErrorHandler 时)。而这个会将您的异常消息作为标题添加到创建的响应中。
vendor/symfony/error-handler/ErrorRenderer/SerializerErrorRenderer.php
标头称为:X-Debug-Exception
所以要小心,如果你构建了一个非常大的异常信息,无论是nginx、chrome(限制256k)还是curl(~128kb)都不能显示你的页面,并使你很难调试,是什么在输出这些大的头信息。
我的建议是,不要盲目复制和粘贴增加的缓冲区大小到你的nginx配置中,它们是治标不治本的。