在做反向代理服务器的负载均衡时,我们通常会使用nginx的均衡配置。其实,haproxy的负载均衡也是属于这一类的。那么关于这方面的配置过程我们现在来进行一下讲解。首先,对haproxy进行一个简单的介绍,之后就是安装和配置环节了。
反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。新的1.3引入了frontend,backend,frontend根据任意 HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.
192.168.169.137 (haproxy)———负载均衡———-(192.168.169.117;192.168.169.118) 安装配置HAproxy [root@tiejiang ~]# cd /usr/local/src/ [root@tiejiang src]# wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.14.2.tar.gz [root@tiejiang src]# tar zxvf haproxy-1.3.14.2.tar.gz [root@tiejiang src]# mv haproxy-1.3.14.2 haproxy [root@tiejiang src]# cd haproxy [root@tiejiang haproxy]# make TARGETR=linux26
[root@tiejiang examples]# vim haproxy.cfg global maxconn 5120 chroot /usr/local/haproxy uid 99 gid 99 daemon quiet nbproc
#pidfile /var/run/haproxy-private.pid defaults log global mode http optionhttplog optiondontlognull log 127.0.0.1 local3 retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen webfarm 0.0.0.0:80 mode http stats uri /haproxy-stats #监控haproxy状态 stats realm Haproxy\ statistics stats auth netseek:52netseek#设置状态监控的用户名为netseek密码为52netseek balance roundrobin #负载均衡算法 cookie SERVERID insert indirect option httpclose # option forwardfor#apache日志转发功能 option httpchk HEAD /check.txt HTTP/1.0#健康检测 server app_bbs1 192.168.169.117:80 cookie app1inst1 check inter 2000 rise 2 fall 5 server app_bbs2 192.168.169.118:80 cookie app1inst2 check inter 2000 rise 2 fall 5 syslog.conf里加一行 local3.*/var/log/haproxy.log [root@tiejiang examples]# touch /var/log/haproxy.log [root@tiejiang examples]# chown haproxy:haproxy /var/log/haproxy.log [root@tiejiang examples]# chmod u+x /var/log/haproxy.log [root@tiejiang examples]# tail –f /var/log/harpoxy.log 监控日志 [root@tiejiang examples]# ./haproxy -f haproxy.cfg 启动服务. 监控状态图示http://192.168.169.137/haproxy-stats ,输入用户名密码查看状态。
配置httpd.conf LogFormat “%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b " combined CustomLog /var/log/httpd/access_log combined
SetEnvIf Request_URI “^/check\.txt$" dontlog LogLevel warn ErrorLog /var/log/httpd/vhost_error.log CustomLog /var/log/httpd/vhost_access.log combined env=!dontlog