前提・実現したいこと
Ruby on Railsでオリジナルアプリを作成しています。
手動デプロイ、Nginxの設定は済んでいますが、Capistranoで自動デプロイを実行したところ、502 Bad gatewayのエラーが出て詰まっています。
発生している問題・エラーメッセージ
アプリケーションのディレクトリでbundle exec cap production deployを実行したところ、下記の画面が出てきて自動デプロイはできている認識でいます。
その後、IPアドレスにアクセスすると502 Bad Gatewayとエラーが表示され解決できずにいます。
00:53 deploy:assets:backup_manifest 01 mkdir -p /var/www/app_name/releases/20210502224248/assets_manifest_backup ✔ 01 ec2-user@xx.xxx.xxx.xxx 0.123s 02 cp /var/www/app_name/releases/20210502224248/public/assets/.sprockets-manifest-d9815ef5061edc1f5133a83421dafb39.json /var/www/app_name/releases/20210502224248/assets_manifest_backup ✔ 02 ec2-user@xx.xxx.xxx.xxx 0.129s 00:54 deploy:migrate [deploy:migrate] Run `rake db:migrate` 00:54 deploy:migrating 01 $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate ✔ 01 ec2-user@xx.xxx.xxx.xxx 1.811s 00:56 deploy:symlink:release 01 ln -s /var/www/app_name/releases/20210502224248 /var/www/app_name/releases/current ✔ 01 ec2-user@xx.xxx.xxx.xxx 0.139s 02 mv /var/www/app_name/releases/current /var/www/app_name ✔ 02 ec2-user@xx.xxx.xxx.xxx 0.128s 00:56 unicorn:start 01 $HOME/.rbenv/bin/rbenv exec bundle exec unicorn -c /var/www/app_name/current/config/unicorn.rb -E deployment -D ✔ 01 ec2-user@xx.xxx.xxx.xxx 1.628s unicorn restarting... 02 kill -s USR2 `cat /var/www/app_name/shared/tmp/pids/unicorn.pid` ✔ 02 ec2-user@xx.xxx.xxx.xxx 0.132s 00:58 deploy:cleanup Keeping 5 of 6 deployed releases on xx.xxx.xxx.xxx 01 rm -rf /var/www/app_name/releases/20210501125235 ✔ 01 ec2-user@xx.xxx.xxx.xxx 1.367s 01:00 deploy:log_revision 01 echo "Branch master (at f36ae5f5de6da628e4f4d3f84ba5994fc505539a) deployed as release 20210502224248 by user" >> /var/www/app_name/revisions.log ✔ 01 ec2-user@xx.xxx.xxx.xxx 0.126s
production.logでエラーログを確認しましたが、エラーの箇所は見当たらないように見えます。
production.log
1I, [2021-05-01T08:45:36.561125 #21248] INFO -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Processing by SpotsController#index as */* 2D, [2021-05-01T08:45:36.562277 #21248] DEBUG -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Rendering layout layouts/application.html.erb 3D, [2021-05-01T08:45:36.563497 #21248] DEBUG -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Rendering spots/index.html.erb within layouts/application 4D, [2021-05-01T08:45:36.564273 #21248] DEBUG -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Rendered shared/_header.html.erb (Duration: 0.4ms | Allocations: 171) 5D, [2021-05-01T08:45:36.565003 #21248] DEBUG -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Spot Load (0.3ms) SELECT `spots`.* FROM `spots` ORDER BY RAND() LIMIT 5 6D, [2021-05-01T08:45:36.566505 #21248] DEBUG -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Spot Load (0.2ms) SELECT `spots`.* FROM `spots` 7D, [2021-05-01T08:45:36.567149 #21248] DEBUG -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Rendered shared/_footer.html.erb (Duration: 0.2ms | Allocations: 132) 8I, [2021-05-01T08:45:36.568368 #21248] INFO -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Rendered spots/index.html.erb within layouts/application (Duration: 4.8ms | Allocations: 1002) 9I, [2021-05-01T08:45:36.568846 #21248] INFO -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Rendered layout layouts/application.html.erb (Duration: 5.4ms | Allocations: 1251) 10I, [2021-05-01T08:45:36.569096 #21248] INFO -- : [52ce6ffa-a195-45c2-b82f-e174073cca51] Completed 200 OK in 8ms (Views: 6.4ms | ActiveRecord: 0.5ms | Allocations: 1794) 11
config/unicorn.rbとNginxのファイルの設定は下記のように設定しています。
該当のソースコード
config/unicorn.rb
1app_path = File.expand_path('../../../', __FILE__) 2 3worker_processes 1 4 5working_directory "#{app_path}/current" 6 7pid "#{app_path}/shared/tmp/pids/unicorn.pid" 8 9listen "#{app_path}/shared/tmp/sockets/unicorn.sock" 10 11stderr_path "#{app_path}/shared/log/unicorn.stderr.log" 12 13stdout_path "#{app_path}/shared/log/unicorn.stdout.log" 14 15timeout 60 16 17preload_app true 18GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true 19 20check_client_connection false 21 22run_once = true 23 24before_fork do |server, worker| 25 defined?(ActiveRecord::Base) && 26 ActiveRecord::Base.connection.disconnect! 27 28 if run_once 29 run_once = false # prevent from firing again 30 end 31 32 old_pid = "#{server.config[:pid]}.oldbin" 33 if File.exist?(old_pid) && server.pid != old_pid 34 begin 35 sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU 36 Process.kill(sig, File.read(old_pid).to_i) 37 rescue Errno::ENOENT, Errno::ESRCH => e 38 logger.error e 39 end 40 end 41end 42 43after_fork do |_server, _worker| 44 defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection 45end
Nginx
1upstream app_server { 2 server unix:/var/www/app_name/shared/tmp/sockets/unicorn.sock; 3} 4 5server { 6 listen 80; 7 server_name xx.xxx.xxx.xxx; 8 client_max_body_size 2g; 9 10 root /var/www/app_name/current/public; 11 12# assetsファイル(CSSやJavaScriptのファイルなど)にアクセスが来た際に適用される設定 13 location ^~ /assets/ { 14 gzip_static on; 15 expires max; 16 add_header Cache-Control public; 17 root /var/www/app_name/current/public; 18 } 19 20 try_files $uri/index.html $uri @unicorn; 21 22 location @unicorn { 23 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 24 proxy_set_header Host $http_host; 25 proxy_redirect off; 26 proxy_pass http://app_server; 27 } 28 29 error_page 500 502 503 504 /500.html; 30}
エラーの原因がわからず、他におかしいと思われる場所がわかりましたらご教示いただけないでしょうか。
回答1件
あなたの回答
tips
プレビュー