安装mod_rpaf
wget
tar xvfz mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
#这里是apache 2.x的安装命令,1.x的不是这样
(#/usr/local/apache/bin/apxs -i -a -c mod_rpaf.c Apache 1.3.x 的安装)
修改apache的配置文件
做完前面工作后,/usr/local/apache/modules目录多出mod_rpaf-2.0.so
现在要做的就是让apache加载这个模块
编辑/usr/local/apache/conf/httpd.conf
在LoadModule区域插入以下文本
1 LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 #ip换成前端nginx服务器ip
RPAFheader X-Forwarded-For
保存配置文件,重启apache
修改nginx的配置文件
编辑/usr/local/nginx/conf/nginx.conf
加入以下文本
1 proxy_set_header Host $host;
2 proxy_set_header X-Real-IP $remote_addr;
3 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
保存配置文件,重启nginx
让apache日志记录正确的访客IP
在LogFormat中加入 %{X-Forwarded-For}i 例如:
1 LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vrnishcombined
#CustomLog logs/access_log combined
#CustomLog "|/usr/sbin/rotatelogs/var/log/httpd/access_log.%Y%m%d 7200 480" combined(apapche默认)
CustomLog "|/usr/sbin/cronolog /var/log/httpd/access_log.%Y%m%d" combined
php程序获取正确访客IP
if ($_SERVER["HTTP_X_FORWARDED_FOR"]=="") {
$user_ip=$_SERVER["REMOTE_ADDR"];
} else {
$user_ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
}
?>
(X-Forwarded-For 知识)