質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

0回答

1203閲覧

【Nginx+Unicorn+Rails】ec2にデプロイしたアプリに接続できない

eici

総合スコア3

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2021/03/06 03:17

エラー内容

こちらの記事を参考にローカルで作成した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 36 02:59 . 4drwxrwxrwx 7 username username 125 36 02:08 .. 5srwxrwxrwx 1 username username 0 36 02:59 .unicorn.sock

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問