https://pan.baidu.com/s/1uwQWVgJ6lfouUu-114G16w
haproxy安装包
安装haproxy环境以及依赖关系
yum install -y \
pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \
make
tar xzvf haproxy-1.5.15.tar.gz -C /opt 解压
cd /opt/haproxy-1.5.15
make TARGET=linux26 PREFIX=/usr/local/haproxy 编译安装haproxy版本
make install PREFIX=/usr/local/haproxy //标识64为系统
mkdir /etc/haproxy 创建目录
useradd -s /sbin/nologin -M haproxy /创建程序用户
id +用户名 /查看用户所属组
uname -r 查看版本信息
cp /opt/haproxy-1.5.15/examples/haproxy.cfg /etc/haproxy/
vi /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local3 //配置全局日志记录,local0为日志设备,notice为输出的日志级别,表示使用本地(127.0.0.1)机器上的rsyslog服务器中的local0设备记录日志等级为notice的日志
maxconn 204800 最大连接数
chroot /usr/local/haproxy-1.5.15 //工作目录
user haproxy /运行的程序用户
group haproxy /运行的程序用户组
daemon /创建1个进程进入deamon模式运行,以后台形式运行harpoxy
nbproc 1
pidfile /var/run/haproxy.pid //haproxy进程pid文件
stats socket /usr/local/haproxy/stats
description haproxy server
#---------------------------------------------------------------------
common defaults that all the ‘listen’ and ‘backend’ sections will
use if not designated in their block
#---------------------------------------------------------------------
defaults
log global //定义日志文件,采用全局定义
mode http //所处理的类别tcp是四层,http是七层,health指挥返回ok,若混合模式则mode不需要设置
maxconn 10000 最大连接数
option httplog //日志类别为http日志格式;如果是混合模式,此处还需要加上tcplog
option httpclose //每次请求完毕主动关闭http通道
option dontlognull //不记录健康检查的日志检查信息
option forwardfor except 127.0.0.0/8 //后端服务器可以从http header中获取客户端ip
retries 3 //三次连接失败就认为服务器不可用
option redispatch //serverid对应的服务器挂掉后强制定向到其他健康的服务器
option abortonclose
balance roundrobin //设置默认负载均衡方式,轮询方式
timeout http-request 10s
timeout queue 1m
timeout connect 10s //连接超时时间
timeout client 1m //客户端连接超时时间
timeout server 1m //服务器连接超时时间
timeout http-keep-alive 10s
timeout check 10s //健康监测的超时时间
#---------------------------------------------------------------------
use listen setting the haproxy status for site
#---------------------------------------------------------------------
统计页面配置
listen admin_status 为haproxy访问状态监控页面配置,名为admin_status
bind *:8089 监听端口
stats enble 启用监听端口
mode http http的7层模式
log 127.0.0.1 local3 err
stats refresh 5s 页面自动刷新的时间为30秒
log global 继承global中log的定义
stats uri /status //监控页面的url访问路径,即http://ip/stats访问监控页面
stats realm www.skeryp.com //监控页面的密码信息框提示信息
stats auth admin:admin //健康页面的用户和密码admin,可以设置多个用户名
stats hide-version //隐藏统计页面上haproxy的版本信息
stats admin if TRUE /当通过认证才可以管理1
#---------------------------------------------------------------------
main listen which proxys to the backends
#---------------------------------------------------------------------
listen www
bind *:80
maxconn 5000
mode http
log global
option httplog
option httpclose
option forwardfor
log global
default_backend default #设置默认访问页面
#定义当请求的内容是静态内容时,将请求转交给static server的acl规则
acl url_static path_beg -i /static /images /img /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .html
acl host_static hdr_beg(host) -i img. video. download. ftp. imags. videos.
#定义当请求的内容是php内容时,将请求转交给php server的acl规则
acl url_php path_end -i .php
#定义当请求的内容是.jsp或.do内容时,将请求转交给tomcat server的acl规则
acl url_jsp path_end -i .jsp .do
#引用acl匹配规则
use_backend static_pool if url_static or host_static
use_backend php_pool if url_php
use_backend tomcat_pool if url_jsp
#定义后端backend server
backend static_pool
option httpchk GET /index.html //检测心条
server static1 192.168.80.101:80 cookie id1 check inter 2000 rise 2 fall 3
backend php_pool
option httpchk GET /info.php
server php1 192.168.80.102:80 cookie id1 check inter 2000 rise 2 fall 3
backend tomcat_pool
option httpchk GET /index.jsp //检测心条
server tomcat1 192.168.80.103:8086 cookie id2 check inter 2000 rise 2 fall 3
#<----------------------default site for listen and frontend------------------------------------>
backend default
mode http
option httpchk GET /index.html
server default 192.168.80.104:80 cookie id1 check inter 2000 rise 2 fall 3 maxconn 5000
设置启动程序
cp haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy 添加执行权限
chkconfig --add haproxy
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
service haproxy start