Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 400错误请求-请求标头或cookie太大_Ruby On Rails_Nginx - Fatal编程技术网

Ruby on rails 400错误请求-请求标头或cookie太大

Ruby on rails 400错误请求-请求标头或cookie太大,ruby-on-rails,nginx,Ruby On Rails,Nginx,我使用Rails应用程序从nginx收到400个错误请求头或cookie太大。重新启动浏览器可修复此问题。我只是在我的cookie中存储一个字符串id,所以它应该很小 在哪里可以找到nginx错误日志?我查看了nano/opt/nginx/logs/error.log,但它没有任何相关内容 我试图设置以下内容,但没有成功: location / { large_client_header_buffers 4 32k; proxy_buffer_size 32k; } ngi

我使用Rails应用程序从nginx收到400个错误请求头或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中存储cookies的代码和cookies的屏幕截图。我使用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

通过添加

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

这就是错误所说的-
请求头或Cookie太大
。您的一个标题非常大,nginx拒绝了它

使用
大客户机\u头\u缓冲区
,您走上了正确的道路。如果您这样做,您会发现它只在
http
server
上下文中有效。将它升级到服务器块,它就会工作

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

顺便说一句,默认的缓冲区编号和大小是
4
8k
,因此您的坏头必须是超过8192字节的头。在您的情况下,所有这些cookie(合并到一个标题)都是完全超出了范围的。这些mixpanel Cookie特别大。

关于上面的答案,但是需要提到
客户机\u头\u缓冲区\u大小

http {
  ...
  client_body_buffer_size     32k;
  client_header_buffer_size   8k;
  large_client_header_buffers 8 64k;
  ...
}
在我的案例(Cloud Foundry/NGiNX buildpack)中,原因是指令
proxy\u set\u header Host…
,删除此行后,NGiNX变得稳定:

http {
  server {
    location /your-context/ {
       # remove it: # proxy_set_header Host myapp.mycfdomain.cloud;
    }
  }
}

我得到的错误几乎每600个请求时,网页抓取。首先,假设代理服务器或远程ngix限制。我试图删除所有cookie和其他浏览器解决方案,但没有成功。远程服务器不在我的控制范围内


在我的例子中,我犯了一个错误,在httpClient对象中添加了一个又一个新头。在定义了一个全局httpclient对象之后,添加了一次头,问题就不再出现了。这是一个小错误,但不幸的是,我们没有试图理解问题,而是跳到了stackoverflow:)有时,我们应该尝试理解问题本身。

会话中存储了多少数据?请,显示在cookie中存储数据的代码部分。这似乎是对google查询此错误消息的高度响应。除了这里讨论的明显原因外,如果代理配置中有一个循环,也可能会导致此问题-这将在错误日志中显示为“768 worker_连接在连接到上游时不够”。此解决方案的基本原理是什么?对问题的一点解释有助于未来的访问者。@BradKoch这是官方的wiki更新链接:感谢您的发布和上下文。总是更好地知道为什么和你正在改变什么,而不是仅仅被告知要改变它!我也有同样的问题。通过遵循这些评论。我已经解决了我的问题。这个解决方案是针对Nginx服务器的。apache服务器有什么解决方案吗?@NareshRupareliya这个问题是关于nginx的;您需要搜索/询问一个新问题或检查。在哪里检查最大值?为了完整性,客户端文档\u标题\u缓冲区\u大小声明如下:“设置用于读取客户端请求头的缓冲区大小。对于大多数请求,1K字节的缓冲区就足够了。但是,如果一个请求包含长cookie,或者来自WAP客户端,则它可能不适合1K。如果请求行或请求标头字段不适合此缓冲区,则会分配较大的缓冲区(由large_client_header_buffers指令配置)。”