【问题标题】:nginx big header responsenginx大头响应
【发布时间】:2012-07-17 16:17:33
【问题描述】:

“从上游读取响应头时,上游发送了太大的头”

当我尝试从 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/默认

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。我以为我提到过,好像我忘了。
  • 你确定吗? nginx 如何从 facebook 进行身份验证?
  • 哦,你的意思是代码明智。我正在使用 PHP。当facebook重定向到我的页面时,问题似乎发生在身份验证之后。
  • PHP如何与nginx交互?你使用 FastCGI 吗?如果是这样,您能否显示相关的配置部分(带有fastcgi_pass 指令)?

标签: facebook nginx header


【解决方案1】:

在 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;
}

【讨论】:

  • 我遇到了同样的问题,这个答案有帮助。
【解决方案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;

另外,我仅为该虚拟主机添加了这些值。我认为这无关紧要,但可能值得一试。

【讨论】:

    【解决方案3】:

    原来 Codeigniter 设置了自己的最大大小。我还没有想出如何限制它,但不幸的是改变 nginx 不会改变任何东西。感谢 VBart 和 gsharma 的所有帮助。

    【讨论】:

      【解决方案4】:

      我们正在移动我们的生产环境,旧的环境没有问题,并且存在相同的“上游发送的标头过大,同时从上游读取响应标头”的问题。这是一个 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 后一切正常。

      (对不起我的英语)

      【讨论】:

        【解决方案5】:

        我在一个 800 字节长、4 个标题的页面上收到此错误。这是一个删除 cookie 的登出页面。为了使 cookie 过期,我将它们设置回我的生日。这在 nginx 中不起作用,它们必须在不到一个月的时间内过期才能通过验证以删除 cookie。

        我检查了几个不同但无效的标题并得到了相同的结果。如果 nginx 无法验证它抛出的标头:上游在从上游读取响应标头时发送了太大的标头

        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: 来自循环我的日志文件的示例 sn-p:

        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
        

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

        【讨论】:

          猜你喜欢
          • 2012-05-15
          • 2017-01-08
          • 2016-02-10
          • 1970-01-01
          • 2012-02-28
          • 2021-07-07
          • 2015-10-26
          • 2012-05-25
          相关资源
          最近更新 更多