299

我收到这些错误:

2014/05/24 11:49:06 [错误] 8376#0: *54031 上游从上游读取响应标头时发送的标头太大,客户端:107.21.193.210,服务器:aamjanata.com,请求:“GET /the-洗脑-编年史-由gujarat-government/赞助,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-赞助-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-赞助-by- gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/ ,%20https:/aamjanata.com/the-brainwash-chronicles-supported-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-洗脑编年史-由gujarat-government/赞助,%20https://aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-赞助-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-赞助-by- gujarat-government/,%20https://aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https://aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government /,%20https:/aamjanata.com/the-brainwash-chronicles-supported-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-洗脑编年史-由gujarat-government/赞助,%20https://aamjanata.com/the-brainwash-chronicles-owned-by-gujarat-government/,%20https:/aamjanata.com/the-brainwash-chronicles-由古吉拉特邦政府赞助/,%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;
    }

然后在服务器块中: set $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";
    }`
4

15 回答 15

520

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

fastcgi_buffers 16 16k; 
fastcgi_buffer_size 32k;
于 2014-05-24T13:46:16.257 回答
184

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

也就是说,对于用户ngx_http_proxy_module

除此之外fastcgi,该proxy模块还将请求标头保存在临时缓冲区中。

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

代理缓冲配置示例

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

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

http {
  proxy_buffering off;
}

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

于 2014-12-18T16:26:25.960 回答
40

Plesk 说明

我在这里结合了前两个答案

在 Plesk 12 中,我让 nginx 作为反向代理运行(我认为这是默认代理)。因此,当前的最佳答案不起作用,因为 nginx 也作为代理运行。

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

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

fastcgi_buffers         16  16k;
fastcgi_buffer_size         32k;
proxy_buffer_size          128k;
proxy_buffers            4 256k;
proxy_busy_buffers_size    256k;
于 2017-03-29T12:32:33.810 回答
31

upstream sent too big header while reading response header from upstream是 nginx 的通用表达方式“我不喜欢我所看到的”

  1. 您的上游服务器线程崩溃了
  2. 上游服务器发回了无效的标头
  3. 从 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

您可以在底部的第 3 行中看到缓冲区限制已被击中、损坏,并且下一个线程在其上写入。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:验证或制作脚本日志,以确保您的线程到达正确的终点并且在完成之前不退出。

于 2015-11-23T18:29:59.617 回答
6

我们最终意识到我们的一台服务器遇到了这种情况,它破坏了 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";

提高缓冲区使 502 更难命中但并非不可能,例如原生:

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 配置,以便将错误记录到磁盘。

于 2017-08-08T23:20:42.380 回答
6

如果您使用的是 Symfony 框架:在弄乱 Nginx 配置之前,请先尝试禁用 ChromePHP。

1 - 打开 app/config/config_dev.yml

2 - 评论这些行:

#chromephp:
    #type:   chromephp
    #level:  info

ChromePHP 将调试信息 json-encoded 打包在 X-ChromePhp-Data 标头中,这对于带有 fastcgi 的 nginx 的默认配置来说太大了。

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

于 2017-09-21T18:08:50.153 回答
6

我有一个部署到 EBS 的 django 应用程序,我使用的是在 64 位 Amazon Linux 2 上运行的 Python 3.8。以下方法对我有用(注意,如果您使用的是以前的 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 NOT .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。

于 2021-06-05T04:54:30.393 回答
5

在 php-fpm 和 docker 容器中的 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 错误。

增加fastcgi_buffer_sizenginx 配置有所帮助,但它似乎更像是抑制问题,而不是治疗。

更好的解决方案是禁用 php-fpm 通过 FastCGI 发送日志。发现可以fastcgi.logging=0在php.ini中设置(默认为1)。php 文档

将其更改为 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 日志中的位置。

于 2021-07-09T10:06:24.240 回答
3

添加:

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

到 nginx.conf 中的服务器{}

为我工作。

于 2020-12-22T15:20:51.403 回答
2

在搜索此错误时,这仍然是 Google 上最高的 SO-question,所以让我们碰一下。

当遇到此错误并且不想立即深入研究 NGINX 设置时,您可能需要检查调试控制台的输出。在我的情况下,我将大量文本输出到 FirePHP / Chromelogger 控制台,并且由于这都是作为标头发送的,因此导致了溢出。

如果此错误是由发送大量日志消息引起的,则可能不需要更改 Web 服务器设置。

于 2019-12-19T09:24:24.273 回答
2

我不确定这个问题是否与 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;
 # ........
}
于 2020-04-23T15:05:29.533 回答
2

对于Symfony项目,尝试将此行添加到您的 .env 文件中:

SHELL_VERBOSITY=0

https://symfony.com/doc/current/console/verbosity.html

我将把它留在这里,因为我在我的项目中花费了大量时间来调试它,只有这个特定的解决方案对我来说 100% 有效(出于正确的原因),而且我从来没有找到与这个主题相关的答案。也许有人会发现它有帮助。

于 2021-09-24T21:28:50.630 回答
2
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
fastcgi_buffers 16 512k;

当我增加数字时它对我有用

于 2021-11-17T09:16:31.443 回答
0

在我们的例子中,我们收到了这个 nginx 错误,因为我们的后端生成了带有很长 URL 的重定向响应:

HTTP/1.1 301 Moved Permanently 
Location: https://www.example.com/?manyParamsHere...

出于好奇,我们将那个大 URL 保存到一个文件中,它的大小为 4.4 Kb。

在配置文件中添加两行/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 文档中阅读有关这些参数的更多信息。

于 2021-03-18T14:34:17.183 回答
0

我遇到了这个错误,我找到了 3 种方法来解决这个问题:

  • SHELL_VERBOSITY=0在 .env 中设置或其他值 < 3: https ://stackoverflow.com/a/69321273/13653732在这种情况下,您禁用 PHP 日志,但它们对于开发和调试很有用。
  • 设置fastcgi.logging=0php.ini. 与上面的结果相同。
  • 将 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 配置,但也没有任何结果。

于 2022-01-06T14:30:01.183 回答