Capistranoを用いてRails appをデプロイした後にサイトを見ると
502 Bad Gatewayと表示されてしまいます。
ログには下記が記録されていました。
packet_write_wait: Connection to [IPアドレス]: Broken pipe1: Connection refused) while connecting to upstream
エラーの解消方法を教えてください。
環境
- CentOS7
- ruby (2.2.3p173)
- Rails (5.0.0)
- unicorn
- nginx (1.11.1)
- Capistrano
現在の確認済み事項
config/unicorn/staging.rb の
listenの参照先(/var/www/vhosts/mydomain/shared/tmp/sockets/unicorn.sock)と
nginxのupstreamの参照先(var/www/vhosts/mydomain/shared/tmp/sockets/unicorn.sock)が一致していることは確認済みです。
何か他にも必要な情報がございましたら
追記していきますので、どうか助言をよろしくお願い致します
ソース
config/unicorn/staging.rb
ruby
1app_path = '/var/www/vhosts/mydomain' 2 3worker_processes 2 4working_directory "#{app_path}" + "/current" 5 6# This loads the application in the master process before forking 7# worker processes 8# Read more about it here: 9# http://unicorn.bogomips.org/Unicorn/Configurator.html 10preload_app true 11 12timeout 30 13 14# This is where we specify the socket. 15# We will point the upstream Nginx module to this socket later on 16listen "#{app_path}/shared/tmp/sockets/unicorn.sock", :backlog => 64 17 18pid "#{app_path}/shared/tmp/pids/unicorn.pid" 19 20# Set the path of the log files inside the log folder of the testapp 21stderr_path "#{app_path}/current/log/unicorn.stderr.log" 22stdout_path "#{app_path}/current/log/unicorn.stdout.log" 23 24before_exec do |server| 25 ENV['BUNDLE_GEMFILE'] = "#{app_path}/current/Gemfile" 26end 27 28before_fork do |server, worker| 29 defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! 30 # Before forking, kill the master process that belongs to the .oldbin PID. 31 # This enables 0 downtime deploys. 32 old_pid = "#{app_path}/shared/tmp/pids/unicorn.pid.oldbin" 33 if File.exists?(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 38 # someone else did our job for us 39 end 40 end 41 sleep 1 42end 43 44after_fork do |server, worker| 45 defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection 46end
/etc/nginx/nginx.conf
#user nginx; worker_processes 1; include /etc/nginx/modules.conf.d/*.conf; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #tcp_nodelay on; #gzip on; #gzip_disable "MSIE [1-6]\.(?!.*SV1)"; server_tokens off; include /etc/nginx/conf.d/*.conf; } # override global parameters e.g. worker_rlimit_nofile include /etc/nginx/*global_params;
nignx.confのhttp内のブロックからincludeされています
/etc/nginx/conf.d/app_nginx.conf
upstream unicorn_server { server unix:/var/www/vhosts/mydomain/shared/tmp/sockets/unicorn.sock fail_timeout=0; } server { listen 80; client_max_body_size 4G; server_name mydomain; keepalive_timeout 5; # Location of our static files root /var/www/vhosts/mydomain/shared/public; access_log /var/log/nginx/app_access.log; error_log /var/log/nginx/app_error.log; location ~ ^/assets/ { root /var/www/vhosts/mydomain/shared/public; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass https://unicorn_server; } error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/vhosts/mydomain/shared/public; } }
回答のご指摘から単独でアクセスできるか確認してみました。
unicorn/staging.rb に書きを追記しました。
ruby
1listen 8080, :tcp_nopush => true
また回答でいただいたサンプルをもとに
/etc/nginx/conf.d/app_nginx.conf を以下のように変更しました。
upstream default_server { server mydomain; } server { listen 8080; client_max_body_size 4G; server_name mydomain; keepalive_timeout 5; # Location of our static files root /var/www/vhosts/mydomain/shared/public; access_log /var/log/nginx/app_access.log; error_log /var/log/nginx/app_error.log; ssl on; ssl_certificate /etc/pki/tls/certs/mydomain_combined.crt; ssl_certificate_key /etc/pki/tls/private/mydomain.key; ssl_session_timeout 10m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; proxy_buffers 300; proxy_max_temp_file_size 300; proxy_temp_path /dev/shm/nginx_proxy_temp; proxy_connect_timeout 3; proxy_send_timeout 3; proxy_read_timeout 3; location / { try_files $uri $uri/ @app } location @default_server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass https://var/www/vhosts/mydomain/shared/tmp/sockets/unicorn.sock; } error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/vhosts/mydomain/shared/public; }
その後
nginxをstartし
telnetコマンドでポートが開いているか確認しました。
Mac:~ user$ telnet IPアドレス 8080 Trying IPアドレス... Connected to mudomain. Escape character is '^]'. ^] telnet> quit Connection closed.
最後に ping IPアドレス したところ以下のように出力されました
Mac:~ user$ ping IPアドレス PING IPアドレス (IPアドレス): 56 data bytes 64 bytes from IPアドレス: icmp_seq=0 ttl=56 time=16.244 ms 64 bytes from IPアドレス: icmp_seq=1 ttl=56 time=9.381 ms 64 bytes from IPアドレス: icmp_seq=2 ttl=56 time=9.856 ms 64 bytes from IPアドレス: icmp_seq=3 ttl=56 time=7.853 ms 64 bytes from IPアドレス: icmp_seq=4 ttl=56 time=8.616 ms ^C --- IPアドレス ping statistics --- 5 packets transmitted, 5 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 7.853/10.390/16.244/3.005 ms
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/07 21:51