Ruby-on-rails 400 Bad Request - 请求头或 cookie 太大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17524396/
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
400 Bad Request - request header or cookie too large
提问by user1883793
I am getting a 400 Bad Request request header or cookie too large from nginx with my Rails app. Restarting the browser fixes the issue. I am only storing a string id in my cookie so it should be tiny.
我的 Rails 应用程序从 nginx 收到了一个 400 Bad Request 请求标头或太大的 cookie。重新启动浏览器可解决此问题。我只在我的 cookie 中存储一个字符串 id,所以它应该很小。
Where can I find the nginx error logs? I looked at nano /opt/nginx/logs/error.log, but it doesn't have anything related.
我在哪里可以找到 nginx 错误日志?我查看了 nano /opt/nginx/logs/error.log,但它没有任何相关内容。
I tried to set following and no luck:
我试图设置以下但没有运气:
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;
# }
#}
}
Here's my code storing the cookies and a screenshot of the cookies in Firebug. I used firebug to check stored session and I found New Relic and jQuery are storing cookies too; could this be why the cookie size is exceeded?
这是我在 Firebug 中存储 cookie 的代码和 cookie 的屏幕截图。我使用 firebug 检查存储的会话,我发现 New Relic 和 jQuery 也在存储 cookie;这可能是超过 cookie 大小的原因吗?


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
回答by Brad Koch
It's just what the error says - Request Header Or Cookie Too Large. One of your headers is really big, and nginx is rejecting it.
这正是错误所说的 - Request Header Or Cookie Too Large。你的一个标题真的很大,nginx 拒绝了它。
You're on the right track with large_client_header_buffers. If you check the docs, you'll find it's only valid in httpor servercontexts. Bump it up to a server block and it will work.
你在正确的轨道上large_client_header_buffers。如果您检查 docs,您会发现它仅在http或server上下文中有效。把它撞到一个服务器块上,它就会工作。
server {
# ...
large_client_header_buffers 4 32k;
# ...
}
By the way, the default buffer number and size is 4and 8k, so your bad header must be the one that's over 8192 bytes. In your case, all those cookies (which combine to one header) are well over the limit. Those mixpanel cookies in particular get quite large.
顺便说一句,默认缓冲区编号和大小是4and 8k,因此您的坏标头必须是超过 8192 字节的标头。在您的情况下,所有这些 cookie(组合到一个标题中)都远远超过限制。特别是那些混合面板饼干变得非常大。
回答by user1883793
Fixed by adding
通过添加固定
server {
...
large_client_header_buffers 4 16k;
...
}
回答by dr.dimitru
With respect to answers above, but there is client_header_buffer_sizeneeds to be mentioned:
关于上面的答案,但client_header_buffer_size需要提到的是:
http {
...
client_body_buffer_size 32k;
client_header_buffer_size 8k;
large_client_header_buffers 8 64k;
...
}
回答by kinjelom
In my case (Cloud Foundry / NGiNX buildpack) the reason was the directive proxy_set_header Host ..., after removing this line nginx became stable:
在我的情况下(Cloud Foundry / NGiNX buildpack)原因是指令proxy_set_header Host ...,删除这一行后nginx变得稳定:
http {
server {
location /your-context/ {
# remove it: # proxy_set_header Host myapp.mycfdomain.cloud;
}
}
}

