チェ・ゲバムラの日記

脱犬の道を目指す男のブログ

AWSでVirtualhost設定(httpでアクセスされてもhttpsに統一)

大前提

これがないとrewriteできないのでコメントアウトを削除するか、無いなら追記。

httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

 

多分下記の太字のとこが重要かと。

なんか条件増やしてた時は上手くいかなかった。

やっぱりなんかやるときはコードシンプルにしてみるのが一番。

8080はLB→EC2(web1,web2)にいくときにSSLを平文にもどすためのhttpポートの想定。

<VirtualHost *:80>

ServerName XXXXX
DocumentRoot /var/www/XXXXX
ErrorLog /var/log/httpd/error_XXXXX_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/log/httpd/access_XXXXX_log combined
# 送信元IPアドレスとして、X-Forwarded-Forを利用する
RemoteIPHeader X-Forwarded-For
<Directory /var/www/XXXXX>
Options -Indexes
AllowOverride All
Require all granted
#Require ip 127.0.0.1 220.151.146.138 202.241.180.57
</Directory>
</VirtualHost>

 

 

<VirtualHost *:8080>
ServerName XXXX
DocumentRoot /var/www/XXXXX
ErrorLog /var/log/httpd/error_XXXX.jp_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/log/httpd/access_XXXXX_log combined
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule /.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RemoteIPHeader X-Forwarded-For
<Directory /var/www/XXXX>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

 

補足

RemoteIPHeader X-Forwarded-For ってなに

これは主にロードバランサ対策。

LBーWEBサーバ という構成なので、アクセスログとかみると全部LBのIPになってしまうのが普通だが、それを防ぐために元のIPを返してくれるコマンド?の一つ。