上一篇文章介绍了haproxy的简单安装以及最小化启动.实际生产环境中haproxy是没有这么简单的.下面贴上我们公司根据request_url来实现动静分离.

  1. global 
  2.    maxconn 10240 
  3.    user haproxy 
  4.    group haproxy 
  5.    daemon 
  6.    nbproc 4 
  7.    pidfile /var/run/haproxy.pid 
  8. defaults 
  9.    log global 
  10.    option dontlognull   #不记录空连接 
  11.    option redispatch 
  12.    timeout connect 10000 
  13.    timeout client 300000 
  14.    timeout server 300000 
  15.    maxconn 60000 
  16.    retries 3 
  17. frontend main *:80 
  18.    mode http 
  19.    option httpclose 
  20.    option httplog 
  21.    option forwardfor 
  22.    acl host_dpool hdr_beg(host) -i cache static attach1 
  23.    acl host_dpool hdr_bet(host) -i yiqisoo.com 
  24.    use_backend dpool if host_dpool 
  25.    acl host_imgcdn hdr_end(host) -i  imgcdn.house.sina.com.cn 
  26.    use_backend imgcdn if host_imgcdn 
  27.    acl host_bbs hdr_end(host) -i bbs.house.sina.com.cn bbs0.house.sina.com.cn 
  28.    use_backend bbs if host_bbs 
  29.    acl host_jiajustatic hdr_end(host) -i static.jiaju.com 
  30.    use_backend jiajustatic if host_jiajustatic 
  31.    default_backend spool  
  32. frontend status 
  33.     bind *:81 
  34.     default_backend  status 
  35. backend status 
  36.     mode http 
  37.     stats enable 
  38.     stats hide-version 
  39.     stats uri /haproxy-stats 
  40.     stats realm Haproxy\ statistics 
  41.     stats auth evil:redhat 
  42. backend dpool 
  43.     mode http 
  44.     balance uri len 128 depth 8 根据uri 进行hash 因为后端的real_server为squid.
  45.     server p-01 1.1.1.1:80 maxconn 2000 check inter 2000 rise 3 fall 3 
  46.     server p-02 2.2.2.2:80 maxconn 2000 check inter 2000 rise 3 fall 3 
  47.     server p-03 3.3.3.3:80 maxconn 2000 check inter 2000 rise 3 
  48. fall 3 
  49. ##check 接受服务器健康检测 
  50. ##inter 两次健康检测所相隔的时间 默认为毫秒 
  51. ##rise  指多少次检测后认为服务为健康状态 
  52. ##fall  指多少次检测后认为服务为不可用 
  53. backend imgcdn 
  54.     mode http 
  55.     ba 
  56.     server imgcdn-01 11.12.13.14:80 weight 1 check inter 10000 
  57. backend bbs 
  58.     mode http 
  59.     balance roundrobin 
  60.     server b-01 172.16.1.1:80 weight 1 check inter 10000 
  61.     server b-02 172.16.1.2:80 weight 1 check inter 10000 
  62.     server b-03 172.16.1.3:80 weight 1 check inter 10000 
  63. backend jiajustatic 
  64.     mode http 
  65.     balance roundrobin 
  66.     server jiajustatic-01 192.168.1.1:80 weight 1 check inter 10000 
  67.  
  68.      

其实很简单,只要通过acl设置.然后再if判断是否为真,则使用哪个backend.

经常用到的acl类型有. hdr_beg(host) -i hdr_end(host) -i path_beg path_end hdr(Referer)

default_backend 参数意为:以上所有规则都不匹配,则是用默认backend