一下是我认为很赞的haproxy的学习过程
片段来自网上和我自己的一些实践,可耻的原创了一下。
1.haproxy 介绍
http://www.oschina.net/question/17_8785
2.haproxy配置实例
# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#日志输出配置,所有日志都记录在本机,通过local0输出
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
#最大连接数
maxconn 65535
#改变当前工作目录。
chroot /usr/local/
#所属用户的uid
uid 99
#所属运行的gid
gid 99
#以后台形式运行haproxy
daemon
defaults
log global
#默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
mode http
option dontlognull
option httplog
#两次连接失败就认为是服务器不可用
retries 3
#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option redispatch
#当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
option abortonclose
#默认的最大连接数
maxconn 65535
#timeout http-keep-alive 10s
# timeout queue 1m
contimeout 5000 #连接超时
clitimeout 50000 #客户端超时
srvtimeout 50000 #服务器超时
timeout check 5s #心跳检测超时
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm linuxidc-test-Haproxy #统计页面密码框上提示文本
stats auth admin:admin123 #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
frontend www
#这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。
bind :80
#acl后面是规则名称,-i是要访问的域名,如果访问www.linuxidc.com这个域名就分发到下面的webserver 的作用域。
acl web hdr(host) -i www.linuxidc.com
#如果访问img.linuxidc.com就分发到imgserver这个作用域。
acl img hdr(host) -i img.linuxidc.com
use_backend webserver if web
use_backend imgserver if img
backend webserver #webserver作用域
mode http
#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数
balance roundrobin
#检测文件,如果分发到后台index.html访问不到就不再分发给它。
option httpchk /index.html
server web01 192.168.158.116:80 check inter 2000 fall 3 weight 20
server web02 192.168.158.117:80 check inter 2000 fall 3 weight 10
server web03 192.168.158.118:80 check inter 2000 fall 3 weight 10
backend imgserver
mode http
option httpchk /index.php
balance roundrobin
server img01 192.1