Linux运维:Haproxy搭建

本文介绍了在Linux CentOS系统上安装和配置Haproxy的步骤,包括使用yum安装haproxy,编辑配置文件举例监听设置,启动并设置开机自启haproxy服务,以及检查haproxy运行状态的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、安装haproxy

[root@company-dev-lb ~]# yum install -y haproxy

二、配置haproxy

[root@company-dev-lb ~]# vim /etc/haproxy/haproxy.cfg

>#---------------------------------------------------------------------
#Example configuration for a possible web application.  See the
#full configuration options online
#
#http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
#Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     20000    #定义每个haproxy进程的最大连接数
    user        haproxy
    group       haproxy
    daemon

    tune.ssl.default-dh-param 2048   #haproxy加密长度
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
 common defaults that all the 'listen' and 'backend' sections will
 use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog   #采取httplog格式
    option                  dontlognull
    option http-server-close   #http请求后关闭通道
    option forwardfor       except 127.0.0.0/8
    option                  redispatch   #确定服务器不可用后重定向
    retries                 3   #健康检查次数3次
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 20000   #最大连接数
    stats uri /ceeety/haproxy?stats   #web url
    ststs realm haproxy
    stats auth haproxy:haproxy@ceeety   #web认证信息

#---------------------------------------------------------------------
 main frontend which proxys to the backends
#---------------------------------------------------------------------
listen https-443
    bind 0.0.0.0:443   #绑定本机ip443端口
    maxconn 20000   #最大连接数
    mode http
    option httplog   #采取httplog格式
    option httpclose   #http请求后关闭通道
    option forwardfor header client-ip
    option abortonclose
    option accept-invalid-http-request
    capture request header Host len 255
    capture request header User-Agent len 255
    capture request header Referer len 255
    capture request header Cookie len 255
    balance roundrobin   #轮询


    acl company-test-www hdr_beg(host) -i company-test-www  #acl规则,这个是命中头部
   acl in_xxx url_reg -i \.com$    acl规则,以.com结尾的


    use_backend company-test-www if company-test-www  #匹配acl规则后使用rabbitmq中的服务器
   use_backend back_xxx if in_xxx


    backend company-test-www
       balance     roundrobin   #轮询
       cookie SERVERID insert indirect   #插入服务器ID,有助于保持会话
       server      mq01:443 weight 8 check inter 1000 rise 3 fall 3   #健康检查间隔1000ms,连续3次成功,连续3次失败
       server      mq02:443 weight 8 check inter 1000 rise 3 fall 3

   backend back_xxx
      balance     roundrobin
      cookie SERVERID insert indirect
     server      xxx:port weight x check inter 1000 rise 3 fall 3
#


    default_backend company-test-www
 
#--------------------------------------------------------------------------------
#listen http-80
    bind 0.0.0.0:80
    maxconn 20000
    mode http
    option httplog
    option httpclose
    balance roundrobin
    acl 80 url_reg -i \.html$
    use_backend 80
#
#
#backend 80
    balance     roundrobin
    server      mq01:80 weight 8 check inter 1000 rise 3 fall 3
    server      mq02:80 weight 8 check inter 1000 rise 3 fall 3
#

default_backend          default_rabbitmq    #未命中的请求使用deafult_rabbitmq中的服务器

backend default_rabbitmq
    balance     roundrobin   #轮询
    server  mq01 192.168.20.116 weight 8 check inter 1000 rise 3 fall 3
    server  mq02 192.168.20.117 weight 8 check inter 1000 rise 3 fall 3

由于是测试环境,还没有明确需求,所以监听配置只是举例

三、打开haproxy,开机自启

[root@company-dev-lb ~]# systemctl start haproxy.service
[root@company-dev-lb ~]# systemctl enable haproxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.

四、查看haproxy状态

[root@company-dev-lb ~]# systemctl status haproxy.service

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值