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

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

新規登録して質問してみよう
ただいま回答率
85.48%
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の基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

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

Q&A

解決済

1回答

2986閲覧

unicornが起動できず、本番環境でnginxのデフォルトページが表示されてしまう

odennkunn

総合スコア16

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の基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

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

0グッド

1クリップ

投稿2020/01/08 10:56

編集2020/01/08 23:33

Ruby on Railsでアプリケーションを開発後、デプロイしました。
この時点では特に問題はなかったのですが、ドメインを取得したいと考え、AWSのRoute53を使用
https://qiita.com/ymzk-jp/items/ae115ed6d0fd2d383cecの記事を参考にRoute 53は設定しました。
またnginx側での設定も必要であることがわかったため、下記ファイルのsever_nameの部分を取得したドメイン名に変更。しかし、それでもデフォルトページが表示され、変更前の状態に戻してもデフォルトページしか表示されなくなってしまいました。
どうすれば良いか教えていただきたいです。
使用しているのはAWS EC2、Unicorn、MySQLです
unicornを確認したところ、現在は起動ができず、これが原因だと思われます。

/etc/nginx/conf.d/rails.conf

upstream app_server { server unix:/var/www/アプリケーション名/shared/tmp/sockets/unicorn.sock; } server { listen 80; server_name Elastic IP 取得したドメイン名; root /var/www/アプリケーション名/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; root /var/www/アプリケーション名/current/public; } 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://app_server; } error_page 500 502 503 504 /500.html; }
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; index index.html index.htm; server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { }

nginx errorlog

2020/01/08 07:23:34 [error] 13634#0: *5434 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 124.18.104.11, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "lifehacksharing.com", referrer: "http://lifehacksharing.com/" 2020/01/08 11:10:56 [error] 3154#0: *7 connect() to unix:/var/www/lifehacksharing/shared/tmp/sockets/unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 139.162.106.181, server: 3.114.134.212, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/lifehacksharing/shared/tmp/sockets/unicorn.sock:/", host: "3.114.134.212" 2020/01/08 11:12:19 [error] 3154#0: *9 connect() to unix:/var/www/lifehacksharing/shared/tmp/sockets/unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 206.81.24.102, server: 3.114.134.212, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/lifehacksharing/shared/tmp/sockets/unicorn.sock:/", host: "3.114.134.212" 2020/01/08 11:31:34 [error] 3154#0: *16 connect() to unix:/var/www/lifehacksharing/shared/tmp/sockets/unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 34.90.133.193, server: 3.114.134.212, request: "OPTIONS / HTTP/1.0", upstream: "http://unix:/var/www/lifehacksharing/shared/tmp/sockets/unicorn.sock:/", host: "3.114.134.212" 2020/01/08 13:23:05 [error] 3288#0: *13 open() "/usr/share/nginx/html/latest/dynamic/instance-identity/document" failed (2: No such file or directory), client: 44.224.22.196, server: localhost, request: "GET http://169.254.169.254/latest/dynamic/instance-identity/document HTTP/1.1", host: "169.254.169.254" 2020/01/08 13:23:05 [error] 3288#0: *14 open() "/usr/share/nginx/html/latest/dynamic/instance-identity/document" failed (2: No such file or directory), client: 44.224.22.196, server: localhost, request: "GET http://[::ffff:a9fe:a9fe]/latest/dynamic/instance-identity/document HTTP/1.1", host: "[::ffff:a9fe:a9fe]" 2020/01/08 13:23:06 [error] 3288#0: *18 open() "/usr/share/nginx/html/latest/dynamic/instance-identity/document" failed (2: No such file or directory), client: 44.224.22.196, server: localhost, request: "GET /latest/dynamic/instance-identity/document HTTP/1.1", host: "169.254.169.254" 2020/01/08 13:23:06 [error] 3288#0: *19 open() "/usr/share/nginx/html/latest/dynamic/instance-identity/document" failed (2: No such file or directory), client: 44.224.22.196, server: localhost, request: "GET /latest/dynamic/instance-identity/document HTTP/1.1", host: "[::ffff:a9fe:a9fe]"

ターミナルでbundle exec unicorn_rails -c config/unicorn.rb -E production -Dを実行すると

bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails) ArgumentError: config_file=config/unicorn.rb would not be accessible in working_directory=/var/www/current /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:592:in `working_directory' config/unicorn.rb:8:in `reload' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:84:in `instance_eval' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:84:in `reload' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:77:in `initialize' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:77:in `new' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:77:in `initialize' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `new' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `<top (required)>' /home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails:23:in `load' /home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails:23:in `<top (required)>' master failed to start, check stderr log for details

