簡體   English   中英

Rails項目:路線給生產帶來500個錯誤,但在開發中起作用

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

讓我的Rails應用程序在生產環境中運行時遇到了一些麻煩。 正如我之前編寫過的rails一樣,但是還沒有部署太多,這可能是我的配置和/或部署過程中的事情。

在開發過程中,一切都在本地運行良好。 所有測試都通過了,我可以看到路由並在localhost上訪問它們。 在生產中(CentOS上的第4條導軌,nginx,獨角獸),我在相同的路線上遇到500個錯誤。

當我傾斜服務器上的路由時,我看到的所有路由都與開發中相同。 服務器上的耙式路徑返回(從完整的路徑列表中縮短):

          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

您可能猜到的是資源:companies

但是http://sortinghat.foo.com/companies給了我關於生產的Rails 500頁面(foo顯然不是域名,但我只是替換了真實域名)。

我不知所措,在這里和網上搜索了想法,讀了很多“如何部署Rails”博客文章,並且看了很多配置文件(您會看到這些是我從其他地方復制來的,一切似乎都很標准)。 我可能缺少明顯的東西。 我在日志中沒有看到任何問題。

我唯一的線索是我沒有啟用/ etc / nginx / sites的目錄,並且看起來好像capistrano需要一個目錄,但是在cap部署過程中我沒有看到任何錯誤(除了我在底部)。 在這種情況下,它是唯一正在運行的站點,因此我不需要虛擬主機,也不認為我需要它,因此不必擔心。

在此先感謝您的幫助。

這是我的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

一位朋友的回答是:使用乘客而不是獨角獸。 開箱即用。

您可以在https://www.phusionpassenger.com/上找到它

(我確實遇到了一個奇怪的問題,我從github收到了錯誤128。我的deploy.rb中沒有任何更改,只是取消了注釋底部為乘客列出的注釋;當然,我的github帳戶或本地AFAIK中沒有任何更改。不知道發生了什么,但是我刪除了這台機器的github密鑰,創建了一個新密鑰,然后一切正常。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM