[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宕机时,能够将其会话重新派发到其它的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
后端的静态请求响应
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
后端的动态请求响应
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
联系客服