反向代理蜘澜,后端Apache獲取真實IP地址
在配置負載均衡時施流,nginx做代理服務器,Apache做web服務鄙信,但是Apache獲取不到真實用戶
的IP瞪醋,需要給Apache增加模塊mod_rpaf
1.Apache設置:
安裝gcc:yum install gcc
安裝httpd-devel:yum install httpd-delvel
安裝模塊mod_rpaf:
#解壓縮以后直接make install
wget -O mod_rpaf.zip https://github.com/gnif/mod_rpaf/archive/stable.zip
unzip mod_rpaf.zip
cd mod_rpaf-stable
make install
修改主配置文件httpd.conf日志配置模塊如下:
<IfModule log_config_module>
? ? #replace %b with %O for more accurate logging
? ? <IfModule mod_logio.c>
? ? ? LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
? ? ? LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O" common
? ? ? LogFormat "%O %I" bytes
? ? ? LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
? ? </IfModule>
? ? CustomLog /var/log/httpd/access_log common
</IfModule>
在模塊配置目錄/etc/httpd/conf.modules.d/增加rpaf模塊引用配置文件rpaf.conf,代碼如下:
#mod_rpaf模塊加載文件
LoadModule? ? ? ? ? ? ? rpaf_module modules/mod_rpaf.so
RPAF_Enable? ? ? ? ? ? ?On
#代理服務器的IP地址
RPAF_ProxyIPs? ? ? ? ? ?192.168.73.130
RPAF_SetHostName? ? ? ? On
RPAF_SetHTTPS? ? ? ? ? ?On
RPAF_SetPort? ? ? ? ? ? On
RPAF_ForbidIfNotProxy? ?Off
2.Nginx設置:
修改配置文件,在反向代理配置代碼中加入參數proxy_set_header装诡,代碼參考如下:
#轉發(fā)請求到負載web服務器
? ? location / {
? ? ? ? proxy_set_header Host $host;
? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? proxy_set_header REMOTE-HOST $remote_addr;
? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? #轉發(fā)到loadbalance組web服務器
? ? ? ? proxy_pass http://192.168.247.134:8080;
? ? }