Ruby-on-rails 如何避免nginx“上游发送太大的标头”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2307231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:19:02  来源:igfitidea点击:

How to avoid nginx "upstream sent too big header" errors?

ruby-on-railsnginxpassenger

提问by Rob Watson

I'm running nginx, Phusion Passenger and Rails.

我正在运行 nginx、Phusion 乘客和 Rails。

I am running up against the following error:

我遇到了以下错误:

upstream sent too big header while reading response header from upstream, client: 87.194.2.18, server: xyz.com, request: "POST /user_session HTTP/1.1", upstream: "passenger://unix:/tmp/passenger.3322/master/helper_server.sock

It is occuring on the callback from an authentication call to Facebook Connect.

它发生在从身份验证调用到 Facebook Connect 的回调中。

After googling, and trying to change nginx settings including proxy_buffer_size and large_client_header_buffers is having no effect.

谷歌搜索后,尝试更改包括 proxy_buffer_size 和 large_client_header_buffers 在内的 nginx 设置无效。

How can I debug this?

我该如何调试?

回答by Rob Di Marco

Came across this error recently.

最近遇到这个错误。

Since Passenger 3.0.8there is now a setting that allows you to set the buffers and buffer size. So now you can do

Passenger 3.0.8 开始,现在有一个设置允许您设置缓冲区和缓冲区大小。所以现在你可以做

http {
    ...
    passenger_buffers 8 16k;
    passenger_buffer_size 32k;
}

That resolved the issue for me.

那为我解决了这个问题。

回答by Antiarchitect

Try to add this to the config:

尝试将其添加到配置中:

http {
    ...
    proxy_buffers 8 16k;
    proxy_buffer_size 32k;
    }

回答by Linus Unneb?ck

Maybee adding this will make it work, how are you connecting to upstream? http, fastcgi or something else?

Maybee 添加这将使它起作用,您如何连接到上游?http、fastcgi 还是别的什么?

http {
    ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
}

回答by Unitech

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

回答by ppostma1

This is everything I have come to understand about this error in the last 2 years:

这是我在过去两年中对这个错误的理解:

upstream sent too big header while reading response header from upstreamis nginx's generic way of saying "I don't like what I'm seeing"

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

  1. Your upstream server thread crashed
  2. The upstream server sent an invalid header back
  3. The Notice/Warnings sent back from STDERR broke their buffer and both it and STDOUT were closed
  1. 你的上游服务器线程崩溃了
  2. 上游服务器发回了一个无效的标头
  3. 从 STDERR 发回的通知/警告打破了他们的缓冲区,它和 STDOUT 都被关闭了

3: Look at the error logs above the message, is it streaming with logged lines preceding the message? PHP message: PHP Notice: Undefined index:Example snippet from a loop my log file:

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

you can see in the 3rd line (from the 20 previous errors) the buffer limit was hit, broke, and the next thread wrote in over it. Nginx then closed the connection and returned 502 to the client.

您可以在第 3 行(从之前的 20 个错误中)看到缓冲区限制已达到、中断,并且下一个线程将其写入。然后 Nginx 关闭连接并向客户端返回 502。

2: log all the headers sent per request, review them and make sure they conform to standards (nginx does not permit anything older than 24 hours to delete/expire a cookie, sending invalid content length because error messages were buffered before the content counted...)

2:记录每个请求发送的所有标头,检查它们并确保它们符合标准(nginx 不允许超过 24 小时的任何内容删除/过期 cookie,发送无效的内容长度,因为错误消息在内容计数之前被缓冲。 ..)

examples include:

例子包括:

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

and this:

和这个:

<?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: verify, or make a script log, to ensure your thread is reaching the correct end point and not exiting before completion.

1:验证或制作脚本日志,以确保您的线程到达正确的终点并且在完成之前不退出。

回答by aaronbartell

I thought I'd chime in with my solution since I don't see it currently listed. Turns out I was unintentionally putting a large object into the session, as shown below.

我想我会加入我的解决方案,因为我目前没有看到它。结果我无意中将一个大对象放入会话中,如下所示。

session["devise.#{provider}_data"] = env["omniauth.auth"]

This only happened when somebody first authenticated with GitHub OAuth and subsequently tried to authenticate with another social profile that used the same email (why I couldn't originally figure out the issue).

这只发生在有人首先使用 GitHub OAuth 进行身份验证,随后尝试使用相同电子邮件的另一个社交个人资料进行身份验证时(为什么我最初无法找出问题)。

Here's the full OmniauthCallbacksControllerfor contextual reference:

这是OmniauthCallbacksController上下文参考的完整内容:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def self.provides_callback_for(provider)
    class_eval %Q{
      def #{provider}
        @user = User.from_omniauth(request.env["omniauth.auth"])
        if @user.persisted?
          sign_in_and_redirect @user, event: :authentication
          set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
        else
          auth = request.env["omniauth.auth"]
          if User.exists?(email: auth.info.email)
            set_flash_message(:notice, :failure, kind: "#{provider}".capitalize, reason: "email " + auth.info.email + " already exists") if is_navigational_format?
          else
            set_flash_message(:notice, :error, kind: "#{provider}".capitalize) if is_navigational_format?
          end
          session["devise.#{provider}_data"] = env["omniauth.auth"] <----- Remove this line
          redirect_to new_user_registration_path
        end
      end
    }
  end

  [:github, :linkedin, :google_oauth2].each do |provider|
    provides_callback_for provider
  end
end

All was well once I removed the offending line. I am guessing I had it in there for debugging purposes.

一旦我删除了违规行,一切都很好。我猜我把它放在那里是为了调试。