コンテンツにスキップするには Enter キーを押してください

タグ: Apache

ApacheのアクセスログをCloudFlareに対応させるTips

CloudFlareはリバースプロキシのため、Aapcheのアクセスログをそのまま参照しても本来のアクセス元を参照することができません。
なので、この問題を解決するためのTips。

1.Apacheモジュール「mod_remoteip」の有効化
※ない場合は別途モジュールをビルドしようNE(´^ω^`)

sudo a2enmod remoteip

2.バーチャルホスト等の設定を更新

# sudo vim /etc/apache2/sites-available/000-default.conf とか
ServerName www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html
RemoteIPHeader CF-Connecting-IP #これを追記
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

3.カスタムログ修正

#sudo vim /etc/apache2/apache2.conf
#LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%a %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined

4.CloudFlareのIPを信頼させるためのルールを作成

## 最新のIPリストは https://www.cloudflare.com/ips/ を参照してね
#sudo vim /etc/apache2/conf-available/remoteip.conf

RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32

5.4.の有効化→Apache更新

sudo a2enconf remoteip

sudo apache2ctl configtest
#↑でOKが出たらApacheリロード
sudo systemctl restart apache2.service
コメントする

Basic認証よりセキュアなDigest認証を使う方法

よく.htaccessで、認証制限をかけたいページにこんなの書いてますよね。

AuthType Basic
AuthName "Input your ID and Password."
AuthUserFile /path/to/.htpasswd
require valid-user

然し、Basic認証って実は入力したユーザ名とパスワードを平文で送信してしまうのです。
つまり、通信パケットを監視されていれば、認証に必要な入力情報をカンタンに拾えてしまうという事。

そこで、認証情報をMD5暗号化して送信するDigest認証。
詳しい仕組みは下記を参照のこと。
http://ja.wikipedia.org/wiki/Digest%E8%AA%8D%E8%A8%BC

自分でApacheのコンフィグを操作できる場合、下記コンフィグが読み込まれているか確認しましょう。
レンタルサーバの場合は対応がまちまちなので、各々お問い合わせください。

# Apache2.2以下:/etc/httpd/conf/httpd.conf
# Apache2.4以降:/etc/httpd/conf.module.d/00-base.conf
LoadModule auth_digest_module modules/mod_auth_digest.so

パスワードの作成は「htpasswd」コマンドの代わりに「htdigest」コマンドを使います。
※パスワードファイルの名前は自由ですが、Apacheの標準設定では「.ht」から始まるファイル名は全て表から閲覧できないようになっています。
なので、パスワードファイルを複数作る際は「.htdigest.loginuser」や「.htadmin」といったように
ファイル名が「.ht」から始まるようにしましょう。

htdigest (オプション引数) "(パスワードファイルのパス)" "(レルム)" (ユーザ名)
↓例↓
htdigest -c "/path/to/.htdigest" "Digest Auth" user001

.htaccessには、上記例を参考にするとこういう記述になるはず。
※AuthName(レルム)が一致していること。

AuthType Digest
AuthName "Digest Auth"
AuthUserFile "/path/to/.htdigest"
Require valid-user

追記:Digestパスワードファイルを生成するphpスクリプトを書き起こしました。

Digest認証のパスワードファイルを作成するphpスクリプト

コメントする