安裝mod_rpaf
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
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
<IfModule mod_rpaf.c>
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 #ip換成前端nginx服務器ip
RPAFheader X-Forwarded-For
</IfModule>
保存配置文件,重啓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
<?php
if ($_SERVER["HTTP_X_FORWARDED_FOR"]=="") {
$user_ip=$_SERVER["REMOTE_ADDR"];
} else {
$user_ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
}
?>
http://zh.wikipedia.org/wiki/X-Forwarded-For (X-Forwarded-For 知識)
http://www.centos.bz/2011/10/install-mod_rpaf-apache-get-real-ip/php