网站做的不好使,自己建立公司网站的步骤,佛山网页设计多少钱,网站建设全网营销客户资源一、HAProxy概述
HAProxy是一款免费、快速且可靠的代理软件#xff0c;提供高可用性、负载均衡#xff0c;支持TCP和HTTP应用代理#xff0c;HAProxy凭借其卓越的性能和灵活性#xff0c;成为众多知名网站和系统的首选代理软件。 核心特点#xff1a;
高性能…一、HAProxy概述
HAProxy是一款免费、快速且可靠的代理软件提供高可用性、负载均衡支持TCP和HTTP应用代理HAProxy凭借其卓越的性能和灵活性成为众多知名网站和系统的首选代理软件。 核心特点
高性能采用事件驱动模型支持大量并发连接具有低延迟和高吞吐量。高可用性能够自动检测后端服务器健康状态实现故障切换确保服务连续性。灵活性支持多种负载均衡算法可根据业务需求灵活配置。安全性可作为反向代理隐藏后端服务器IP支持SSL/TLS加密保护数据传输安全。适用场景广泛应用于大型网站、Web应用集群、微服务架构等为各种业务场景提供负载均衡和高可用性解决方案。配置与部署支持分多个文件配置通过灵活配置可实现定制化需求。可视化监测支持通过页面方式可视化监测后端服务运行状态。
二、安装Haproxy
安装文件可点击进入官网下载也可以在文章关联的资源中进行下载。
# 安装基础软件
yum install -y gcc gcc-c glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget# 下载安装包
wget https://www.haproxy.org/download/2.0/src/haproxy-2.0.31.tar.gz# 解压依赖
tar xvf haproxy-2.0.31.tar.gz# 查看系统内核版本根据内核心指定编译参数TARGET的值如linux3100、linux5108等
uname -r# 编译版本
make ARCHx86_64 TARGETlinux3100 USE_PCRE1 USE_OPENSSL1 USE_ZLIB1 USE_SYSTEMD1 USE_CPU_AFFINITY1 PREFIX/usr/local/haproxy# 进行安装
make install PREFIX/usr/local/haproxy# 复制应用至path目录
cp haproxy /usr/sbin/# 添加用户
groupadd haproxy
useradd -g haproxy haproxy
id haproxy# 创建配置文件存放目录
mkdir /etc/haproxy mkdir /etc/haproxy/conf三、配置文件
以下提供的是一下基础的Http服务配置保存到主配置文件/etc/haproxy/haproxy.cfg中,配置多配置文件配置在haproxy.cfg也可配置到/etc/haproxy/conf目录下通过启动参数 -f 指定。
# 编译haproxy.cfg
vi /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
globallog 127.0.0.1 local0 infochroot /usr/local/haproxymaxconn 20000daemon#-----------
# admin settings
#-----------
defaults adminmode httpstats enablestats refresh 30stimeout connect 10stimeout client 1mtimeout server 1m
listen http_statsbind *:8888 #监控访问端口stats uri /haproxy #监控访问路径stats hide-version #隐藏版本号stats auth admin:123456 #设置管理员帐号和密码log 127.0.0.1 local0 err #[err warning info debug]
##################################################################
#Default mode http
##################################################################
defaults HTTPmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 5000timeout client 50000timeout server 50000maxconn 65535stats uri /status
#---------------------------------------------------------------------
# HTTP 外网
#---------------------------------------------------------------------
frontend http-frontbind :80#bind :443 ssl crt /etc/haproxy/ssl/xxxxx.pemmode httpoption forwardforhttp-request set-header X-Client-IP %[src]http-request set-header X-forwarded-Port %[dst_port]http-request add-header X-forwarded-Proto https if { ssl_fc }#强制https#redirect scheme https if !{ ssl_fc }#HAuse_backend haproxy if { path_beg -i /haproxy } #---------------------------------------------------------------------
# HTTP 内网
#---------------------------------------------------------------------
frontend http-prodbind :8085mode httpoption forwardforhttp-request set-header X-Client-IP %[src]#HAuse_backend haproxy if { path_beg -i /haproxy } #票务内部接口
backend haproxy mode httpbalance sourceserver haproxy-1 127.0.0.1:8888 check inter 2000 rise 2 fall 3 weight 1四、配置服务
在/usr/lib/systemd/system目录下添加haproxy.service文件将haproxy添加为系统服务实现开机启动-f可以指向文件也可以指向文件夹当指向文件夹时文件夹的所有文件将作为配置文件合并到haproxy.cfg中。
# 编写haproxy服务
vi /usr/lib/systemd/system/haproxy.service
[Unit]
DescriptionHAProxy Load Balancer
Aftersyslog.target network.target[Service]
#支持多配置文件读取类似于从侧面是实现配置文件的include功能。
ExecStartPre/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -c -q
ExecStart/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
ExecReload/bin/kill -USR2 $MAINPID[Install]
WantedBymulti-user.target
设置开始启动
# 启用服务实现开机启动
systemctl enable haproxy.service # 加载systemd管理配置
systemctl daemon-reload# 启动服务
systemctl start haproxy# 停止服务
systemctl stop haproxy# 查看状态
systemctl status haproxy
访问测试
启动haproxy服务后通过http://Ip:8888/haproxy可打开监控页面通过配置文件中的帐号admin密码123456进行登录访问。