一、系统环境:Haproxy172.16.19.103Web1:172.16.19.104Web2:172.16.19.106二、Haproxy安装[root@haproxy soft]# tar zxvf haproxy-1.4.22.tar.gz[root@haproxy soft]# cd haproxy-1.4.22[root@haproxy haproxy-1.4.22]# uname –aspacer.gif[root@haproxy haproxy-1.4.22]#make TARGET=linux26 PREFIX=/usr/local/haproxy[root@haproxy haproxy-1.4.22]# make install PREFIX=/usr/local/haproxy三、修改haproxy配置[root@haproxy~]#cp /soft/haproxy-1.4.22/examples/haproxy.cfg /usr/local/haproxy/[root@haproxy ~]# vi /usr/local/haproxy/haproxy.cfgglobal        log 127.0.0.1   local0                                      #全局日志        maxconn 4096                                                   #最大连接数        chroot /usr/local/haproxy        uid 99                                                                 #用户ID        gid 99                                                                 #ID        daemon                                                              #后台运行        nbproc 1                                                             #创建进程数        pidfile /usr/local/haproxy/haproxy.pid             #pid文件defaults        log     127.0.0.1 local3        mode    http                                                   #支持的模式        option  httplog                                                 #日志格式        option  httpclose                                              #请求完成后关闭http通道        option  dontlognull        option  forwardfor                                           #apache日志转发        option redispatch        retries 3                                                             #重连次数        maxconn 2000        balance roundrobin                                           #算法类型

       stats uri /haproxy-stats                                      #状态统计页面

#http:// ip/haproxy-stats

       #stats auth admin:admin                                   # 状态页面用户密码,可选        contimeout      5000                                     # 连接超时        clitimeout      50000                                     # 客户端超时        srvtimeout      50000                                    # 服务器超时 listen  proxy 172.16.19.103:8080                                   # 访问地址及端口  option httpchk HEAD /index.html HTTP/1.0                  # 健康检查页面  server web1 172.16.19.104:80 cookie app1inst1 check inter 2000 rise 2 fall 5  server web2 172.16.19.106:80 cookie app1inst2 check inter 2000 rise 2 fall 5 四、启动 haproxy [root@haproxy ~]#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg 报错: [ALERT] 311/000401 (1888) : parsing [/usr/local/haproxy/haproxy.cfg:83] : error opening file </etc/haproxy/errors/503.http> for custom error message <503>. [ALERT] 311/000401 (1888) : Error(s) found in configuration file : /usr/local/haproxy/haproxy.cfg [WARNING] 311/000401 (1888) : config : dispatch address of proxy 'appli3-relais' will be ignored in balance mode. [ALERT] 311/000401 (1888) : Fatal errors found in configuration. 解决: errorfile       503     /usr/local/haproxy/errors/503.http [root@haproxy ~]# mkdir /usr/local/haproxy/errors [root@haproxy ~]# touch /usr/local/haproxy/errors/503.http 删除,除了第三步的其余内容 五、修改 web1 web2 主页,并启动 apache 主页文件目录: /var/www/html/index.html Web1     172.16.19.104 Web2     172.16.19.106 六、查看状态页面 http://172.16.19.103:8080/haproxy-stats spacer.gif 七、 haproxy 测试 spacer.gif [root@haproxy ~]# for i in $(seq 10); do curl http://172.16.19.103:8080;done

本文出自 “seven” 博客,请务必保留此出处http://addict.blog.51cto.com/1183172/1179270