2

以下を使用して、本番モードのテスト アプリケーションを VPN にデプロイしようとしています。

  • デジタルオーシャンの新しくきれいな液滴
  • Ubuntu 14.10
  • レール 4.2.0
  • ルビー 2.1.5
  • RVM
  • PostgreSQL
  • ユニコーン
  • カピストラーノ 3.2.1
  • ニンクス

アプリは正常にデプロイされましたが、500.html がスローされました。

アプリは移行を行い、パブリック ディレクトリにアセットを取得しました。展開中およびログ (production.log、unicorn_error.log、unicorn.log) にエラーはありませんでした。ユニコーンの労働者をチェックps aux|grep unicornしたところ、数人の労働者がそれに取り組んでいました。本番環境の Rails コンソールも動作していたので、PostgreSQL との接続がありました。

ローカル PC で実稼働環境でアプリを起動しようとしたところ、rake RAILS_ENV=production assets:precompile.

問題を見つけるのを手伝ってください。問題の原因を特定するにはどうすればよいですか? それは私の展開コードにありますか、それともサーバーの設定にありますか?

Nginx 設定 /etc/nginx/sites-available/default

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

server {
        listen 80 default deferred;
        #server_name example.com;
        root /var/www/app/current/public;

        location ^~ /assets/ {
                gzip_static on;
                expires max;
                add_header Cache_Control public;
                #try_files $uri/index.html $uri.html $uri @app;
        }

        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;
        keepalive_timeout 10;
}

私が使用するGemfileで

group :development do
  gem 'capistrano', '3.2.1', require: false
  gem 'capistrano-rails', require: false
  gem 'capistrano-bundler', require: false
  gem 'rvm1-capistrano3', require: false
  gem 'capistrano3-unicorn', require: false
end

group :production do
  gem 'unicorn'
end

/config/deploy.rb

set :application, 'app'
set :user, "XXX"

set :scm, :git
#set :branch, ->{ `git rev-parse --abbrev-ref HEAD`.chomp }
set :branch, "master"
set :repo_name, 'deployment'
set :repo_url, ->{ "git@github.com:sfolt/#{fetch :repo_name}.git" }

set :rails_env, fetch(:stage)
set :rvm1_ruby_version, 'ruby-2.1.5'

set :keep_releases, 5
set :format, :pretty
set :use_sudo, false
set :deploy_via, :remote_cache

set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
set :bundle_without, [:development, :test]

set :linked_files, %w{
  config/database.yml
  config/secrets.yml
}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets tmp/sessions}

namespace :assets do
  task :precompile do
    run "cd #{release_path}; rake assets:precompile RAILS_ENV=production"
  end
end

namespace :deploy do
  task :restart do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
  end
  task :start do
    run "bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
  end
  task :stop do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
  end
end

after 'deploy:finishing', 'deploy:cleanup'
after 'deploy:publishing', 'unicorn:restart'

/config/deploy/production.rb

set :stage, :production
set :deploy_to, "/var/www/app"

server 'XXX.XXX.XXX.XXX',
  user: 'XXX',
  port: XXX,
  roles: [:app, :web, :db],
  ssh_options: {
    user: 'XXX'
  }

/config/unicorn/production.rb

deploy_to  = "/var/www/app"
rails_root = "#{deploy_to}/current"
pid_file   = "#{deploy_to}/shared/pids/unicorn.pid"
socket_file= "#{deploy_to}/shared/unicorn.sock"
log_file   = "#{rails_root}/log/unicorn.log"
err_log    = "#{rails_root}/log/unicorn_error.log"
old_pid    = pid_file + '.oldbin'

timeout 120
worker_processes 2
listen socket_file, backlog: 1024
pid pid_file
stderr_path err_log
stdout_path log_file

preload_app true

GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "#{rails_root}/Gemfile"
end

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
  ActiveRecord::Base.connection.disconnect!

  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
  ActiveRecord::Base.establish_connection
end
4

1 に答える 1

1

502 エラーは、nginx とユニコーン プロセスの間に通信がないことを意味します。

構成ファイルを調べると、このソケットでリッスンするように unicorn を設定しているのに、このソケット/var/www/app/shared/unicorn.sockを介して unicorn と通信するように nginx に指示しているように思えます。/tmp/unicorn.app.sock

upstreamファイルの一部を次のように変更nginx.confすると、問題が修正されます。

upstream unicorn {
  server unix:/var/www/app/shared/unicorn.sock fail_timeout=0;
}
于 2014-12-27T23:15:39.093 に答える