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 webmaster@localhost
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

コメント

タイトルとURLをコピーしました