[root@node2 ~]# yum -y install haproxy 安裝haproxy
[root@node2 ~]# vim /etc/haproxy/haproxy.cfg
#
log
127.0
.
0.1
local2
chroot /
var
/lib/haproxy
pidfile /
var
/run/haproxy.pid
maxconn
4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /
var
/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the
'listen'
and
'backend'
sections will
#
use
if
not designated
in
their block
#---------------------------------------------------------------------
defaults
mode http 指定haproxy工作模式為http
log global
option httplog
option dontlognull
option http-server-close 當客戶端超時時,允許服務端斷開連接
option forwardfor except
127.0
.
0.0
/
8
在http的響應頭部加入forwardfor
option redispatch #在使用了基于cookie的會話保持的時候,通常加這么一項,一旦后端某一server宕機時,能夠將其會話重新派發(fā)到其它的upstream servers
retries
3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn
3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:
80
前端代理
acl url_static path_beg -i /
static
/images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
acl url_dynamic path_end -i .php
use_backend
static
if
url_static
default_backend
dynamic
#---------------------------------------------------------------------
#
static
backend
for
serving up images, stylesheets and such
#---------------------------------------------------------------------
backend
static
后端的靜態(tài)請求響應
balance roundrobin
server
static
192.168
.
1.100
:
80
inter
3000
rise
2
fall
3
check maxconn
5000
#---------------------------------------------------------------------
# round robin balancing between the
var
ious backends
#---------------------------------------------------------------------
backend
dynamic
后端的動態(tài)請求響應
balance roundrobin
server dynamic1
192.168
.
1.101
:
80
inter
3000
rise
2
fall
3
check maxconn
5000
server dynamic2
192.168
.
1.102
:
80
inter
3000
rise
2
fall
3
check maxconn
5000
listen statistics
mode http
bind *:
8080
~ stats enable
stats auth admin:admin
stats uri /admin?stats 指定URI的訪問路徑
stats admin
if
TRUE
stats hide-version
stats refresh 5s
acl allow src
192.168
.
0.0
/
24
定義訪問控制列表
tcp-request content accept
if
allow
tcp-request content reject