Rails项目:路由在生产中产生500个错误但在开发中工作

时间:2013-09-27 14:42:11

标签: ruby-on-rails deployment nginx capistrano unicorn

我在使用rails应用程序在生产中运行时遇到了一些麻烦。由于我之前编写过rails,但是没有部署很多,这可能是我的配置和/或部署过程中的一些内容。

在开发过程中,一切都运行良好。所有测试都通过,我可以看到路由并在localhost访问它们。在生产中(rails 4,nginx,CentOS上的独角兽)我在同一条路线上遇到500个错误。

当我在服务器上搜索路由时,我看到与开发时相同的路由。服务器上的Rake路由返回(从完整的路由列表缩短):

          Prefix Verb   URI Pattern                           Controller#Action
       companies GET    /companies(.:format)                  companies#index
                 POST   /companies(.:format)                  companies#create
     new_company GET    /companies/new(.:format)              companies#new
    edit_company GET    /companies/:id/edit(.:format)         companies#edit
         company GET    /companies/:id(.:format)              companies#show
                 PATCH  /companies/:id(.:format)              companies#update
                 PUT    /companies/:id(.:format)              companies#update
                 DELETE /companies/:id(.:format)              companies#destroy

您可能猜到的来自资源:公司

但是http://sortinghat.foo.com/companies给了我生产500页的轨道(foo显然不是域名,但我刚刚取代了真正的域名。)

我在这里和网上寻找想法,阅读了大量的“如何部署rails”博客帖子,看了很多配置文件,我感到很遗憾(你会看到这些是我从中复制过的文件)在其他地方,但似乎都很标准)。我可能错过了一些明显的东西。我没有在日志中看到任何问题。

我唯一的线索是我没有/ etc / nginx / sites-enabled目录,看起来像capistrano想要一个,但我没有看到帽子部署期间的任何错误(除了清理一个我请注意底部)。在这种情况下,它是唯一运行的站点,因此我不需要虚拟主机,并且认为我不需要它,所以不关心。

提前感谢您的帮助。

这是我的nginx.config

# Taken from http://www.sitepoint.com/deploying-your-rails-app-to-the-cloud-with-unicorn-nginx-and-capistrano/

upstream unicorn {
  server unix:/tmp/unicorn.sortinghat.sock fail_timeout=0;
}

server {
    listen 80 default deferred;
    server_name sortinghat;
    if ($host = 'sortinghat' ) {
        rewrite ^/(.*)$ http://sortinghat.foo.com/$1 permanent;
    }
  root /var/www/sortinghat/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

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

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

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

我的unicorn.rb

# Taken from http://www.sitepoint.com/deploying-your-rails-app-to-the-cloud-with-unicorn-nginx-and-capistrano/

# Define your root directory
root = "/var/www/sortinghat/current"

# Define worker directory for Unicorn
working_directory root

# Location of PID file
pid "#{root}/tmp/pids/unicorn.pid"

# Define Log paths
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

# Listen on a UNIX data socket
listen "/tmp/unicorn.sortinghat.sock"

# 16 worker processes for production environment
worker_processes 16

# Load rails before forking workers for better worker spawn time
preload_app true

# Restart workes hangin' out for more than 240 secs
timeout 240

和我的deploy.rb(注意:我用 chmod 777 作为一个快速入侵使得一些东西可以读取。我只是想让它在内部运行以运行测试防火墙。我可以把它拿出去,但我怀疑它们现在正在伤害任何东西。)

require 'capistrano/ext/multistage'

set :application, "sortinghat"

# Need to use gnutar since that is what the server uses
# see https://coderwall.com/p/ypwmpg
set :copy_local_tar, "/usr/bin/gnutar" if `uname` =~ /Darwin/

# remove old releases
set :keep_releases, 3


# Source Control
# You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :repository,  "git@github.com:me/project.git"  # changed here but correct in the actual file
set :scm, :git
set :branch, "master"
set :deploy_via, :copy


# Deployment
# Default deployment directory is /u/apps/#{application}” (where #{application} but alternate can be set
# set :deploy_to, "/var/www"
set :user, "mherschberg"
default_run_options[:pty] = true
set :use_sudo, false
set :ssh_options, {:forward_agent => true}


role :web, "sortinghat.foo.com"                          # Your HTTP server, Apache/etc
role :app, "sortinghat.foo.com"                          # This may be the same as your `Web` server
role :db,  "lin-db3.foo.com", :primary => true #,  :no_release => true          # This is where Rails migrations will run
#role :db,  "your slave db-server here" # no slave for test

set :rails_env, "production"

set :stages, ["staging", "production"]
set :default_stage, "staging"

# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"


namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      sudo "chmod 777 /var/www/sortinghat/current/config/unicorn_ini.sh"
      run "/etc/init.d/unicorn_#{application} #{command}" # Using unicorn as the app server
    end
  end

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_ini.sh /etc/init.d/unicorn_#{application}"
    sudo "chmod 777 #{current_path}/config/unicorn_ini.sh"
    sudo "chmod 777 /var/www/sortinghat/current/config/unicorn_ini.sh"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end
  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"

  desc "Make sure local git is in sync with remote."
  task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
      puts "WARNING: HEAD is not the same as origin/master"
      puts "Run `git push` to sync changes."
      exit
    end
  end
  before "deploy", "deploy:check_revision"
end


# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
# namespace :deploy do
#   task :start do ; end
#   task :stop do ; end
#   task :restart, :roles => :app, :except => { :no_release => true } do
#     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
#   end
# end

cap deploy 只能处理一个错误,但这只是清理旧版本的权限问题。 (上限部署:冷运行良好)

  ** [out :: sortinghat.foo.com] rm: cannot remove `/var/www/sortinghat/releases/20130917180205/thout-curl-include=dir/include/build_info': Permission denied

1 个答案:

答案 0 :(得分:-1)

朋友的回答是:使用乘客,而不是独角兽。它几乎开箱即用。

您可以在https://www.phusionpassenger.com/

找到它

(我确实有一些奇怪的问题,我从github得到了错误128.我的deploy.rb没有任何改变,除了取消注释底部的注释用于乘客;当然我的github帐户或AFAIK本地没有任何改变。我不确定发生了什么,但我删除了我的github密钥,创建了一个新的,然后一切正常。)