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_rpaf:Apache-2.2支持;Apache-2.4不支持。网上教程很多
mod_remoteip:Apache-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变量