本文主要介紹Haproxy負載均衡的安裝配置以及結合keepalived保證高可用,概要如下:
-安裝haproxy
-配置haporxy
-單台haproxy負載均衡配置
-安裝keepalived
-配置keepalived
-haproxy主從配置
-haproxy主從切換測試
-haproxy配置檔案詳解
安裝haproxy
- 環境說明:
-
軟體下載下傳目錄:/opt/soft
安裝目錄:/opt/haproxy
日志目錄:/data/logs/haproxy/haproxy.log
所屬使用者:haproxy.haproxy
擷取haproxy
Haproxy 官網下載下傳位址:http://www.haproxy.org/
cd /opt/soft
Wget http://www.haproxy.org/download//src/haproxy-..tar.gz
編譯haproxy
編譯時指定核心版本target=linux26,你可以# uname –r檢視一下本地的系統核心版本。PREFIX=/opt/haproxy是指定haproxy安裝路徑。
tar -zxvf haproxy-..tar.gz
cd haproxy-.
make TARGET=linux26 PREFIX=/opt/haproxy
make install PREFIX=/opt/haproxy
修改配置檔案
Haproxy安裝完後預設是沒有配置檔案的,需要手動vi haproxy.cfg編譯一個,或者從源碼包目錄examples/haproxy.cfg拷貝過來。
cd /opt/haproxy
cp /opt/soft/haproxy-./examples/haproxy.cfg /opt/haproxy/
修改配置檔案:
vi haproxy.cfg
修改前 | 修改後 |
---|---|
chroot /usr/share/haproxy | chroot /usr/share/haproxy |
uid 99 | uid haproxy |
gid 99 | gid haproxy |
redispatch | redispatch |
contimeout 5000 | timeout connect 5000 |
clitimeout 50000 | timeout client 50000 |
srvtimeout 50000 | timeout server 50000 |
在daemon下面增加一行:
pidfile /opt/haproxy/haproxy.pid
如圖:
可以将檔案中的幾個listen段删除,并在timeout server 下面增加這一段:
listen admin_stats
option httplog
stats refresh
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin
listen webserver1
cookie SERVERID rewrite
balance roundrobin
server app1_1 cookie app1inst1 check inter rise
fall
server app1_2 cookie app1inst2 check inter ri
se fall
server app1_3 cookie app1inst3 check inter ri
se fall
server app1_4 cookie app1inst4 check inter rise
fall
如果不删除的話,則需要将檔案71行左右的
srvtimeout 20000 改為 timeout server 20000,
将最後一行(79行左右)
errorfile 503 /etc/haproxy/errors/503.http
改為
errorfile 503 /opt/haproxy/errorfiles/503.http,
并将解壓後的源碼包目錄下的/opt/soft/haproxy-1.5.9/examples/errorfiles拷貝到/opt/haproxy下,即
cp –r /opt/soft/haproxy-./examples/errorfiles /opt/haproxy
vi /opt/haproxy/haproxy.cfg
最後配置檔案如下:
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log local0
log local1 notice
#log loghost local0 info
maxconn
#chroot /usr/share/haproxy
uid haproxy
gid haproxy
daemon
pidfile /opt/haproxy/haproxy.pid
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries
#redispatch
maxconn
timeout connect
timeout client
timeout server
listen appli1-rewrite
cookie SERVERID rewrite
balance roundrobin
listen appli2-insert
option httpchk
balance roundrobin
cookie SERVERID insert indirect nocache
server inst1 cookie server01 check inter fall
server inst2 cookie server02 check inter fall
capture cookie vgnvisitor= len
option httpclose # disable keep-alive
listen appli3-relais
dispatch
option persist
server inst2 check inter fall backup
listen ssl-relay
option ssl-hello-chk
balance source
server inst1 check inter fall
server inst2 check inter fall
server back1 backup
listen appli5-backup
option httpchk *
balance roundrobin
cookie SERVERID insert indirect nocache
server inst1 cookie server01 check inter fall
server inst2 cookie server02 check inter fall
server inst3 backup check inter fall
capture cookie ASPSESSION len
timeout server
option httpclose # disable keep-alive
option checkcache # block response if set-cookie & cacheab
le
rspidel ^Set-cookie:\ IP= # do not let this cookie tell our intern
al IP address
errorloc http://192.168.114.58/error502.html
errorfile /opt/haproxy/errorfiles/.http
啟動haproxy服務
建立服務啟動環境
useradd haproxy
mkdir /data/logs/haproxy
chown –R haproxy.haproxy /opt/haproxy
chown –R haproxy.haproxy /data/logs/haproxy
為haproxy加上日志支援,
即将配置檔案中的log 127.0.0.1 local0添加具體日志路徑。
vi /etc/rsyslog.conf
增加如下兩行
local0.* /data/logs/haproxy/haproxy.log
local1.* /data/logs/haproxy/haproxy.log
然後修改rsyslog參數,使它支援外來日志。
vi /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=”-r -m 0”
重新開機rsyslog服務。
/etc/init.d/rsyslog restart
編寫haproxy啟動腳本:
#!/bin/sh
#
# haproxy Startup script for the haproxy daemon
#
# chkconfig: 2345 85 15
# description: Start and stop haproxy
# processname: haproxy
# config: /opt/haproxy/haproxy.cfg
# pidfile: /opt/haproxy/haproxy.pid
SROOT=/opt/haproxy
HAPROXY_OWNER=haproxy
RETVAL=
# Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit
fi
start() {
su - $HAPROXY_OWNER -c "$SROOT/sbin/haproxy -f $SROOT/haproxy.cfg"
RETVAL=$?
[ $? -eq ] && echo "Haproxy started..."
}
stop() {
su - $HAPROXY_OWNER -c "kill $(cat $SROOT/haproxy.pid)"
RETVAL=$?
[ $? -eq ] && echo "Haproxy stoped..." || echo "some wrong has occured whe
n stoped the haproxy..."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=
esac
exit $RETVAL
将服務加到開機啟動
chmod +x haproxy.sh
cp haproxy.sh /etc/rc.d/init.d/haproxy
chkconfig –add haproxy
chkconfig –level haproxy on
啟動服務
/etc/init.d/haproxy start
如下則表示啟動成功。
常見啟動錯誤
無法chroot的問題
[[email protected] soft]# /etc/init.d/haproxy start
[ALERT] 349/142919 (45886) : [/opt/haproxy/sbin/haproxy.main()] Cannot chroot(/usr/share/haproxy).
解決方案:将配置檔案裡的chroot /usr/share/haproxy一行注釋掉就好。
FD值,可打開的最大檔案數限制值的問題
[[email protected] ~]# /etc/init.d/haproxy start
[WARNING] 338/142149 (60372) : [/opt/haproxy/sbin/haproxy.main()] Cannot raise FD limit to 8222.
[WARNING] 338/142149 (60372) : [/opt/haproxy/sbin/haproxy.main()] FD limit (1024) too low for maxconn=4096/maxsock=8222. Please raise ‘ulimit-n’ to 8222 or more to avoid any trouble.
解決方案:系統裡的能打開的檔案描述符太小,程式裡的比較大,隻要将系統裡的FD重新調大就好。
ulimit -n
重新開機就好了
檢視系統FD值可以用 ulimit -a
[[email protected] ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7771
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 7771
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
照網上一些部落格說的,有以下方法修改FD值:
1.在/etc/rc.local 中增加一行 ulimit -SHn 65535
2.在/etc/profile 中增加一行 ulimit -SHn 65535
3.在/etc/security/limits.conf最後增加如下兩行記錄
* soft nofile 65535
* hard nofile 65535
修改完後重新登入就可以看到了。
Contimeout參數不支援的問題
[[email protected] haproxy]# /etc/init.d/haproxy start
[WARNING] 338/150533 (60528) : parsing [/opt/haproxy/haproxy.cfg:27] : the ‘contimeout’ directive is now deprecated in favor of ‘timeout connect’, and will not be supported in future versions.
[WARNING] 338/150533 (60528) : parsing [/opt/haproxy/haproxy.cfg:28] : the ‘clitimeout’ directive is now deprecated in favor of ‘timeout client’, and will not be supported in future versions.
[WARNING] 338/150533 (60528) : parsing [/opt/haproxy/haproxy.cfg:29] : the ‘srvtimeout’ directive is now deprecated in favor of ‘timeout server’, and will not be supported in future versions.
解決方案:這說明contimeout這樣的參數類型太老了,新版本已經不支援該類型,請使用 timeout connect這樣的參數類型來使用。類似clitimeout、srvtimeout都是這樣設定。
contimeout 5000
clitimeout 50000
srvtimeout 50000
—>改為
timeout connect 5000
timeout client 50000
timeout server 50000
修改後如圖:
再重新開機服務就好了。
71行的srvtimeout 2000也是這樣改。
Starting haproxy: [WARNING] 349/142919 (45886) : parsing [/opt/haproxy/haproxy.cfg:72] : the ‘srvtimeout’ directive is now deprecated in favor of ‘timeout server’, and will not be supported in future versions.
解決方案:将71行的srvtimeout 20000 改為 timeout server 20000
類似的redispatch也是這樣,隻要将redispatch注釋就好。
[[email protected] ~]# /etc/init.d/haproxy start
[WARNING] 339/041102 (40040) : parsing [/opt/haproxy/haproxy.cfg:22]: keyword ‘redispatch’ is deprecated in favor of ‘option redispatch’, and will not be supported by future versions.
解決方案:将配置檔案中的redispatch 注釋掉就好了。
Socket無法綁定的問題
[[email protected] ~]# /etc/init.d/haproxy restart
Haproxy stoped…
[ALERT] 338/151423 (60563) : Starting proxy webserver1: cannot bind socket [0.0.0.0:80]
解決方案:無法綁定socket,這是因為本地開啟了web服務綁定了80端口,或者其他應用程式占用了該socket。
而且80端口<1024,隻能由root使用者綁定。haproxy普通使用者無法綁定。
隻要将端口号改為其他大于1024的就好。
修改/opt/haproxy/haproxy.cfg裡面的socket,将端口改為了8090,如下圖
再重新開機服務就好了。
Pid檔案無法建立的問題
[[email protected] haproxy]# /etc/init.d/haproxy start
[ALERT] 236/112258 (5538) : [haproxy.main()] Cannot create pidfile /opt/haproxy/haproxy.pid
解決方案:
1、檢視目錄是否存在,不存在則mkdir建立
2、檢視檔案所在位置權限,沒有則chown -R haproxy.haproxy /opt/haproxy
Uid/user無效的問題
[[email protected] ~]# /etc/init.d/haproxy restart
Haproxy stoped…
[WARNING] 338/142429 (60406) : [/opt/haproxy/sbin/haproxy.main()] Failed to drop supplementary groups. Using ‘gid’/’group’ without ‘uid’/’user’ is generally useless.
解決方案:這是對haproxy.cfg裡面指定的使用者識别有問題。隻要将uid與gid指定為我們想要的使用者即可。
—-》改為如下圖
再重新開機就好了。
打開檔案錯誤的問題
[[email protected] ~]# /etc/init.d/haproxy start
[ALERT] 339/082035 (40193) : parsing [/opt/haproxy/haproxy.cfg:87] : error opening file for custom error message <503>.
[ALERT] 339/082035 (40193) : Error(s) found in configuration file : /opt/haproxy/haproxy.cfg
[ALERT] 339/082035 (40193) : Fatal errors found in configuration.
解決方案:打開檔案錯誤,這是因為/etc/haproxy/errors/503.http檔案不存在的問題。源碼包目錄下的examples/errorfiles檔案裡面有該檔案,隻要将它拷貝過來就好。
cp -r /opt/soft/haproxy-./examples/errorfiles /opt/haproxy/
修改配置檔案裡的路徑
vi /opt/haproxy/haproxy.cfg
改為
再重新開機服務就好了。
單台haproxy負載均衡
- 環境說明:
-
haproxy主機:192.168.253.132 8090
web server1:192.168.253.129 80
web server2:192.168.253.130 80
web server3:192.168.253.131 80
修改配置檔案haproxy.cfg
vi /opt/haproxy/haproxy.cfg
增加如下listen字段:
listen admin_stats
option httplog
stats refresh
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin
listen webserver1
cookie SERVERID rewrite
balance roundrobin
server app1_1 cookie app1inst1 check inter rise
fall
server app1_2 cookie app1inst2 check inter ri
se fall
server app1_3 cookie app1inst3 check inter ri
se fall
server app1_4 cookie app1inst4 check inter rise
fall
重新啟動haproxy服務
/etc/init.d/haproxy restart
檢視監控界面
在浏覽器中輸入如上配置的監控位址,http://192.168.253.132:8888/stats就可以看見圖形監控界面。如下:
檢視輪詢效果
在位址欄裡輸入http://192.168.253.132:8090就可以看到web界面了,它是将通路平均配置設定到每台主機的,預設每台主機輪詢一次過
安裝keepalived
擷取keepalived
官網:http://www.keepalived.org
cd /opt/soft
wget http://www.keepalived.org/software/keepalived-..tar.gz
編譯keepalived
tar –zxvf keepalived-..tar.gz
cd keepalived-.
./configure --prefix=/opt/keepalived
make && make install
添加進系統服務
cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /opt/keepalived/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
chmod +x /etc/init.d/keepalived
haproxy主從高可用
- 環境說明:
-
VIP: 192.168.253.222
master: 192.168.253.132 8090
backup: 192.168.253.131 8090
web server1:192.168.253.129 80
web server2:192.168.253.130 80
web server3:192.168.253.131 80
參考連結:http://www.cnblogs.com/kgdxpr/p/3325788.html
配置master
修改keepalived.conf
vi /etc/keepalived/keepalived.conf
配置如下:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] #發生故障時,接受資訊的email位址
}
notification_email_from [email protected]
smtp_server ..
smtp_connect_timeout
router_id LVS_DEVEL
}
vrrp_script chk_haproxy { #自定義的監控腳本
script "/etc/keepalived/check_haproxy.sh"
interval
weight
}
vrrp_instance VI_1 {
state MASTER #從伺服器設為BACKUP
interface eth #監聽接口
virtual_router_id
priority #優先級,backup機器上的優先級要小與這個值
advert_int #檢查間隔
authentication {
auth_type PASS
auth_pass
}
track_script { #自定義的監控腳本
chk_haproxy
}
virtual_ipaddress { #VIP位址,可以設多個
.
}
}
編寫haproxy服務狀态檢測腳本
為了防止haproxy服務挂了,但是keepalived卻還正常,沒有切換到slave上,是以我們編寫這個腳本來檢測haproxy服務的狀态,haproxy服務挂掉後,該腳本會自動重新開機haproxy服務,不成功,則關閉keepalived服務,自動切換到backup。
vi /etc/keepalived/chk_haproxy.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq ];then
/etc/init.d/haproxy start
fi
sleep
if [ $(ps -C haproxy --no-header | wc -l) -eq ]; then
/etc/init.d/keepalived stop
fi
啟動服務
service haproxy start
service keepalived start
檢視VIP如下
配置backup
所有步驟都和master一樣,相當于master的一個鏡像。
修改keepalived.conf
vi /etc/keepalived/keepalived.conf
修改如下兩行
State MASTER 改為 state BACKUP
Priority 100 改為 priority 90
修改後如下圖
啟動服務
service haproxy start
service keepalived start
高可用測試
停止master的keepalived服務
# service keepalived stop
# tail –f /var/log/messages
Dec :: localhost Keepalived[]: Stopping Keepalived v1. (/,)
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) sending priority
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) removing protocol VIPs.
Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP . removed
發現master的vip已經釋放了。
再檢視backup上的日志
# tail –f /var/log/messages
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP . added
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for .
Dec :: localhost Keepalived_vrrp[]: Process [] didn't respond to SIGTERM
發現backup已經綁定了VIP
再将master上的keepalived服務啟動。
# tail –f /var/log/messages
Dec :: localhost Keepalived_healthcheckers[]: Using LinkWatch kerne
l netlink reflector...
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Transition
to MASTER STATE
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Received lo
wer prio advert, forcing new election
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering MA
STER STATE
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) setting pro
tocol VIPs.
Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gra
tuitous ARPs on eth0 for .
Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector rep
orts IP . added
發現master又重新綁定了vip
檢視backup日志:
Dec 6 11:23:38 localhost Keepalived_vrrp[40281]: VRRP_Instance(VI_1) Received h
igher prio advert
Dec 6 11:23:38 localhost Keepalived_vrrp[40281]: VRRP_Instance(VI_1) Entering B
ACKUP STATE
Dec 6 11:23:38 localhost Keepalived_vrrp[40281]: VRRP_Instance(VI_1) removing p
rotocol VIPs.
Dec 6 11:23:38 localhost Keepalived_healthcheckers[40280]: Netlink reflector re
ports IP 192.168.253.222 removed
發現backup又重新釋放了vip
haproxy配置檔案詳解
參考連結:http://www.linuxidc.com/Linux/2012-07/65350.htm
http://www.iyunv.com/thread-18514-1-1.html
http://www.douban.com/note/275230796/
####################全局配置資訊########################
#######參數是程序級的,通常和作業系統(OS)相關#########
global
log 127.0.0.1 local0 #日志輸出配置,所有日志都記錄在本機,通過local0輸出
#log 127.0.0.1 local1 notice
#log loghost local0 info
ulimit-n 82000 #設定每個程序的可用的最大檔案描述符
maxconn 4096 #最大連接配接數
chroot /var/haproxy #chroot運作的路徑
uid 99 #所屬運作的使用者uid
gid 99 #所屬運作的使用者組
daemon #以背景形式運作haproxy
nbproc 1 #程序數量(可以設定多個程序提高性能)
pidfile /var/run/haproxy.pid #haproxy的pid存放路徑,啟動程序的使用者必須有權限通路此檔案
debug #調試模式,輸出啟動資訊到标準輸出
#quiet #安靜模式,啟動時無輸出
#####################預設的全局設定######################
##這些參數可以被利用配置到frontend,backend,listen元件##
defaults
log global
mode http #所處理的類别 (#7層 http;4層tcp )
option httplog #日志類别,采用httplog
maxconn 20480 #最大連接配接數
option httplog #日志類别http日志格式
option httpclose #每次請求完畢後主動關閉http通道
option dontlognull #不記錄健康檢查的日志資訊
option forwardfor #如果後端伺服器需要獲得用戶端真實ip需要配置的參數,可以從Http Header中獲得用戶端ip
option redispatch #serverId對應的伺服器挂掉後,強制定向到其他健康的伺服器
option abortonclose #當伺服器負載很高的時候,自動結束掉目前隊列處理比較久的連接配接
stats refresh 30 #統計頁面重新整理間隔
retries 3 #3次連接配接失敗就認為服務不可用,也可以通過後面設定
balance roundrobin #預設的負載均衡的方式,輪詢方式
#balance source #預設的負載均衡的方式,類似nginx的ip_hash
#balance leastconn #預設的負載均衡的方式,最小連接配接
contimeout 5000 #連接配接逾時
clitimeout 50000 #用戶端逾時
srvtimeout 50000 #伺服器逾時
timeout check 2000 #心跳檢測逾時
####################監控頁面的設定#######################
listen admin_status #Frontend和Backend的組合體,監控組的名稱,按需自定義名稱
bind 0.0.0.0:65532 #監聽端口
mode http #http的7層模式
log 127.0.0.1 local3 err #錯誤日志記錄
stats refresh 5s #每隔5秒自動重新整理監控頁面
stats uri /admin?stats #監控頁面的url
stats realm itnihao\ itnihao #監控頁面的提示資訊
stats auth admin:admin #監控頁面的使用者和密碼admin,可以設定多個使用者名
stats auth admin1:admin1 #監控頁面的使用者和密碼admin1
stats hide-version #隐藏統計頁面上的HAproxy版本資訊
stats admin if TRUE #手工啟用/禁用,後端伺服器(haproxy-1.4.9以後版本)
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
errorfile 504 /etc/haproxy/errorfiles/504.http
#################HAProxy的日志記錄内容設定###################
capture request header Host len 40
capture request header Content-Length len 10
capture request header Referer len 200
capture response header Server len 40
capture response header Content-Length len 10
capture response header Cache-Control len 8
#######################網站監測listen配置#####################
###########此用法主要是監控haproxy後端伺服器的監控狀态############
listen site_status
bind 0.0.0.0:1081 #監聽端口
mode http #http的7層模式
log 127.0.0.1 local3 err #[err warning info debug]
monitor-uri /site_status #網站健康檢測URL,用來檢測HAProxy管理的網站是否可以用,正常傳回200,不正常傳回503
acl site_dead nbsrv(server_web) lt 2 #定義網站down時的政策當挂在負載均衡上的指定backend的中有效機器數小于1台時傳回true
acl site_dead nbsrv(server_blog) lt 2
acl site_dead nbsrv(server_bbs) lt 2
monitor fail if site_dead #當滿足政策的時候傳回503,網上文檔說的是500,實際測試為503
monitor-net 192.168.16.2/32 #來自192.168.16.2的日志資訊不會被記錄和轉發
monitor-net 192.168.16.3/32
########frontend配置############
#####注意,frontend配置裡面可以定義多個acl進行比對操作########
frontend http_80_in
bind 0.0.0.0:80 #監聽端口,即haproxy提供web服務的端口,和lvs的vip端口類似
mode http #http的7層模式
log global #應用全局的日志配置
option httplog #啟用http的log
option httpclose #每次請求完畢後主動關閉http通道,HA-Proxy不支援keep-alive模式
option forwardfor #如果後端伺服器需要獲得用戶端的真實IP需要配置次參數,将可以從Http Header中獲得用戶端IP
########acl政策配置#############
acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$
#如果請求的域名滿足正規表達式中的2個域名傳回true -i是忽略大小寫
acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn
#如果請求的域名滿足www.itnihao.cn傳回true -i是忽略大小寫
#acl itnihao hdr(host) -i itnihao.cn
#如果請求的域名滿足itnihao.cn傳回true -i是忽略大小寫
#acl file_req url_sub -i killall=
#在請求url中包含killall=,則此控制政策傳回true,否則為false
#acl dir_req url_dir -i allow
#在請求url中存在allow作為部分位址路徑,則此控制政策傳回true,否則傳回false
#acl missing_cl hdr_cnt(Content-length) eq 0
#當請求的header中Content-length等于0時傳回true
########acl政策比對相應#############
#block if missing_cl
#當請求中header中Content-length等于0阻止請求傳回403
#block if !file_req || dir_req
#block表示阻止請求,傳回403錯誤,目前表示如果不滿足政策file_req,或者滿足政策dir_req,則阻止請求
use_backend server_web if itnihao_web
#當滿足itnihao_web的政策時使用server_web的backend
use_backend server_blog if itnihao_blog
#當滿足itnihao_blog的政策時使用server_blog的backend
#redirect prefix http://blog.itniaho.cn code 301 if itnihao
#當通路itnihao.cn的時候,用http的301挑轉到http://192.168.16.3
default_backend server_bbs
#以上都不滿足的時候使用預設server_bbs的backend
##########backend的設定##############
#下面我将設定三組伺服器 server_web,server_blog,server_bbs
###########################backend server_web#############################
backend server_web
mode http #http的7層模式
balance roundrobin #負載均衡的方式,roundrobin平均方式
cookie SERVERID #允許插入serverid到cookie中,serverid後面可以定義
option httpchk GET /index.html #心跳檢測的檔案
server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
#伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,
#fall 3是3次失敗認為伺服器不可用,weight代表權重
server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2
#伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,
#fall 3是3次失敗認為伺服器不可用,weight代表權重
###################################backend server_blog###############################################
backend server_blog
mode http #http的7層模式
balance roundrobin #負載均衡的方式,roundrobin平均方式
cookie SERVERID #允許插入serverid到cookie中,serverid後面可以定義
option httpchk GET /index.html #心跳檢測的檔案
server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1
#伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2
#伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
###################################backend server_bbs###############################################
backend server_bbs
mode http #http的7層模式
balance roundrobin #負載均衡的方式,roundrobin平均方式
cookie SERVERID #允許插入serverid到cookie中,serverid後面可以定義
option httpchk GET /index.html #心跳檢測的檔案
server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1
#伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重
server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2
#伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重