エラー内容
こちらの記事を参考にローカルで作成したrailsアプリケーションをec2を用いてデプロイしてみましたが、ブラウザからアクセスすると500エラーが返ってきます。
nginxのエラーログを見てみると
2021/03/06 02:07:48 [crit] 20728#0: *2 connect() to unix:/var/www/rails/{myapp}/tmp/sockets/.unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: x.x.x.x, server: x.x.x.x, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/rails/{myapp}/tmp/sockets/.unicorn.sock:/", host: "x.x.x.x"
ソケットが無いと出ているので確認してみると、
shell
1$ ls -a /var/www/rails/{myapp}/tmp/sockets 2. .. .unicorn.sock
となっているので、ソケット自体はあります。
設定ファイルなど
/etc/nginx/conf.d/{myapp}.conf
# log directory error_log /var/www/rails/{myapp}/log/nginx.error.log; access_log /var/www/rails/{myapp}/log/nginx.access.log; # max body size client_max_body_size 2G; upstream app_server { # for UNIX domain socket setups server unix:/var/www/rails/{myapp}/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; server_name x.x.x.x; # nginx so increasing this is generally safe... keepalive_timeout 5; # path for static files root /var/www/rails/{myapp}/public; # page cache loading try_files $uri/index.html $uri.html $uri @app; location @app { # HTTP headers proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } # Rails error pages error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/rails/{myapp}/public; } }
/var/www/rails/{myapp}/config/unicorn.conf.rb
ruby
1 # set lets 2 $worker = 2 3 $timeout = 30 4 $app_dir = "/var/www/rails/{myapp}" 5 $listen = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir 6 $pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir 7 $std_log = File.expand_path 'log/unicorn.log', $app_dir 8 # set config 9 worker_processes $worker 10 working_directory $app_dir 11 stderr_path $std_log 12 stdout_path $std_log 13 timeout $timeout 14 listen $listen 15 pid $pid 16 # loading booster 17 preload_app true 18 # before starting processes 19 before_fork do |server, worker| 20 defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! 21 old_pid = "#{server.config[:pid]}.oldbin" 22 if old_pid != server.pid 23 begin 24 Process.kill "QUIT", File.read(old_pid).to_i 25 rescue Errno::ENOENT, Errno::ESRCH 26 end 27 end 28 end 29 # after finishing processes 30 after_fork do |server, worker| 31 defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection 32 end
試したこと
- ブラウザからnginxへの接続ができているか確認
nginx.access.logを見てみたが、ブラウザからアクセスするたびにログが1つずつ増えていた。
- unicornが起動できているか確認
shell
1$ ps -ef | grep unicorn | grep -v grep 2root 21637 1 0 02:37 ? 00:00:01 unicorn_rails master -c /var/www/rails/{myapp}/config/unicorn.conf.rb -D -E production 3root 21639 21637 0 02:37 ? 00:00:00 unicorn_rails worker[0] -c /var/www/rails/{myapp}/config/unicorn.conf.rb -D -E production 4root 21640 21637 0 02:37 ? 00:00:00 unicorn_rails worker[1] -c /var/www/rails/{myapp}/config/unicorn.conf.rb -D -E production
起動しているっぽい。
- ソケットの権限を調べてみる
shell
1$ ll -a /var/www/rails/{myapp}/tmp/sockets/ 2合計 0 3drwxrwxrwx 2 username username 27 3月 6 02:59 . 4drwxrwxrwx 7 username username 125 3月 6 02:08 .. 5srwxrwxrwx 1 username username 0 3月 6 02:59 .unicorn.sock
あなたの回答
tips
プレビュー