Rails 4.0.0遇到现有路由的周期性RoutingError异常(404)

时间:2016-03-04 00:23:36

标签: ruby-on-rails ruby nginx puma

问题是我们的网站会定期为存在的路由呈现404 RoutingError异常;甚至根路线。

这一问题似乎在当天最繁忙的时段出现。如果刷新页面,页面可能会正确呈现,也可能不会呈现。

如果刷新Puma,问题会暂时解除。

对于实际存在的路径的这404个异常的puma日志与对于实际不存在的路由发生的404异常完全相同。示例日志如下:

ActionController::RoutingError (No route matches [GET] : "/app"
  actionpack (4.0.11.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.11.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.11.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.11.1) lib/rails/rack/logger.rb:22:in `call'
  request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
  actionpack (4.0.11.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.5) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.11.1) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.11.1) lib/rails/engine.rb:511:in `call'
  railties (4.0.11.1) lib/rails/application.rb:97:in `call'
  puma (2.15.3) lib/puma/configuration.rb:79:in `call'
  puma (2.15.3) lib/puma/server.rb:541:in `handle_request'
  puma (2.15.3) lib/puma/server.rb:388:in `process_client'
  puma (2.15.3) lib/puma/server.rb:270:in `block in run'
  puma (2.15.3) lib/puma/thread_pool.rb:106:in `call'
  puma (2.15.3) lib/puma/thread_pool.rb:106:in `block in spawn_thread'

此路由确实存在,但服务器仍在为请求呈现404。

我对正在发生的事情感到茫然,所以我在下面列出了服务器的配置。

常规服务器信息

Ubuntu 14.04

Nginx 1.4.7

Puma 2.15.3

Ruby 2.2.0

Rails 4.0.11.1

Nginx配置

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Nginx虚拟主机配置

upstream puma {
  server unix:/tmp/puma.sock;
}

server {
  listen 80 default deferred default_server;

  root /home/deployer/deploys/current/public;

  if ($http_host ~* domain.com) {
    return 301 https://$host$request_uri$is_args$args;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    proxy_set_header X-Forwarded-Proto https;
  }

  try_files $uri/index.html $uri @puma;

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mp3|flv|mpeg|avi|svg)$ {
    try_files $uri @puma;
  }

  location @puma {
    proxy_headers_hash_max_size 51200;
    proxy_headers_hash_bucket_size 6400;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto http;
    proxy_set_header X-Forwarded-Ssl off;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_read_timeout 250;  #4 minutes
    proxy_pass http://puma;
    proxy_set_header X-Forwarded-Proto https;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 60;
}

server {
  listen 443 default_server ssl;
  server_name _;

  ssl                  on;
  ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
  ssl_certificate      /home/deployer/ssl/tld/domain.chained.crt;
  ssl_certificate_key  /home/deployer/ssl/tld/domain.key;

  root /home/deployer/deploys/current/public;

  if ($http_host !~* domain.com) {
    return 301 http://$host$request_uri$is_args$args;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    proxy_set_header X-Forwarded-Proto https;
  }

  try_files $uri/index.html $uri @puma;

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mp3|flv|mpeg|avi|svg)$ {
    try_files $uri @puma;
  }

  location @puma {
    proxy_headers_hash_max_size 51200;
    proxy_headers_hash_bucket_size 6400;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_read_timeout 250;  #4 minutes
    proxy_pass http://puma;
    proxy_set_header X-Forwarded-Proto https;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 60;
}

Puma配置(config / puma.rb)

workers Integer(4)
threads 12, 12

port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

通过Capistrano进行Puma部署

exec su - deployer -c 'cd /home/deployer/deploys/releases/20160303234701; export PORT=5000; export RAILS_ENV=production;  export PUMA_WORKERS=4; bundle exec puma -e $RAILS_ENV --pidfile /tmp/puma.pid -C config/puma.rb -b unix:///tmp/puma.sock >> /var/log/domain-service/web-1.log 2>&1'

1 个答案:

答案 0 :(得分:0)

这可以追溯到控制器中的reload_routes!电话。