2013-07-08 1649 views
26

我收到了一个400错误请求请求标头或cookie,它与我的Rails应用程序中的nginx太大。重新启动浏览器可以解决问题。我只在我的cookie中存储字符串ID,所以它应该很小。400错误请求 - 请求标头或Cookie太大

我在哪里可以找到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; 
# } 
#} 

} 

这里是我的代码存储的cookie和在Firebug的Cookie截图。我用萤火虫检查存储的会话,我发现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 
+0

多少数据你在会话存储? –

+0

请显示cookie中存储数据的代码部分。 –

回答

52

这只是错误怎么说 - Request Header Or Cookie Too Large。其中一个标题非常大,而nginx则拒绝它。

您正在对large_client_header_buffers正确的方向前进。如果您使用check the docs,您会发现它仅在httpserver上下文中有效。将它撞上一个服务器模块,它就可以工作。

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

顺便说一句,默认的缓冲区数量和大小是48k,使你的坏头必须是一个最超过8192个字节。在你的情况下,所有这些cookie(合并为一个头)都远远超过limit。特别是那些mixpanel饼干变得相当大。

+0

感谢张贴和上下文。总是更好地知道为什么和你正在改变,而不是被告知要改变它! –

+0

不客气! –

13

固定加入

server { 
    ... 
    large_client_header_buffers 4 16k; 
    ... 
} 
+4

该解决方案的基本原理是什么?对问题的一点解释有助于未来的访问者。 –

+0

@BradKoch这是官方维基http://wiki.nginx.org/HttpCoreModule#large_client_header_buffers –

2

对于以上的答案,但有client_header_buffer_size需要提到:

http { 
    ... 
    client_body_buffer_size  32k; 
    client_header_buffer_size 8k; 
    large_client_header_buffers 8 64k; 
    ... 
} 
+1

为了完整起见,client_header_buffer_size的文档声明如下:“设置读取客户端请求头的缓冲区大小对于大多数请求来说,1K字节的缓冲区就足够了,但如果请求包含长的Cookie或来自WAP客户端,它可能不适合1K,如果请求行或请求头字段不适合此缓冲区,则会分配由large_client_header_buffers指令配置的较大缓冲区。“ – Christof