LoginSignup
12
10

More than 3 years have passed since last update.

ELBからEC2のapacheに対するヘルスチェックが「unhealthy」(301エラー)になる

Last updated at Posted at 2019-03-30

結果的には「index.html」を配置していなかったという超凡ミスが原因だが、apacheや.htaccessの設定を理解しておらず、丸一日悩んだ・・・。

環境

AWS EC2にApacheとWordPressをインストールし、AWS ELB(ロードバランサ)を追加。

Untitled Diagram (1) (2).png

事象

EC2にルーティングするためのターゲットグループを作成したところ、ヘルスチェック(※1)が「unhealthy」(301エラー)になった。

※1 EC2 > ロードバランシング(左側のリスト) > ターゲットグループ > ターゲットタブ

image.png

設定確認

ヘルスチェックの設定を確認。httpのポート80に対してヘルスチェックをしている。

image.png

調査

301エラーは対象のページがリダイレクトされているために発生しているとのこと。
このため、EC2へのアクセスは成功しているが、Apacheの何かしらの設定でリダイレクトされていると推測。

1. apacheの設定ファイル を確認。

/etc/httpd/conf/httpd.conf
一番最後の2行に下記の記載がある。
これは、「 /etc/httpd/conf.d」配下の「〇〇.conf」ファイルを全て読み込む、という意味。

/etc/httpd/conf/httpd.conf
# Load config files in the "Cconf.d" directory, if any.
IncludeOptional conf.d/*.conf

2. 「 /etc/httpd/conf.d」配下の設定ファイルを確認

前にバーチャルホストの設定のために追加した設定ファイルを確認。
ドキュメントルートは「/var/www/hoge-documentroot」に設定。
リダイレクトの設定は特にしていない。
が、真ん中らへんの、「AllowOverride All」に注目。
これは、.htaccess で設定可能なものは全て有効にする、という意味。

/etc/httpd/conf.d/hoge.conf
<VirtualHost *:80>
    DocumentRoot "/var/www/hoge-documentroot"
    serverName www.hoge.work
    ErrorLog "logs/hoge-error.log"
    CustomLog "logs/hoge-access.log" combined
    <Directory "/var/www/hoge-documentroot">
        AllowOverride All
    </Directory>
    <Directory "/var/www/hoge-documentroot/wp-admin">
        AuthType Digest
        AuthName wp-admin-area
        AuthUserFile /etc/httpd/conf/htdigest
        Require valid-user
    </Directory>
</VirtualHost>

3. .htaccessを確認

wordPressをインストールしたディレクトリ(ドキュメントルートに設定している)直下に存在する。(デフォルトで配置される)
設定を見ると、リクエストされたファイルまたはディレクトリが存在しない場合、「index.php」にリダイレクトするようになっている。

/var/www/hoge-documentroot/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

linuxのコマンドだと、ls -la で確認できる。(.から始まるファイルは ls -l だと表示されないので注意)

参考

.htaccessとは? - Qiita

原因

ドキュメントルート直下に、「index.html」が存在せず、「index.php」にリダイレクトしていたのが原因と考えられる。

対処

1. ドキュメントルート直下にindex.htmlを作成

/var/www/hoge-documentroot/index.html
hello world

2. apacheを再起動

service httpd restart

対処(2019/4/7追記)

ELB経由で表示したいのはindex.htmlではなくてWordPressのindex.phpだったため、下記の対処を実施する必要があった。

1. ヘルスチェック(※2)のパスを「/index.php」に変更

image.png

※2 EC2 > ロードバランシング(左側のリスト) > ターゲットグループ > ヘルスチェックタブ

2. 「 /etc/httpd/conf.d」配下の設定ファイル を変更。

.httaccessの設定を無効にする。

/etc/httpd/conf.d/hoge.conf
// ... 略
    <Directory "/var/www/tf4-documentroot">
        AllowOverride None
    </Directory>
// ... 略

2. apacheを再起動

service httpd restart

結果

ELBのヘルスチェックが「healty」になった!
image.png

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10