ruby-on-rails - 400 错误请求 - 请求 header 或 cookie 太大

标签 ruby-on-rails nginx

我使用 Rails 应用程序从 nginx 收到 400 Bad Request 请求 header 或 cookie 太大。重新启动浏览器可以解决该问题。我只在 cookie 中存储一个字符串 id,所以它应该很小。

哪里可以找到 nginx 错误日志?我查看了nano/opt/nginx/logs/error.log,但它没有任何相关内容。

我尝试设置以下内容但没有成功:

location / {
    large_client_header_buffers  4 32k;
    proxy_buffer_size  32k;
}

nginx.conf

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
  worker_connections  1024;
}
http {
passenger_root /home/app/.rvm/gems/ruby-1.9.3-p392/gems/passenger-3.0.19;
passenger_ruby /home/app/.rvm/wrappers/ruby-1.9.3-p392/ruby;
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
client_max_body_size 20M;
server {
    listen       80;
    server_name  localhost;
    root /home/app/myapp/current/public;
    passenger_enabled on;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;

# location / {
#   large_client_header_buffers  4 32k;
#   proxy_buffer_size  32k;
# }

     #  location / {
     #   root   html;
     #   index  index.html index.htm;
     #   client_max_body_size 4M;
#   client_body_buffer_size 128k;
# }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

这是我在 Firebug 中存储 cookie 的代码和 cookie 的屏幕截图。我使用 firebug 检查存储的 session ,发现 New Relic 和 jQuery 也在存储 cookie;这可能是 cookie 大小超出的原因吗?

enter image description here

def current_company
  return if current_user.nil?
  session[:current_company_id] = current_user.companies.first.id if session[:current_company_id].blank?
    @current_company ||= Company.find(session[:current_company_id])
end

最佳答案

这正是错误所说的 - 请求 header 或 Cookie 太大。您的 header 之一非常大,nginx 拒绝了它。

使用large_client_header_buffers,您的方向是正确的。如果你check the docs ,您会发现它仅在 httpserver 上下文中有效。将其放到服务器 block 上即可工作。

server {
    # ...
    large_client_header_buffers 4 32k;
    # ...
}

顺便说一下,默认的缓冲区数量和大小是 48k,因此您的错误 header 一定是超过 8192 字节的 header 。就您而言,所有这些 cookie(组合成一个 header )都远远超过 limit 。那些 mixpanel cookie 尤其变得相当大。

关于ruby-on-rails - 400 错误请求 - 请求 header 或 cookie 太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17524396/

相关文章:

nginx - Docker 和 Bower 链接

nginx - 一个服务器 block 中有多个 HTTP_X_ACCEL_MAPPING

logging - Nginx 记录到 access.log.1 而不是 access.log

ruby-on-rails - 我无法使用accepts_nested_attributes_for 创建模型对象。它不会创建嵌套对象

ruby-on-rails - has_one 嵌套属性不保存

ruby-on-rails - CanCanCan:授权/取消授权特定模型属性

amazon-web-services - 我是否必须在 AWS Application Load Balancer 后面的 nginx.conf 文件中配置 SSL 证书文件?

nginx 直播延迟太高 45 秒

ruby-on-rails - 用户属性不会保存

jquery - 在动态创建的元素上动态附加元素