Nginx做爲前端,Apache做爲後端的狀況下,Apache只能獲取到Nginx前端的內網ip地址(10.10.0.*),而沒法獲取到用戶的真實ip地址,在這種狀況下,後端是Apache如何獲取用戶真實IP地址?前端
nginx 關鍵配置linux
proxy_set_header Host $host;nginx
proxy_set_header X-Real-IP $remote_addr;git
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;github
獲取真實IP地址有Apache有2個模塊:
mod_rpaf:Apache-2.2支持;Apache-2.4不支持。網上教程不少
mod_remoteip:Apache-2.4自帶模塊;Apache-2.2支持;推薦apache
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
修改配置文件:ide
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變量
spa