Nginx做前端处理静态文件,动态文件交给Apache后端处理,这时候出现一个问题就是,查看Apache生成的日志文件,发现访问IP全为127.0.0.1
1.编辑nginx配置文件nginx.conf
#vim /etc/nginx/nginx.conf proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2.安装apache的第三方模块mod_rpaf
下载最新版
# cd /usr/local/src # wget http://www.stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
安装
# tar zxvf 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
如果报如下错误
mod_rpaf-2.0.c: In function 'rpaf_cleanup': mod_rpaf-2.0.c:150: error: 'conn_rec' has no member named 'remote_ip' mod_rpaf-2.0.c:151: error: 'conn_rec' has no member named 'remote_addr' mod_rpaf-2.0.c:151: warning: implicit declaration of function 'inet_addr' mod_rpaf-2.0.c:151: error: 'conn_rec' has no member named 'remote_ip' mod_rpaf-2.0.c: In function 'change_remote_ip': mod_rpaf-2.0.c:164: error: 'conn_rec' has no member named 'remote_ip' mod_rpaf-2.0.c:183: error: 'conn_rec' has no member named 'remote_ip' mod_rpaf-2.0.c:186: error: 'conn_rec' has no member named 'remote_ip' mod_rpaf-2.0.c:187: error: 'conn_rec' has no member named 'remote_addr' mod_rpaf-2.0.c:187: error: 'conn_rec' has no member named 'remote_ip' apxs:Error: Command failed with rc=65536
解决办法:
将mod_rpaf-2.0.c的150、151、164、183、186、187这几行的remote_ip修改成client_ip,remote_addr修改成client_addr
3.安装好后编辑apache配置文件httpd.conf
# vim /etc/httpd/conf/httpd.conf
添加如下代码
LoadModule rpaf_module modules/mod_rpaf-2.0.so RPAFenable On RPAFsethostname On RPAFproxy_ips 127.0.0.1 RPAFheader X-Forwarded-For
重启apache即可实现获取Nginx转发来的真实访客IP地址。