首页 > Web开发 > 详细

前端Nginx,后端Apache获取用户真实IP地址

时间:2015-07-31 10:49:44      阅读:249      评论:0      收藏:0      [点我收藏+]

Nginx作为前端,Apache作为后端的情况下,Apache只能获取到Nginx前端的内网ip地址(10.10.0.*),而无法获取到用户的真实ip地址,在这种情况下,后端是Apache如何获取用户真实IP地址?


nginx 关键配置


            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


获取真实IP地址有Apache有2个模块:
mod_rpafApache-2.2支持;Apache-2.4不支持。网上教程很多
mod_remoteipApache-2.4自带模块;Apache-2.2支持;推荐


mod_remoteip
Apache-2.2下配置mod_remoteip如下:
安装

wget https://github.com/ttkzw/mod_remoteip-httpd22/raw/master/mod_remoteip.c
/usr/local/apache/bin/apxs -i -c -n mod_remoteip.so mod_remoteip.c

修改配置文件:

vi /usr/local/apache/conf/httpd.conf
LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 10.10.0.21
RemoteIPInternalProxy 10.10.0.22

测试:

# /usr/local/apache/bin/apachectl -t

原理,将ng传过来的X-Forwarded-For变量 重写为remote_addr变量



前端Nginx,后端Apache获取用户真实IP地址

原文:http://2804976.blog.51cto.com/2794976/1680410

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!