global
log 127.0.0.1 local2
chroot /var/haproxy #chroot运行的路径,增加安全性
uid 99 #程序运行的用户id
gid 99 #程序运行的用户组id
daemon #以后台形式运行haproxy
nbproc 1 #number of process进程数量,不建议修改
pidfile /var/run/haproxy.pid #haproxy的pid存放路径
maxconn 4000 #默认最大连接数
defaults
mode http #;工作模式
option dontlognull
log global #;记录日志
option http-server-close #;启用服务器端关闭
option forwardfor except 127.0.0.0/8 #;传递客户端ip
option redispatch #;当服务器组中的某台设备故障后,自动将请求重定向到组内其他主机。
retries 3 #;请求重试的次数
timeout http-request 10s #;http请求超时时间
timeout queue 1m #;一个请求在队列里的超时时间·
timeout connect 10s #;连接服务器超时时间
timeout client 1m #;设置客户端侧最大非活动时间
timeout server 1m #;设置服务器侧最大非活动时间
timeout http-keep-alive 10s #;设置http-keep-alive的超时时间
timeout check 10s #;当一个连接建立之后,
maxconn 3000 #;同时处理的最大连接数
#errorfile 403 /etc/haproxy/errorfiles/403.http
#errorfile 500 /etc/haproxy/errorfiles/500.http
#errorfile 502 /etc/haproxy/errorfiles/502.http
#errorfile 503 /etc/haproxy/errorfiles/503.http
#errorfile 504 /etc/haproxy/errorfiles/504.http
#启用stats查看,认证等功能:
#默认在/haproxy?stats
listen stas #自定义监听名,任意取
bind 192.168.1.97:1099 #监听的地址和端口
mode http #模式
stats enable #启用状态监控
stats hide-version #隐藏软件版本号
stats auth admin:admin #登陆用户名和密码
stats realm HAproxy\ stats #提示信息,空格之前加\
stats admin if TRUE #当通过认证才可管理
stats uri /stats #访问路径
stats refresh 5 #页面自动刷新间隔
#Haproxy 负载均衡实例:
frontend webserver
bind 192.168.1.97:80
default_backend webservers
backend webservers
balance roundrobin
server web1 192.168.1.100:80 check
server web2 192.168.1.101:80 check
#或者
#listen webservers
# bind 192.168.1.97:80
# server web1 192.168.1.100:80 check
# server web2 192.168.1.101:80 check
加上日志支持
查看 /var/log/haproxy/haproxy.log 是否存在
不存在创建
<pre name="code" class="html" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 28px;"><span style="font-family: 宋体;">cd /var/log</span>
sudo mkdir haproxy
cd haproxy
sudo touch haproxy.log
sudo chmod a+w haproxy.log
编辑 vim /etc/rsyslog.conf
将
$ModLoad imudp
$UDPServerRun
514
两行前# 去掉
在最下边增加
local2.* /var/log/haproxy.log
vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=”-c 2 -r -m 0″
#-c 2 使用兼容模式,默认是 -c 5
#-r 开启远程日志
#-m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
service rsyslog restart
service haproxy start
访问
http://192.168.1.97/
监控
http://192.168.1.97:1099/stats