2013-07-08 1653 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