Nginx大型头部响应问题

6

"upstream sent too big header while reading response header from upstream"

当我尝试从Facebook进行认证时,我一直遇到这个问题。我已经增加了我的缓冲区:

proxy_buffer_size   256k;
proxy_buffers   8 256k;
proxy_busy_buffers_size   512k;
fastcgi_buffers 8 256k;
fastcgi_buffer_size 128k;

但似乎并没有起到帮助作用。您认为可能的原因是什么呢?

nginx.conf 文件:

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}


http {
    include       /etc/nginx/mime.types;

proxy_buffer_size   256k;
proxy_buffers   8 256k;
proxy_busy_buffers_size   512k;
fastcgi_buffers 8 256k;
fastcgi_buffer_size 128k;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/default

server {
    listen   80 default;
    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www/nginx-default;
        index  index.html index.htm;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
            }

    location /images {
        root   /usr/share;
        autoindex on;
    }
}

nginx。我以为我已经提到了,看起来我忘记了。 - Aram Papazian
PHP如何与nginx交互?您使用FastCGI吗?如果是,能否展示相关的配置部分(包括fastcgi_pass指令)? - VBart
我正在使用FastCGI,但我没有设置fastcgi_pass指令。Nginx(如果我没记错的话)自动为它的php使用fastcgi。这是我第一次安装Nginx,所以我对它还是很陌生的,但我在我的nginx.conf文件中没有看到任何东西。 - Aram Papazian
附加的文件也没有fastcgi_pass。在Nginx中,关于PHP没有任何魔法或自动化。除非您展示所有相关配置,否则无法提供帮助。 - VBart
我正在展示我拥有的所有文件 = / 我删除的唯一部分是文件中的注释。我将进行通用grep以快速查找fastcgi_pass并查看其位置... - Aram Papazian
显示剩余7条评论
5个回答

13

在Codeigniter中我也遇到了同样的错误。这对我有用:

http://forum.nginx.org/read.php?2,192785,196003#msg-196003

在.conf文件中:

location ~* \.php$ {
    fastcgi_pass 127.0.0.1:9001;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    # 16-sept-2012 parametros para evitar el 502
    fastcgi_temp_file_write_size 10m;
    fastcgi_busy_buffers_size 512k;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 16 512k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_intercept_errors on;
    fastcgi_next_upstream error invalid_header timeout http_500;
}

1
我遇到了同样的问题,这个答案帮了我很多。 - aarreoskari

2

今天早上我也遇到了完全相同的问题。不过,增加缓冲区大小对我有用。这是我使用的设置:

   proxy_buffer_size    128k;
   proxy_buffers     4 256k;
   proxy_busy_buffers_size 256k;
   proxy_temp_file_write_size 256k;

您的配置文件中唯一我看不到的设置是:
   proxy_temp_file_write_size 256k;

另外,我仅为该虚拟主机添加了这些值。我认为这并不重要,但可以尝试一下。


1
原来Codeigniter设置了自己的最大尺寸。我还没有找到如何限制它的方法,但是更改nginx不会有任何影响。感谢VBart和gsharma的所有帮助。

0

我在一个800字节长、有4个头部的页面上遇到了这个错误。这是一个注销页面,用于删除cookies。为了使cookies过期,我将它们设置回我的生日。但在nginx中,它们必须在一个月内过期才能通过验证以删除cookies。

我对几个不同但无效的头部进行了检查,并得到了相同的结果。如果nginx无法验证头部,则会抛出:upstream sent too big header while reading response header from upstream

2015年:从经验中获得更多信息: 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:
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

在前20个错误中的第3行可以看到缓冲区限制被触发、破裂并且下一个线程在其上写入。然后Nginx关闭了连接并向客户端返回502。

2:记录每个请求发送的所有标头,审核它们并确保它们符合规范(Nginx不允许删除/过期24小时以前的任何内容的Cookie,并发送无效的内容长度,因为在计算内容之前会缓冲错误消息...)

示例包括:

<?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:验证或记录脚本,以确保您的线程到达正确的结束点并在完成之前不退出。


0

我们正在迁移生产环境,旧环境没有问题,但出现了“从上游读取响应头时发送的头太大”的问题。这是一个Codeigniter 2.x应用程序。

就像@gsharma所说,更改服务器配置后,错误日志消失了。

    fastcgi_buffers 256 4k;
    fastcgi_buffer_size 8k;

然而,仍然存在一些问题:登录不再起作用了。 问题出在 $config['sess_encrypt_cookie']=TRUE;

当使用 sess_encrypt_cookie 时,Codeigniter 尝试使用 mcrypt 库,但如果不存在,则使用名为 '_xor_encode' 的方法。好吧,我认为这个方法有缺陷。

安装 php-mcrypt 后,一切都正常了。

(对我的英语表示抱歉)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接