1、Nginx反向代理配置:php
一、虛擬主機配置前端
location / {
try_files $uri @apache;
}
location @apache {
internal;
proxy_pass http://127.0.0.1:8080;
include proxy.conf;
}git
location ~ .*\.(php|php5)?$ {
proxy_pass http://127.0.0.1:8080;
include proxy.conf;
}github
2.代理配置apache
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_buffers大小
proxy_temp_file_write_size 64k;#代理緩存臨時文件大小
proxy_max_temp_file_size 128m;#代理最大緩存臨時文件大小
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;
2、Apache獲取真實IP模塊
獲取真實IP地址有Apache有2個模塊:
mod_rpaf:Apache-2.2支持;Apache-2.4不支持。網上教程不少,並且官網連接已失效
mod_remoteip:Apache-2.4自帶模塊;Apache-2.2支持;推薦後端
A、使用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地址,有幾個填幾個
3.測試cookie
# /usr/local/apache/bin/apachectl -t
# /usr/local/apache/bin/apachectl restart
# 看日誌是否獲取到了真實IP
B、使用mod_remoteip模塊
一、Apache-2.2下配置mod_remoteip以下:
1)、安裝ide
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
2)、修改配置文件:
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
3)、測試:
# /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
注意:
一、替換remote.c文件中remote_addr和remote_ip爲client_addr和client_ip