导航

不许杀人,也不许放火

« 千龙军事网的HYThaproxy+varnish+backendweb »

haproxy

今天有空尝试了一下,haproxy反向代理,根据HTTP请求的主机头及请求文件类型进行后端服务器的选择,实现类似7层交换的功能。

定义好frontend和backend,用ACL+use_backend进行匹配,配置文件的条理还是很清晰的。负载均衡可以有多种方式:
1、简单的轮询,balance roundrobin;
2、根据请求的源IP,balance source;
3、根据请求的uri,balance uri;
4、根据请求RUL中的参数,balance url_param。

在上述方式的同时,还可以根据cookie来保持同一会话落在特定的后端服务器上。

配置例子如下:

global
  maxconn 5120
  chroot /usr/local/haproxy
  uid 502
  gid 502
  daemon

frontend webserver
  bind :80
  mode http
  log global
  option httplog
  option dontlognull
  option allbackups
  monitor-uri /monitoruri
  maxconn 8000
  clitimeout 30000
  acl mysite hdr_dom(host) -i
www.2tutu.com
  acl status hdr_dom(host) -i stat.2tutu.com
  acl statcs url_reg \.(jpg|html|htm|png|gif|css|shtml)$
  use_backend caches if mysite statcs
#以.jpg等结尾的URL为静态内容,将请求落到运行在8081端口上的varnish
  use_backend servers1 if mysite !statcs
#其他相同主机头的非静态内容,将请求落到运行在8080端口上的web服务上
  use_backend stats if status
#状态查看页面的后端
  default_backend servers0

backend servers0
  mode http
  balance roundrobin
  contimeout 30000
  srvtimeout 30000
  redispatch
  retries 2
  option redispatch
  cookie DYNSRV insert indirect nocache
  fullconn 4000
  server wwwserver1 192.168.1.10:80 minconn 50 maxconn 500 cookie s1 check inter 1000
#可以继续增加其他服务器作为负载均衡池里的节点

backend servers1
  mode http
  balance roundrobin
  contimeout 5000
  srvtimeout 5000
  redispatch
  cookie DYNSRV insert indirect nocache
  server server1 127.0.0.1:8080 check cookie first inter 1000
#可以继续增加其他服务器作为负载均衡池里的节点
#server server1 192.168.1.11:8080 check cookie second inter 1000
#轮询的同时,根据插入的cookie DYNSRV 的值来做会话保持。

backend caches
  mode http
  balance roundrobin
  contimeout 5000
  srvtimeout 5000
  redispatch
  server server1 127.0.0.1:8081 check inter 1000
#可以继续增加其他服务器作为负载均衡池里的节点
#server server1 127.0.0.1:8082 check inter 1000

backend stats
  log global
  mode http
  stats uri /
  balance roundrobin

可以通过stat.2tutu.com访问到状态页面
 

  • quote 2.xxdtpyy
  • 楼主,您好:
    如果有时间的话,能不能请教个问题?
    您在做上面的测试的时候,能记录haproxy的日志吗?我的配置文件如下:
    global
    maxconn 5120
    # log 192.168.10.180 local3 info
    log 127.0.0.1 local1 notice
    uid 501
    gid 502
    daemon
    quiet
    nbproc 2
    pidfile /var/run/haproxy-private.pid
    defaults
    mode http
    option httplog
    option dontlognull
    # log 127.0.0.1 local3
    retries 3
    redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

  • 2009-2-17 19:40:31 回复该留言
  • quote 3.xxdtpyy
  • listen webfarm 192.168.10.180:80
    mode http
    stats enable
    balance uri
    # dispatch 192.168.10.180:80
    # cookie SERVERID insert indirect
    log global
    option httplog
    option logasap
    option forwardfor
    server web181 192.168.10.181:80 cookie app1inst1 check inter 2000 rise 2 fall 5
    server web182 192.168.10.182:80 cookie app1inst2 check inter 2000 rise 2 fall 5
    至于网上流传的:
    syslog.conf里加一行
    local3.* /var/log/haproxy.log
    # touch /var/log/haproxy.log
    # chown haproxy:haproxy /var/log/haproxy.log
    # chmod u+x /var/log/haproxy.log
    也都试过,可是/var/log/haproxy.log就是不记录日志,不知道为什么?
    楼主能否指点一下?非常感谢!
    minuteman 于 2009-2-18 8:25:35 回复
    是不是syslog需要-r启动参数?
  • 2009-2-17 19:40:49 回复该留言

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Search

控制面板

首页

最近发表

Powered By Z-Blog