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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

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

nginx

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

AWS(Amazon Web Services)

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

Q&A

解決済

1回答

22956閲覧

nginxで(111: Connection refused)が出て、アクセスできないです。

takayaEbino

総合スコア4

Ruby on Rails 6

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

nginx

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

AWS(Amazon Web Services)

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

0グッド

0クリップ

投稿2020/12/03 12:51

nginxがrailsサーバーに接続してません

nginxが急に接続できなくなり、We're sorry, but something went wrong.が表示されました。
現在 failed (111: Connection refused) while connecting to upstreamというエラーが発生してしまい、ウェブサイトにアクセスできなくなりました。
以下が、nignx.error.logの内容です。

error

1 while connecting to upstream, client: 10.0.1.171, server: www.sumigokochi2020.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/rails/rails_app/tmp/sockets/puma.sock:/", host: "10.0.0.222" 22020/12/03 12:22:24 [error] 3277#0: *439 connect() to unix:/var/www/rails/rails_app/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: 10.0.0.196, server: www.sumigokochi2020.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/rails/rails_app/tmp/sockets/puma.sock:/", host: "10.0.0.222"

rails s -e productionを実行すると以下の結果が得られます。

=> Booting Puma => Rails 6.0.3.4 application starting in production => Run `rails server --help` for more startup options Puma starting in single mode... * Version 4.3.6 (ruby 2.7.2-p137), codename: Mysterious Traveller * Min threads: 5, max threads: 5 * Environment: production * Listening on unix:///var/www/rails/rails_app/tmp/sockets/puma.sock Use Ctrl-C to stop

error.log

connect() to unix:/var/www/rails/rails_app/tmp/sockets/puma.sock

rails s -e production実行結果の

* Listening on unix:///var/www/rails/rails_app/tmp/sockets/puma.sock

のListening先とconnect先が違うことがエラーの原因なのではないかと考えているのですが、どなたかわかる人がいました、教えて頂きたいです。
railsproduction.logが一切更新されてないので、Nginxの問題かなとは考えています。
## 本番環境

  • AWS EC2 Linux
  • Rails 6.0.3
  • Nginx

Nginx.conffile

nginx/conf:nginx

1 2# For more information on configuration, see: 3# * Official English Documentation: http://nginx.org/en/docs/ 4# * Official Russian Documentation: http://nginx.org/ru/docs/ 5 6user nginx; 7worker_processes auto; 8error_log /var/log/nginx/error.log; 9pid /run/nginx.pid; 10 11# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 12include /usr/share/nginx/modules/*.conf; 13 14events { 15 worker_connections 1024; 16} 17 18http { 19 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 20 '$status $body_bytes_sent "$http_referer" ' 21 '"$http_user_agent" "$http_x_forwarded_for"'; 22 23 access_log /var/log/nginx/access.log main; 24 25 sendfile on; 26 tcp_nopush on; 27 tcp_nodelay on; 28 keepalive_timeout 65; 29 types_hash_max_size 4096; 30 31 include /etc/nginx/mime.types; 32 default_type application/octet-stream; 33 34 # Load modular configuration files from the /etc/nginx/conf.d directory. 35 # See http://nginx.org/en/docs/ngx_core_module.html#include 36 # for more information. 37 include /etc/nginx/conf.d/*.conf; 38 # server{}欄を変更して,httpでアクセスされたものをhttpsにリダイレクトさせる 39 server { 40 listen 80; 41 listen [::]:80; 42 server_name www.sumigokochi2020.com; 43 root /usr/share/nginx/html; 44 45 # Load configuration files for the default server block. 46 include /etc/nginx/default.d/*.conf; 47 48 error_page 404 /404.html; 49 location = /40x.html { 50 } 51 52 error_page 500 502 503 504 /50x.html; 53 location = /50x.html { 54 } 55 } 56}

nginx/conf.d/rails_app.conf

