简体   繁体   English

Rails项目:路线给生产带来500个错误,但在开发中起作用

[英]Rails project: routes give 500 errors in production but work in development

I'm having some trouble getting my rails app to run in production. 让我的Rails应用程序在生产环境中运行时遇到了一些麻烦。 As I've coded rails before but haven't deployed much it's probably something in my configs and/or deployment process. 正如我之前编写过的rails一样,但是还没有部署太多,这可能是我的配置和/或部署过程中的事情。

Everything runs fine locally in development. 在开发过程中,一切都在本地运行良好。 All tests pass, I can see the routes and access them at localhost. 所有测试都通过了,我可以看到路由并在localhost上访问它们。 When in production (rails 4, nginx, unicorn on CentOS) I'm getting 500 errors on the very same routes. 在生产中(CentOS上的第4条导轨,nginx,独角兽),我在相同的路线上遇到500个错误。

When I rake routes on the server I see all the same routes as when in development. 当我倾斜服务器上的路由时,我看到的所有路由都与开发中相同。 Rake routes on the server returns (shortened from the full list of routes): 服务器上的耙式路径返回(从完整的路径列表中缩短):

          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

Which as you can probably guess comes from resources :companies 您可能猜到的是资源:companies

But http://sortinghat.foo.com/companies gives me the rails 500 page on production (foo is obviously not the domain but I've just replaced the real domain). 但是http://sortinghat.foo.com/companies给了我关于生产的Rails 500页面(foo显然不是域名,但我只是替换了真实域名)。

I am at a loss having searched on here and the web for ideas, having read lots of "how to deploy rails" blog posts, and have looked at lots of config files (you'll see these are ones I copied from elsewhere but it all seems pretty standard). 我不知所措,在这里和网上搜索了想法,读了很多“如何部署Rails”博客文章,并且看了很多配置文件(您会看到这些是我从其他地方复制来的,一切似乎都很标准)。 I'm probably missing something obvious. 我可能缺少明显的东西。 I'm not seeing any issues in the logs. 我在日志中没有看到任何问题。

The only clue I have is that I don't have an /etc/nginx/sites-enabled directory and it looks like capistrano wants one, but I didn't see any errors during cap deploy (except the cleanup one I note at the bottom). 我唯一的线索是我没有启用/ etc / nginx / sites的目录,并且看起来好像capistrano需要一个目录,但是在cap部署过程中我没有看到任何错误(除了我在底部)。 In this case it's the only site being run so I don't need a virtual host and didn't think I needed it so wasn't concerned. 在这种情况下,它是唯一正在运行的站点,因此我不需要虚拟主机,也不认为我需要它,因此不必担心。

Thanks in advance for any help. 在此先感谢您的帮助。

Here's my nginx.config 这是我的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;
}

My unicorn.rb 我的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

And my deploy.rb (Note: I made some things world readable with the chmod 777 as a quick hack. I'm just trying to get this running internally to run a test all behind a firewall. I can probably take those out but I doubt they're hurting anything right now.) 和我的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 works with only one error, but it's just a problem with permissions cleaning up old releases. cap deploy只能解决一个错误,但这只是清理旧版本的权限问题。 ( cap deploy:cold ran fine) 上限部署:冷跑没事)

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

The answer from a friend was: use passenger, not unicorn. 一位朋友的回答是:使用乘客而不是独角兽。 It pretty much worked out of the box. 开箱即用。

You can find it at https://www.phusionpassenger.com/ 您可以在https://www.phusionpassenger.com/上找到它

(I did have some strange issue which I got error 128 from github. Nothing had changed in my deploy.rb except to uncomment the commented out lined at the bottom for passenger; certainly nothing changed in my github account or AFAIK locally. I'm not sure what happened but I deleted my github key for this machine, created a new one and then everything worked.) (我确实遇到了一个奇怪的问题,我从github收到了错误128。我的deploy.rb中没有任何更改,只是取消了注释底部为乘客列出的注释;当然,我的github帐户或本地AFAIK中没有任何更改。不知道发生了什么,但是我删除了这台机器的github密钥,创建了一个新密钥,然后一切正常。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM