最近在將Apache-2.2和Apache-2.4添加到《lnmp一鍵安裝包》中,Nginx作為前端,Apache作為後端的情況下,Apache只能獲取到Nginx前端的ip地址(127.0.0.1),而無法獲取到用戶的真實ip地址,在這種情況下,後端是Apache如何獲取用戶真實IP地址?
Nginx配置如下:
location /
{
try_files $uri @apache;
}
location @apache
{
internal;
proxy_pass http://127.0.0.1:8080;
include proxy.conf;
}
location ~
.*\.(php|php5)?$ {
proxy_pass http://127.0.0.1:8080;
include proxy.conf;
}
proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4
64k;
proxy_busy_buffers_size 128k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept–Encoding
”;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
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支持;推薦
Apache-2.2.25
mod_rpaf模塊
wget http://stderr.net/apache/rpaf/download/mod_rpaf–0.6.tar.gz
tar –xzvf mod_rpaf–0.6.tar.gz
cd mod_rpaf–0.6/
/usr/local/apache/bin/apxs –i –c –n mod_rpaf–2.0.slo mod_rpaf–2.0.c
添加Apache配置
vi /usr/local/apache/conf/httpd.conf
Include conf/extra/httpd–rpaf.conf
vi /usr/local/apache/conf/extra/httpd-rpaf.conf
LoadModule rpaf_module modules/mod_rpaf–2.0.so
RPAFenable
On
RPAFsethostname
On
RPAFproxy_ips
127.0.0.1
10.8.0.110
# 代理伺服器的ip地址(記得做相應修改)
RPAFheader X–Forwarded–For
備註:RPAFproxy_ips後面添加代理伺服器的ip地址,有幾個填幾個
測試
# /usr/local/apache/bin/apachectl -t
# /usr/local/apache/bin/apachectl restart
# 看日誌
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
Include conf/extra/httpd–remoteip.conf
vi /usr/local/apache/conf/extra/httpd–remoteip.conf
LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X–Forwarded–For
RemoteIPInternalProxy
127.0.0.1
測試:
# /usr/local/apache/bin/apachectl -t
# /usr/local/apache/bin/apachectl restart
# 看日誌
Apache-2.4配置mod_remoteip除了上面(自帶mod_remoteip模塊不需要安裝),還需要修改日誌格式(折騰很久)
LogFormat
“%h %a %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
LogFormat
“%h %a %l %u %t \”%r\” %>s %b” common
LogFormat
“%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\” %I %O” combinedi
在日誌格式中加上%a
以下文章點擊率最高
Loading…