介绍
在使用CDN以后,apache日志中显示的是CDN的IP,而访客真实IP是需要使用第三方模块获取。根据阿里云CDN的指引(https://help.aliyun.com/knowledge_detail/40535.html),需要使用一个叫做mod_rpaf的模块。
下载
mod_rpaf经典版本是0.6,网上教程大都基于0.6撰写,实测适用于apache2.2(apache2.4未测,但应该也可用,未测是否支持ssl)。因原作者网站关闭,所以mod_rpaf-0.6.tar.gz在网上不好找了,下面分享两个可用的下载地址。
https://fossies.org/linux/www/apache_httpd_modules/old/mod_rpaf-0.6.tar.gz/
http://dl.kvm.la/softsource/mod_rpaf-0.6.tar.gz
而最新版本已在在github发布。
https://github.com/gnif/mod_rpaf/
安装
#在apache 1.3版本的安装命令: apxs -i -a -c mod_rpaf.c #在apache 2版本的安装命令: apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c #或者直接使用make命令编译 make
#解压缩以后直接make install wget -O mod_rpaf.zip https://github.com/gnif/mod_rpaf/archive/stable.zip unzip mod_rpaf.zip cd mod_rpaf-stable make install
记录一下编译后的.so文件位置
配置
mod_rpaf配置需要找到httpd.conf
(DirectAdmin中路径为/etc/httpd/conf/httpd.conf
),或者添加到拓展文件extra/
文件夹下其他httpd-xxx文件中,比如/extra/httpd-phpmodules.conf
(当然要确定这个拓展文件是被基本配置文件httpd.conf所引用的)。
#.so文件位置必须使用安装后的实际位置,其余参考官方文档 LoadModule rpaf_module /usr/lib/apache/mod_rpaf.so RPAFenable On # Enable reverse proxy add forward RPAFproxy_ips 127.0.0.1 10.0.0.1 # which ips are forwarding requests to us RPAFsethostname On # let rpaf update vhost settings # allows to have the same hostnames as in the "real" # configuration for the forwarding Apache RPAFheader X-Forwarded-For # Allows you to change which header mod_rpaf looks # for when trying to find the ip the that is forwarding # our requests
#mod_rpaf.so文件的位置,必须要根据编译后的显示结果。 LoadModule rpaf_module /usr/lib/apache/mod_rpaf.so RPAF_Enable On RPAF_ProxyIPs 127.0.0.1 8.8.8.8 RPAF_SetHostName On RPAF_SetHTTPS On RPAF_SetPort On RPAF_ForbidIfNotProxy Off # RPAF_ProxyIPs 一般有127.0.0.1[空格]阿里云外网[空格]阿里云内网IP
日志
修改apache日志,让日志获取访客真实ip。需要自定义apache的日志,一般为httpd.conf
基本配置文件。
将其中%a
或者%h
,改为%{X-Forwarded-For}i
下面为我修改后的例子,可做参考。
<IfModule log_config_module> #replace %b with %O for more accurate logging <IfModule mod_logio.c> LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O" common LogFormat "%O %I" bytes LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio </IfModule> CustomLog /var/log/httpd/access_log common </IfModule>