# log directory error_log /var/www/rails/rails_app/log/nginx.error.log; #自分のアプリケーション名に変更 access_log /var/www/rails/rails_app/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/rails_app/tmp/sockets/puma.sock fail_timeout=0; #自分のアプリケーション名に変更 } server { listen 80; server_name www.sumigokochi2020.com; #自分のElasticIP # nginx so in:creasing this is generally safe... keepalive_timeout 5; # path for static files root /var/www/rails/rails_app/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/rails_app/public; #自分のアプリケーション名に変更 } }

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

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

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

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

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

neko_daisuki

2020/12/04 00:27

We're sorry, but something went wrong は rails のエラーのはずですが failed (111: Connection refused) while connecting to upstream となるまでの間に何か作業はされましたか? puma.rb に pidfile '/var/www/rails/rails_app/tmp/pids/puma.pid' みたいな行があると思うのですが、 そのファイルを cat で開くと番号が書かれてます。 `ps aux | grep 番号` とすると puma 4.3.6 (unix:///var/www/rails/rails_app/tmp/sockets/puma.sock) と表示されますか?
takayaEbino

2020/12/04 03:26 編集

返信ありがとうございます。`ps aux | grep 番号` で確認したところ、 serverは`unix:///var/www/rails/rails_app/tmp/sockets/puma.sock`で、 '/etc/nginx/conf.d/rails_app.conf'のupstream_serverと違うので、修正してみたところ、無事に繋がりました。 git stashとかしたので、そこの都合でcodeが少し変形してしまい、upstream server先と、pumaの指定先がズレたのだと思います。 本当にありがとうございました。
yu_1985

2020/12/04 06:15

どうでもいいですがgit stashしたらコミットしてない変更が一時退避されるのでむしろよくて、間違えて行ったのは`git push`ではないでしょうか。
yu_1985

2020/12/04 06:17

あ、でもそうとも限らないか。 いずれにしても、リポジトリにあるものが正、というのが望ましい状態ですね。
takayaEbino

2020/12/04 07:40

git pushが原因で今回のことが発生したことは間違いないと思います。 いくつか本番環境上で直接設定していた設定がgit pullによって吹き飛んで、errorになったんだなと感じています。 なので、開発環境にproduction用の設定を全て書き写して、mergeして、動作するようにしました。 リポジトリとはできるだけ近づけて置かないと、いらぬエラーを発生させてしまいますね。
yu_1985

2020/12/04 08:06

「近づけておく」、ではなくて「リポジトリにあるものが正しい」、という状態にしてください。
takayaEbino

2020/12/04 08:11

わかりました。 気をつけておきます。
guest

回答1

0

自己解決

puma.rb に pidfile '/var/www/rails/rails_app/tmp/pids/puma.pid' みたいな行があると思うのですが、

そのファイルを cat で開くと番号が書かれてます。
ps aux | grep 番号 とすると puma 4.3.6 (unix:///var/www/rails/rails_app/tmp/sockets/puma.sock) と表示されますか?

これを確かめてみたところ、
serverはunix:///var/www/rails/rails_app/tmp/sockets/puma.sockで、
/etc/nginx/conf.d/rails_app.confのupstream_server先は、

upstream app_server { # for UNIX domain socket setups server unix:/var/www/rails/rails_app/tmp/sockets/puma.sock fail_timeout=0; #自分のアプリケーション名に変更 }

でした。
rails_app.conf内のserver unix:/var/www/rails/rails_app/tmp/sockets/puma.sock
pumaunix:///var/www/rails/rails_app/tmp/sockets/puma.sock
が明らかにずれていたので、upstream先をpumaに合わせると無事に繋がりました。
多分本番環境で間違えて編集したものを、git stashしてしまい、その結果codeにズレが発生したのが、原因だと思います。
できる限り、本番環境では、git pullだけにしないといけないとつくづく実感しました。

投稿2020/12/04 03:25

編集2020/12/04 03:27
takayaEbino

総合スコア4

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問