上一篇文章介绍了haproxy的简单安装以及最小化启动.实际生产环境中haproxy是没有这么简单的.下面贴上我们公司根据request_url来实现动静分离.
- global
- maxconn 10240
- user haproxy
- group haproxy
- daemon
- nbproc 4
- pidfile /var/run/haproxy.pid
- defaults
- log global
- option dontlognull #不记录空连接
- option redispatch
- timeout connect 10000
- timeout client 300000
- timeout server 300000
- maxconn 60000
- retries 3
- frontend main *:80
- mode http
- option httpclose
- option httplog
- option forwardfor
- acl host_dpool hdr_beg(host) -i cache static attach1
- acl host_dpool hdr_bet(host) -i yiqisoo.com
- use_backend dpool if host_dpool
- acl host_imgcdn hdr_end(host) -i imgcdn.house.sina.com.cn
- use_backend imgcdn if host_imgcdn
- acl host_bbs hdr_end(host) -i bbs.house.sina.com.cn bbs0.house.sina.com.cn
- use_backend bbs if host_bbs
- acl host_jiajustatic hdr_end(host) -i static.jiaju.com
- use_backend jiajustatic if host_jiajustatic
- default_backend spool
- frontend status
- bind *:81
- default_backend status
- backend status
- mode http
- stats enable
- stats hide-version
- stats uri /haproxy-stats
- stats realm Haproxy\ statistics
- stats auth evil:redhat
- backend dpool
- mode http
- balance uri len 128 depth 8 根据uri 进行hash 因为后端的real_server为squid.
- server p-01 1.1.1.1:80 maxconn 2000 check inter 2000 rise 3 fall 3
- server p-02 2.2.2.2:80 maxconn 2000 check inter 2000 rise 3 fall 3
- server p-03 3.3.3.3:80 maxconn 2000 check inter 2000 rise 3
- fall 3
- ##check 接受服务器健康检测
- ##inter 两次健康检测所相隔的时间 默认为毫秒
- ##rise 指多少次检测后认为服务为健康状态
- ##fall 指多少次检测后认为服务为不可用
- backend imgcdn
- mode http
- ba
- server imgcdn-01 11.12.13.14:80 weight 1 check inter 10000
- backend bbs
- mode http
- balance roundrobin
- server b-01 172.16.1.1:80 weight 1 check inter 10000
- server b-02 172.16.1.2:80 weight 1 check inter 10000
- server b-03 172.16.1.3:80 weight 1 check inter 10000
- backend jiajustatic
- mode http
- balance roundrobin
- server jiajustatic-01 192.168.1.1:80 weight 1 check inter 10000
其实很简单,只要通过acl设置.然后再if判断是否为真,则使用哪个backend.
经常用到的acl类型有. hdr_beg(host) -i hdr_end(host) -i path_beg path_end hdr(Referer)
default_backend 参数意为:以上所有规则都不匹配,则是用默认backend