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

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

Elastic Load Balancing

Elastic Load Balancingは、Amazon社が提供する、 EC2インスタンス間で自動的にトラフィックの負荷分散を行うサービスです。

AWS(Amazon Web Services)

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

Q&A

解決済

2回答

2617閲覧

ALBの経由してアクセスができない

ryouya

総合スコア14

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

Elastic Load Balancing

Elastic Load Balancingは、Amazon社が提供する、 EC2インスタンス間で自動的にトラフィックの負荷分散を行うサービスです。

AWS(Amazon Web Services)

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

0グッド

0クリップ

投稿2020/05/25 14:56

編集2020/05/26 07:35

お世話になっております。
下記問題について知見のある方がいらっしゃいましたらご教示お願いします。

#起きている問題
AWS環境でNginx+Unicorn+Railsを実装し、EC2ALBを経由してアクセスしようとしているのですが、
ALBDNS 名でアクセスするとNginxのデフォルトページが表示されてしまいます。
デプロイしているRailsのアプリケーションを開くためにはどのようにすればよろしいでしょうか。
![イメージ説明

#確認したこと
nginxの構文を確認したところ、サーバー名の長さが上限を超えていました。(ALBのDNS名を設定しているため)

$ sudo nginx -t [sudo] ryouya のパスワード: nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64 nginx: configuration file /etc/nginx/nginx.conf test failed

nginx設定ファイルに『server_names_hash_bucket_size 128;』を追記したいのですが、構文エラーになってしまいます。

#追記時のエラー_1 $ vi coffee_app.conf : server { listen 80; client_max_body_size 4G; server_name {ALBのDNS名}; keepalive_timeout 5; server_names_hash_bucket_size 128; #この一文を追記 : $ sudo nginx -t nginx: [emerg] "server_names_hash_bucket_size" directive is not allowed here in /etc/nginx/conf.d/coffee_app.conf:15 nginx: configuration file /etc/nginx/nginx.conf test failed #追記時のエラー_2 $ vi coffee_app.conf : http { server_names_hash_bucket_size 128; #この一文を追記 server { listen 80; client_max_body_size 4G; server_name {ALBのDNS名}; keepalive_timeout 5; : $ sudo nginx -t nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/coffee_app.conf:10 nginx: configuration file /etc/nginx/nginx.conf test failed

上記、記述方法がわかる方がいらっしゃいましたら、ご教示お願いします。
#関連ファイル
####coffee_app.conf(nginx設定ファイル)

$ cat coffee_app.conf error_log /var/www/rails/coffee_app/log/nginx.error.log; access_log /var/www/rails/coffee_app/log/nginx.access.log; upstream unicorn_server { server unix:/var/www/rails/coffee_app/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; client_max_body_size 4G; server_name {ALBのDNS名}; keepalive_timeout 5; # Location of our static files root /var/www/rails/coffee_app/public; location ~ ^/assets/ { root /var/www/rails/coffee_app/public; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://unicorn_server; break; } } error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/rails/coffee_app/public; } location = /favicon.ico { log_not_found off; } }

####unicorn.conf.rb

$ vi unicorn.conf.rb stdout_path $std_log timeout $timeout listen $listen pid $pid # loading booster preload_app true # before starting processes before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin Process.kill "QUIT", File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end # after finishing processes after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end

#環境
Rails 5.1.6
Ruby 2.5.1
Unicorn 5.4.1
Nginx 1.12.2

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

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

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

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

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

y_waiwai

2020/05/25 15:23

> ALBのDNS 名で検索をかけるとNginxのデフォルトページが表示されてしまいます。 検索とはどこで検索という話かな?
ryouya

2020/05/25 15:35

依頼ありがとうございます。 すみません、正確には「アクセス」でした。 修正します。
yu_1985

2020/05/26 01:13

nginxの設定を貼ってください
ryouya

2020/05/26 01:52

依頼ありがとうございます。 nginxの設定について追記しました。
yu_1985

2020/05/26 08:21

これ以外にもデフォルトのnginx.confがあるかと思うのですが、そちらにserver_names_hash_bucket_sizeの設定はありませんか?
ryouya

2020/05/26 09:07

ご回答ありがとうございます。 申し訳ございません、 デフォルトnginx設定ファイルの格納パスを探す方法をご教示いただけないでしょうかm(_ _)m ``` $ find . -name "nginx" -type d find: ./Pictures/写真ライブラリ.photoslibrary: Operation not permitted ./Desktop/スクリーンショット/environment/portfolio_01/containers/nginx : : find: ./Library/Cookies: Operation not permitted ./.Trash/docker01/nginx ``` findで何度か思い当たる用語で探したのですが見つかりませんでした。。
yu_1985

2020/05/26 09:13

なぜ自分のローカル端末の中を探しているのでしょうか…。 EC2インスタンスの中の、自分で作成したnginxの設定ファイルの配置ディレクトリのあたりにあるはずです。 通常であれば/usr/local/etcか/etcの配下あたりにnginxのディレクトリがあると思われます。
ryouya

2020/05/26 15:13

ご回答ありがとうございます。 また、ご返信遅れましてすみませんm(_ _)m ローカル直下にあるものだと勘違いしていました汗 本件、/etc/nginx/nginx.confにserver_names_hash_bucket_size 128;を追記したら解決いたしました。 ご教示してくださりありがとうございましたm(_ _)m
guest

回答2

0

自己解決

解決しました。

■原因1
coffee_app.conf(nginx設定ファイル)のサーバー名がEC2Elastic IPアドレスを使用していた。
■解決方法
coffee_app.conf(nginx設定ファイル)のサーバー名をALBDNS名に変更

■原因2
coffee_app.conf(nginx設定ファイル)のサーバー名がALBDNS名のため長文でありNginxがエラーで起動しない
■解決方法
/etc/nginx/nginx.conf(nginxデフォルト設定ファイル)にserver_names_hash_bucket_size 128;を追記

投稿2020/05/26 15:23

ryouya

総合スコア14

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

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

0

nginxで設定しているサーバー名とDNSに登録してある名前が違っていませんか?

投稿2020/05/26 00:41

winterboum

総合スコア23329

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

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

ryouya

2020/05/26 01:40 編集

ご回答ありがとうございます。 nginx設定ファイルを確認したところ、ご指摘通り違っておりEC2のIPアドレスとなっておりました。 なので、サーバー名をELBのDNS 名に変更しました。 ``` #変更前 $ cat {アプリ名}.conf : server { listen 80; client_max_body_size 4G; server_name {EC2のElastic IP アドレス}; keepalive_timeout 5; : #変更後 $ cat {アプリ名}.conf : server { listen 80; client_max_body_size 4G; server_name {ELBのDNS 名}; keepalive_timeout 5; : ``` nginx設定ファイルを変更後の状態でnginxを起動したところ、 起動が失敗してしまいました。 ``` $ sudo service nginx restart [sudo] ユーザー のパスワード: Redirecting to /bin/systemctl restart nginx.service Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. ``` systemctl status nginx.serviceとjournalctl -xeを確認したところ下記が返ってきました。 ``` $ systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since 火 2020-05-26 01:31:28 UTC; 1min 42s ago Process: 15484 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 16422 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE) Process: 16421 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 15486 (code=exited, status=0/SUCCESS) $ journalctl -xe Hint: You are currently not seeing messages from other users and the system. Users in groups 'adm', 'systemd-journal', 'wheel' can see all messages. Pass -q to turn off this notice. ・・・ sshd[3739]: Received disconnect from 60.112.169.243 port 50419:11: disconnected by user ・・・ sshd[3739]: Disconnected from 60.112.169.243 port 50419 : : ・・・sshd[13642]: Received disconnect from 60.112.169.243 port 56695:11: disconnected by user ```
ryouya

2020/05/26 07:19

nginxの構文を確認したところ、サーバー名の長さが上限を超えていました。(ALBのDNS名を設定しているため) ``` $ sudo nginx -t [sudo] ryouya のパスワード: nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64 nginx: configuration file /etc/nginx/nginx.conf test failed ``` nginx設定ファイルに『server_names_hash_bucket_size 128;』を追記したいのですが、構文エラーになってしまいます。 ``` #追記時のエラー_1 : server { listen 80; client_max_body_size 4G; server_name {ALBのDNS名}; keepalive_timeout 5; server_names_hash_bucket_size 128; #この一文を追記 : $ sudo nginx -t nginx: [emerg] "server_names_hash_bucket_size" directive is not allowed here in /etc/nginx/conf.d/coffee_app.conf:15 #追記時のエラー_2 : http { server_names_hash_bucket_size 128;#この一文を追記 server { listen 80; client_max_body_size 4G; server_name {ALBのDNS名}; keepalive_timeout 5; : $ sudo nginx -t nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/coffee_app.conf:10 nginx: configuration file /etc/nginx/nginx.conf test failed ```
winterboum

2020/05/26 09:41

DNSにもっと短い名前**も**登録しましょう。 Aレコードで、Aliasを使います
ryouya

2020/05/26 15:15

ご回答ありがとうございます。 また、ご返信遅れましてすみませんm(_ _)m 本件、/etc/nginx/nginx.confにserver_names_hash_bucket_size 128;を追記したら解決いたしました。 ご教示してくださったAliasを使用する方法も参考にさせていただきます。 ご回答ありがとうございましたm(_ _)m
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問