案例環境:html
主機 操做系統 IP地址 主要的軟件
----------------------------------------------------------------------------
Haproxy CentOS6.6 x86_64 192.168.200.101 haproxy-1.4.24.tar.gz
Nginx1 CentOS6.6 x86_64 192.168.200.103 nginx-1.6.2.tar.gz
Nginx2 CentOS6.6 x86_64 192.168.200.104 nginx-1.6.2.tar.gzlinux
配置負載均衡服務器nginx
1.安裝Haproxy依賴包及源碼包編譯安裝c++
yum -y install gcc gcc-c++ make pcre-devel bzip2-develweb
2.將準備好的軟件包解壓vim
tar xf haproxy-1.4.24.tar.gz -C /usr/src/瀏覽器
3.編譯安裝bash
cd /usr/src/haproxy-1.4.24/服務器
make TARGET=linux26 && make install負載均衡
4.創建haproxy的配置目錄及文件
mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/
5.haproxy 配置文件修改
vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
# log 127.0.0.1 local0
# log 127.0.0.1 local1 notice
log
/dev/log
local0 info
log
/dev/log
local0 notice
maxconn 4096
uid 99
gid 99
daemon
defaults
log global
mode http
option httplog
retries 3
maxconn 4096
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webcluster 0.0.0.0:80
option httpchk GET
/index
.html
balance roundrobin
server inst1 192.168.200.113:80 check inter 2000 fall 3
server inst1 192.168.200.114:80 check inter 2000 fall 3
listen admin_stats
bind 0.0.0.0:8000
mode http
option httplog
maxconn 100
stats refresh 30s
stats uri
/stats
stats realm Crushlinux\ Haproxy
stats auth admin:admin
stats hide-version
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
chmod +x /etc/init.d/haproxy
/etc/init.d/haproxy start
netstat -anpt |grep :80
killall -s HUP nginx 從新加載服務
killall -s QUIT nginx 退出服務
測試環境:
用瀏覽器打開 http://192.168.200.111
打開一個新的瀏覽器再次訪問 http://192.168.200.111
能夠驗證兩次訪問到的結果分別爲:
server 192.168.200.113
server 192.168.200.114
Haproxy 日誌文件:
1.添加日誌文件
vim /etc/haproxy/haproxy
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
log
/dev/log
local0 info
log
/dev/log
local0 notice
if
($programname ==
'haproxy'
and $syslogserverity-text ==
'info'
)
then
-
/var/log/haproxy/haproxy-info
.log
&~
if
($programname ==
'haproxy'
and $syslogserverity-text ==
'notice'
)
then
-
/var/log/haproxy/haproxy-notice
.log
&~