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

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

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

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

Amazon EC2

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

Q&A

解決済

EC2でNginxで502エラーになる

JuniorSirius
JuniorSirius

総合スコア38

nginx

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

Amazon EC2

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

1回答

0グッド

0クリップ

1213閲覧

投稿2022/02/25 01:58

EC2上に/var/www/diary_appとしてアプリを構成しています。
EC2のNginx設定のファイルを/etc/nginx/nginx.confとして起点となるファイルから/etc/nginx/conf.d/app.confと別ファイルを構成しました。

bundle exec rails s -e productionとするとPumaは立ち上がるのでうまく設定したつもりですが502エラーになり、

Nginxのログによるとpuma.sockがないと言われますが、diary_app/tmp/sockets/puma.sockにはソケットは存在していました。

2022/02/24 06:20:08 [crit] 3474#0: *348 connect() to unix:///diary_app/tmp/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.12.137, server: 35.77.159.241, request: "GET / HTTP/1.1", upstream: "http://unix:///diary_app/tmp/sockets/puma.sock:/", host: "10.0.11.191" 2022/02/24 06:20:08 [crit] 3474#0: *348 connect() to unix:///diary_app/tmp/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.12.137, server: 35.77.159.241, request: "GET / HTTP/1.1", upstream: "http://unix:///diary_app/tmp/sockets/puma.sock:/500.html", host: "10.0.11.191"

なのでEC2に設定しているNginx の/etc/nginx/conf.d/diary_app.confの設定の指定場所が誤っているのでしょうか。
色々と/var/wwwを抜いてみたりと書き換えてきましたが相変わらず502エラーが出てしまいます。
ご教授お願いいたします。

ps -aux | grep "puma"でpumaの起動は確認できましたがpuma.sockに繋がらないみたいです

ターミナル1

[sirius@ip-10-0-11-191 diary_app]$ bundle exec rails s -e production => Booting Puma => Rails 5.2.6.2 application starting in production => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.12.6 (ruby 2.6.4-p104), codename: Llamas in Pajamas * Min threads: 5, max threads: 5 * Environment: production * Listening on unix:///var/www/diary_app/tmp/sockets/puma.sock Use Ctrl-C to stop

ターミナル2

[sirius@ip-10-0-11-191 diary_app]$ ps -aux | grep "puma" sirius 12144 0.8 13.3 967692 131876 pts/2 Sl+ 01:38 0:02 puma 3.12.6 (unix:///var/www/diary_app/tmp/sockets/puma.sock) [diary sirius 12295 0.0 0.0 119440 912 pts/3 S+ 01:42 0:00 grep --color=auto puma

nginx.error.log

2022/02/25 01:36:31 [crit] 11943#0: *193 connect() to unix:///diary_app/tmp/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.11.184, server: 35.77.159.241, request: "GET / HTTP/1.1", upstream: "http://unix:///diary_app/tmp/sockets/puma.sock:/500.html", host: "10.0.11.191"

(EC2) /etc/nginx/nginx.conf

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; 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; include /etc/nginx/conf.d/*.conf; index index.html index.htm; upstream puma { server unix:///var/www/diary_app/tmp/sockets/puma.sock; } server { listen 80 default_server; listen [::]:80 default_server; server_name www.diary_app.net; root /var/www/diary_app/public; location / { try_files $uri $uri/index.html $uri.html @webapp; } location @webapp { proxy_read_timeout 300; proxy_connect_timeout 300; proxy_redirect off; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://puma; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 443 ssl; server_name www.diary_app.net; location @webapp { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://puma; } } }

(EC2) /etc/nginx/conf.d/app.conf

# (EC2)/etc/nginx/conf.d/app.conf error_log /var/www/diary_app/log/nginx.error.log; access_log /var/www/diary_app/nginx.access.log; upstream app_server { # for UNIX domain socket setups server unix:/var/www/diary_app/tmp/sockets/puma.sock fail_timeout=0; } server { listen 80; server_name www.diary_app.net; # nginx so increasing this is generally safe... # path for static files root /var/www/diary_app/current/public; # page cache loading try_files $uri/index.html $uri @app_server; location / { # HTTP headers proxy_pass http://app_server; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; } # Rails error pages error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/diary_app/current/public; } client_max_body_size 4G; keepalive_timeout 5; }

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

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

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

下記のような質問は推奨されていません。

  • 質問になっていない投稿
  • スパムや攻撃的な表現を用いた投稿

適切な質問に修正を依頼しましょう。

回答1

0

自己解決

EC2でNginxを実行してエラーが起きていました。
そして、デフォルトのNginxファイルはEC2では/etc/nginx/nginx.confファイルが設置されているため、そこを起点に/etc/nginx/conf.d/*.confを読み込むことが主流になっており、
何かしらそのデフォルトのファイルを書き換えて不具合が起きていたと思うので、yum remove nginxとしてsudo amazon-linux-extras install nginx1とした上で再インストールをした上で(nginx1と書く)、
/etc/nginx/conf.d/app.confと任意の設定ファイルを記述したらいけました。
なので自分の場合は
EC2のNginxの環境再編成起動ファイルの編成で解決となりました。

/etc/nginx/conf.d/app.conf

# /etc/nginx/conf.d/app.confとしてEC2に設置 # log directory error_log /var/www/diary_app/log/nginx.error.log; access_log /var/www/diary_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/diary_app/tmp/sockets/puma.sock fail_timeout=0; } server { listen 80; server_name www.diary_app.net; #ElasticIP # nginx so increasing this is generally safe... keepalive_timeout 5; # path for static files root /var/www/diary_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/diary_app/public; } }

投稿2022/02/25 03:37

編集2022/02/25 03:38
JuniorSirius

総合スコア38

下記のような回答は推奨されていません。

  • 質問の回答になっていない投稿
  • スパムや攻撃的な表現を用いた投稿

このような回答には修正を依頼しましょう。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.83%

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

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

質問する

関連した質問

同じタグがついた質問を見る

nginx

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

Amazon EC2

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