と出て起動ができない状態になっています。

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

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

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

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

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

scsi

2020/01/08 11:51

nginx.conf はどうなってますか?
odennkunn

2020/01/08 12:04 編集

そのファイル自体がないと思います
scsi

2020/01/08 12:08

/etc/nginx/nginx.conf がないのですか?
odennkunn

2020/01/08 12:15

申し訳ございません、ありましたので載せます
guest

回答1

0

ベストアンサー

nginxとUnicornの接続ができていないようです。
まずは以下を確認してみてください

  1. /var/log/nginx/access.log、/var/log/nginx/error.log Unicornのlogではどうなってるか確認する
  2. Unicornは立ち上がってるか ps で確認する
  3. ソケットfile /var/www/アプリケーション名/shared/tmp/sockets/unicorn.sock がUnicorn起動時の時刻で作成されているか確認する

投稿2020/01/08 13:23

winterboum

総合スコア23347

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

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

odennkunn

2020/01/08 14:34

エラーログは追加しました。 unicornは立ち上がっていないみたいです... unicornをRAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -Dで立ち上げようとすると、bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails) ArgumentError: config_file=config/unicorn.rb would not be accessible in working_directory=/var/www/currentといったエラーが出て起動することもできません....
odennkunn

2020/01/08 15:28

自分の考察でしかないのですが、恐らくNginxのデフォルトページが表示されていることから、Nginx周りの設定は間違っておらず、Unicornがうまく起動しないことが、今回の原因だと思います。ただ、今回Unicorn周りの設定は変えておらず、正常に起動していた時と変わらないはずなのに、エラーの原因がわかりません…
winterboum

2020/01/08 20:58

[Nginx周りの設定は間違っておらず、Unicornがうまく起動しない]これはまず正しいと思います。 私は最近pumaになってしまったのでUnicornはどこまでお手伝いできるかわかりませんので、質問のタイトルを変更して、Unicornに詳しい方の目に止まりやすくすることをおすすめします
winterboum

2020/01/08 21:04

ドメイン取得前は動いていたのですね。 1) 変更点は server_name だけですか? 2) Unicorn関係は触っていない? 3) ドメイン取得前はIPでアクセスしていたのでしょうか?それに戻しても同じ?
odennkunn

2020/01/08 22:50

変更点はsever_nameのみです。 unicorn関係は、触った覚えはないですが、強いて言うならば、pumaのバージョンの脆弱性があり、Githubからの通知があり、自分でも気づかないうちにpumaのバージョンを変えたかもしれせん… ドメイン取得前はIPアドレスでアクセスしていました。それを戻しても今はunicornが起動していないので、デフォルトページが表示されてしまっています。
winterboum

2020/01/08 22:53 編集

puma? unicornのversionですよね? ですとそのversionも併記して、質問のタイトル変えましょう
odennkunn

2020/01/08 23:28

まず今unicornのバージョンを確認したところローカルでは5.5.1、本番環境の方では5.4.1でした。バージョンはやはり一致させた方がいいのでしょうか? pumaなのですが、gitから通知がきてBump puma from 3.12.1 to 3.12.2という風に変更していました。 これは関係ないのでしょうか? 本当にお手数おかけして申し訳ございません
winterboum

2020/01/08 23:32

pumaを使っていないのになぜそのmessegeが出たのか疑問ですが、もしかしてGemfileにpumaの記述も残ってるのかな。 関係ないとは思いますが、Unicornと依存関係が絡んでると嫌ですね。 localで Gemfileからpumaを除いて動くことを確認してdeployしてみてください バージョンは一致している方が安心ですが、必須ではないかも
odennkunn

2020/01/08 23:43

pumaを除いて動くことは確認できましたが、deployができませんでした。 エラー内容はこうです。 bundle exit status: 16 (SSHKit::Runner::ExecuteError) bundle stdout: Nothing written bundle stderr: You are trying to install in deployment mode after changing your Gemfile. Run `bundle install` elsewhere and add the updated Gemfile.lock to version control.
winterboum

2020/01/08 23:49

これは deploy のときのですね? your Gemfile. Run `bundle install` elsewhere and add the updated Gemfile.lock to version control. これしてみてください。 bundle install は下と思うので、Gemfile Gemfuile.lock のgit の commit、PUSH を。
odennkunn

2020/01/08 23:59

ご指摘通り実行しましたが、今度は別のエラーがでてしまいます... Exception while executing as ec2-user@3.114.134.212: rake exit status: 1 (SSHKit::Runner::ExecuteError) rake stdout: Nothing written rake stderr: rake aborted! Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問