haproxy实验-2

news2024/9/22 14:28:01

haproxy中的算法

静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度

等,且无法实时修改权重(只能为0和1,不支持其它值),只能靠重启HAProxy生效。

static-rr:基于权重的轮询调度

不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)

不支持端服务器慢启动

其后端主机数量没有限制,相当于LVS中的 wrr

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance static-rr
	server webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
[root@localhost ~]#systemctl restart haproxy.service 

first

其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务

不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效

会忽略服务器的权重设置

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance first
	server webserver1 192.168.0.101:80 maxconn 3 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

动态算法

基于后端服务器状态进行调度适当调整,

新请求将优先调度至当前负载较低的服务器,权重可以在haproxy运行时动态调整无需重启

roundrobin

  1. 基于权重的轮询动态调度算法,

  2. 支持权重的运行时调整,不同于lvs中的rr轮训模式,

  3. HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),

  4. 其每个后端backend中最多支持4095个real server,

  5. 支持对real server权重动态调整,

  6. roundrobin为默认调度算法,此算法使用广泛

    [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance roundrobin
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

动态调整权重:

[root@haproxy ~]# echo "set weight webserver_80/webserver1 2" | socat stdio
/var/lib/haproxy/haproxy.sock

leastconn

leastconn加权的最少连接的动态

支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接)

比较适合长连接的场景使用,比如:MySQL等场景。

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance leastconn
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

其他算法

source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一

个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服

务器,默认为静态方式;源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法

和一致性hash

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance source
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
	

[root@localhost ~]#systemctl restart haproxy.service

map-base 取模法

map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请

求转发至对应的后端服务器。

此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度

缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果

整体改变

取模运算,就是计算两个数相除之后的余数(源地址%总权重=余数)(余数不会超过hash环的数字2的32次方-1)

hash(source_ip)%所有后端服务器相加的总权重

eg:

当源hash值时1111,1112,1113,三台服务器a b c的权重均为1,总权重为3;

即abc的调度标签分别会被设定为 0 1 2(1111%3=1,1112%3=2,1113%3=0)

1111 ----- > node a

1112 ------> node b

1113 ------> node c

如果a下线/再上线一台服务器后,权重数量发生变化

1111%2=1,1112%2=0,1113%2=1

1112和1113被调度到的主机都发生变化,这样会导致会话丢失

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance source
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

一致性hash

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动

该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

算法:

1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)
2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0---4294967295]之间,
3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

hash环,假如a.jpg,b.jpg,c.jpg 要放入A、B、C服务器中,a.jpg,b.jpg,c.jpg顺时针找最近的服务器储存,如果B服务器下线,储存在B中的图片暂时找不到,但是不会影响其他服务器的图片,通过了hash运算,有虚拟节点

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance source
	hash-type consistent(动态算法)
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

uri

对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后,根据最终结果将请求转发到后端指定服务器

适用于后端是缓存服务器场景

默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性

hash

此算法基于应用层,所以只支持 mode http ,不支持 mode tcp

<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
协议         用户     密码      域名   端口/文件路径(index.html);指令 ? 查询的目标 #

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance uri
	hash-type consistent
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

web1、web2:

root@localhost ~]#echo 172.25.254.10 - index1.html > /usr/share/nginx/html/index1.html

[root@localhost ~]#echo 172.25.254.10 - index2.html > /usr/share/nginx/html/index2.html

[root@localhost ~]#echo 172.25.254.10 - index3.html > /usr/share/nginx/html/index3.html

url_param

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance url_param name,userid  #支持对多个url_param hash
	hash-type consistent
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
	

[root@localhost ~]#systemctl restart haproxy.service

hdr

针对用户每个http头部(header)请求中的指定信息做hash,

此处由 name 指定的http首部将会被取出并做hash计算

web1:

[root@localhost ~]#curl -v 172.25.254.100

......User-Agent: curl/7.76.1......

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance hdr(User-Agent)
	hash-type consistent
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

[root@localhost ~]#systemctl restart haproxy.service

可以指定浏览器

[root@localhost ~]# curl -v 172.25.254.100
[root@localhost ~]#curl -vA "firefox" 172.25.254.100
[root@localhost ~]#curl -vA "sougo" 172.25.254.100

总结:

静态:

static-rr:做r轮询,不管后端服务器的负载不支持socat调整权重

first:第一台服务器到达上限时(崩溃),才会分配下一台

动态:

roundrobin:支持调整权重,谁负载小、权重高就分给哪台服务器

leastconn:谁的链接少,权重高给哪台服务器

#以下静态和动态取决于hash_type是否consistent

source+hash

Uri+hash

url_param+hash

hdr+hash

haproxy的状态监控

stats enable #基于默认的参数启用stats page
stats hide-version #将状态页中haproxy版本隐藏为了安全
stats refresh <delay> #设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix> #自定义stats page uri,默认值:/haproxy?stats
stats auth <user>:<passwd> #认证时的账号和密码,可定义多个用户,每行指定一个用户
#默认:no authentication 随便访问不用认证
stats admin { if | unless } <cond> #启用stats page中的管理功能

启用状态页

listen stats
    mode http
    bind *:5555
    stats enable
    stats refresh 2(刷新值)
    stats uri /status
    stats auth hui:hui

[root@localhost ~]#systemctl restart haproxy.service

打开浏览器

登录状态页

#pid为当前pid号,process为当前进程号,nbproc和nbthread为一共多少进程和每个进程多少个线程
pid = 27134 (process #1, nbproc = 1, nbthread = 1)
#启动了多长时间
uptime = 0d 0h00m04s
#系统资源限制:内存/最大打开文件数/
system limits: memmax = unlimited; ulimit-n = 200029
#最大socket连接数/单进程最大连接数/最大管道数maxpipes
maxsock = 200029; maxconn = 100000; maxpipes = 0
#当前连接数/当前管道数/当前连接速率
current conns = 2; current pipes = 0/0; conn rate = 2/sec; bit rate = 0.000 kbps
#运行的任务/当前空闲率
Running tasks: 1/14; idle = 100 %
active UP: #在线服务器
backup UP: #标记为backup的服务器
active UP, going down: #监测未通过正在进入down过程
backup UP, going down: #备份服务器正在进入down过程
active DOWN, going up: #down的服务器正在进入up过程
backup DOWN, going up: #备份服务器正在进入up过程
active or backup DOWN: #在线的服务器或者是backup的服务器已经转换成了down状态
not checked: #标记为不监测的服务器
#active或者backup服务器人为下线的
active or backup DOWN for maintenance (MAINT)
#active或者backup被人为软下线(人为将weight改成0)
active or backup SOFT STOPPED for maintenance

backend server信息

session rate(每秒的连接会话信息): Errors(错误统计信息):
cur:每秒的当前会话数量 : Req:错误请求量
max:每秒新的最大会话数量 conn:错误链接量
limit:每秒新的会话限制量 Resp:错误响应量
sessions(会话信息): Warnings(警告统计信息):
cur:当前会话量 Retr:重新尝试次数
max:最大会话量 Redis:再次发送次数
limit: 限制会话量
Total:总共会话量 Server(real server信息):
LBTot:选中一台服务器所用的总时间 Status:后端机的状态,包括UP和DOWN
Last:和服务器的持续连接时间 LastChk:持续检查后端服务器的时间
Wght:权重
Bytes(流量统计): Act:活动链接数量
In:网络的字节输入总量 Bck:备份的服务器数量
Out:网络的字节输出总量 Chk:心跳检测时间
Dwn:后端服务器连接后都是DOWN的数量
Denied(拒绝统计信息): Dwntme:总的downtime时间
Req:拒绝请求量 Thrtle:server 状态
Resp:拒绝回复量

基于cookie的会话保持

cookie保存在浏览器里,给服务器发送了一个会话让服务器一直保持;

session保存在服务器上,服务器发送一个数据包让浏览器缓存;

号外号外:不支持 tcp mode,使用 http mode

概念:cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址hash

调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少, 已经被session

共享服务器代替;

打开动态算法source+hash

listen webserver_80
	bind 172.25.254.100:80
	mode http
	balance source
	hash-type consistent(动态算法)
	server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5
	server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

打开不同的浏览器访问172.25.254.100

配置选项:

cookie name [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [
preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ](这些是参数)
常用的参数:
name: #cookie 的 key名称,用于实现持久连接
insert: #插入新的cookie,默认不插入cookie
indirect: #如果客户端已经有cookie,则不会再发送cookie信息
nocache: #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,
			#因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

how to do?

打开hap

[root@localhost ~]#vim /etc/haproxy/haproxy.cfg

使用最常用的roundrobin

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server web1 172.25.254.10:80 cookie liu check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 cookie hui check inter 2 fall 3 rise 5 weight 1
  :wq

验证cookie信息

通过命令行验证:

[root@web1 ~]# curl -b WEBCOOKIE=liu 172.25.254.100

[root@web1 ~]# curl -b WEBCOOKIE=hui 172.25.254.100

IP透传技术

web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、行为分析、区域排行等场景。

打开hap

[root@localhost ~]#vim /etc/haproxy/haproxy.cfg

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

web服务器日志格式配置

#apache 配置:
LogFormat "%{X-Forwarded-For}i %a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%
{User-Agent}i\"" combined
#nginx 日志格式:
$proxy_add_x_forwarded_for: 包括客户端IP和中间经过的所有代理的IP
$http_x_forwarded_For: 只有客户端IP
log_format main '"$proxy_add_x_forwarded_for" - $remote_user [$time_local]
"$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_For';
#查看日志如下:
[root@rs1 ~]# tail -n 3 /var/log/nginx/access.log
"172.25.254.10, 192.168.0.10" 192.168.0.10 - - [10/Jul/2024:16:15:00 +0800] "GET
/ HTTP/1.1"200 18 "-" "curl/7.29.0" "172.25.254.10"
[root@rs2 ~]# tail -n 3 /etc/httpd/logs/access_log
172.25.254.10 192.168.0.10 - - [11/Jul/2024:00:15:00 +0800] "GET / HTTP/1.1" 200
27 "-" "curl/7.29.0"

打开web1、web2:

[root@web2 ~]# cat /var/log/nginx/access.log

在web1停止nginx服务

[root@web1 ~]# systemctl disable nginx.service

[root@web1 ~]# systemctl stop nginx.service

在web1上装http服务:

[root@web1 ~]# dnf install httpd -y

[root@web1 ~]# echo web36 -172.25.254.10 > /var/www/html/index.html

[root@web1 ~]# systemctl start httpd

curl 一下172.25.254.100

[root@web1 ~]# cat /etc/httpd/logs/access_log

172.25.254.100 - - [12/Aug/2024:20:45:01 +0800] "GET / HTTP/1.1" 200 21 "-" "curl/7.76.1" 172.25.254.100 - - [12/Aug/2024:20:45:03 +0800] "GET / HTTP/1.1" 200 21 "-" "curl/7.76.1"

加参数到apache的日志中:

[root@web1 ~]#vim /etc/httpd/conf/httpd.conf

%{X-Forwarded-For}i

[root@web1 ~]# systemctl restart httpd.service

[root@web1 ~]# cat /etc/httpd/logs/access_log

curl 一下172.25.254.100

[root@web1 ~]# cat /etc/httpd/logs/access_log

http是七层,tcp是四层

怎么配置nginx:

send-proxy

server webserver1 192.168.0.101:80 send-proxy weight 1 check inter 3s fall 3
rise 5

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webcluster
    bind *:80
    mode tcp
    balance roundrobin
    server web1 172.25.254.10:80 send-proxy check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1

[root@haproxy ~]# systemctl restart haproxy.service

在web2上:

[root@web2 ~]# vim /etc/nginx/nginx.conf

proxy_protocol

server {
        listen       80 proxy_protocol;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

[root@web2 ~]# systemctl restart nginx.service

在日志中加一条:

' "$proxy_protocol_addr"'

[root@web2 ~]# systemctl restart nginx.service

[root@web2 ~]# tail /var/log/nginx/access.log

curl 一下172.25.254.100

haproxy的访问控制列表应用

ACL

概念:它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配)即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。

配置选项:

#用acl来定义或声明一个acl
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型

名称:对acl命名

acl  test   path_end    -m    sub    /a
#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大
小写,比如:my_acl和My_Acl就是两个完全不同的acl5.8.1.2 ACL-criterion

ACL匹配规范(判断条件):

hdr string,提取在一个HTTP请求报文的首部
hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出
现次数
常用:
hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin(以什么开头)
hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end(以什么结尾)
hdr_dom([<name> [,<occ>]]):域匹配,header中的dom(host)(是什么什么)

hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配 模糊匹配c 报文中a/b/c也会匹
配

栗栗栗栗子:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

frontend webcluster
    bind *:80
    mode http
    acl test hdr(dom) -i www.huihui.org
    use_backend webcluster-host if test
    default_backend default-host

backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
:wq

[root@haproxy ~]# systemctl restart haproxy.service

在Windows怎么解析?

打开c盘——>Windows——>system32——>drivers——>etc——>hosts——>用记事本打开——>编辑——>保存

172.25.254.100   www.huihui.org

测试一下

hdr_dom:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.huihui.org
    use_backend webcluster-host if test
    default_backend default-host

backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
:wq

[root@haproxy ~]# systemctl restart haproxy.service

hdr_end

frontend webcluster
    bind *:80
    mode http
    acl test hdr_end(host) -i .org
    use_backend webcluster-host if test
    default_backend default-host

backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

测试:

base

改acl那块:

acl test base_sub -m sub hui

在C盘+                 ctrl+s保存

172.25.254.100    www.huihui.org    www.huihui.com    bbs.huihui.org    www.test.com

测试:

在web1下

[root@web1 ~]# mkdir /var/www/html/hui -p
[root@web1 ~]# echo 172.25.254.10 xixi > /var/www/html/hui/index.html
[root@web1 ~]# curl 172.25.254.10/hui/index.html
172.25.254.10 xixi

测试:

base_reg(以什么结尾)

acl test base_reg -i hui/$

base包含path

acl test path_sub -m sub hui

测试:

利用ACL做动静分离等访问控制

ACL示例域名匹配

打开hap

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

acl domain hdr_dom(host) -i www.huihui.org
use_backend webcluster-host if domain

[root@haproxy ~]# systemctl restart haproxy.service

测试:

ACL示例基于源IP或子网调度访问

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24
use_backend webcluster-host if ctrl_ip

[root@haproxy ~]# systemctl restart haproxy.service

测试:

拒绝访问how to do?

acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24
 http-request deny if ctrl_ip

[root@haproxy ~]# systemctl restart haproxy.service

测试:

ACL示例匹配浏览器类型

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

acl badwebrowers hdr_sub(User-Agent) -i curl wget
 http-request deny if badwebrowers

测试:

ACL示例基于文件后缀名实现动静分离

在web1上:

[root@web1 ~]# dnf install php -y

[root@web1 ~]# systemctl restart httpd

[root@web1 ~]# vim /var/www/html/index.php

<?php
        phpinfo
?>
:wq

[root@web1 ~]# pwd

/root

[root@web1 ~]# cat /var/www/html/index.php

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

  acl static path_end -i .html .jpg .png .css .js
    acl php    path_end -i .php
    use_backend webcluster-host if php
:wq

在web2上:

[root@web2 ~]# mkdir /usr/share/nginx/html/static -p [root@web2 ~]# echo static - 172.25.254.20 > /usr/share/nginx/html/static/index.html [root@web2 ~]# curl 172.25.254.20/static/ static - 172.25.254.20

在web1上:

[root@web1 ~]# mkdir -p /var/www/html/php [root@web1 ~]# cp /var/www/html/index.php /var/www/html/php/

回hap

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

   acl static path_sub -m sub static
    acl php    path_sub -m sub php
    use_backend webcluster-host if php

测试:

haproxy自动逸错误页面内容

打开web2:

[root@web2 ~]# systemctl stop nginx.service

打开hap

[root@haproxy ~]# rpm -ql haproxy | grep http

[root@haproxy ~]# cat /usr/share/haproxy/503.http

[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p

[root@haproxy ~]# vim /etc/haproxy/errorpage/badpage.http

HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8
<html><body><h1>why not hang out other page?</h1>
hhhoo
</body></html>

:wq

[root@haproxy ~]# systemctl restart httpd

指定:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

在defaults写入

   errorfile 503     /etc/haproxy/errorpage/badpage.http

[root@haproxy ~]# systemctl restart haproxy.service

测试打开浏览器172.25.254.100:

基于http重定向错误页面

打开hap

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

把上面的badpage注释写入

errorloc 503 https://www.baidu.com

[root@haproxy ~]# systemctl restart haproxy.service

打开浏览器刷新一下页面自动跳转百度

haproxy的http实现

在hap:

[root@haproxy ~]# mkdir -p /etc/haproxy/certs

[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/huihui.org.key -x509 -days 365 -out /etc/haproxy/certs/huihui.org.crt

Country Name (2 letter code) [XX]:CN   
State or Province Name (full name) []:Yunnan
Locality Name (eg, city) [Default City]:Yuxi
Organization Name (eg, company) [Default Company Ltd]:huihui
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.huihui.org
Email Address []:admin@huihui.org

[root@haproxy ~]# ls /etc/haproxy/certs/huihui.org.crt huihui.org.key

[root@haproxy ~]# cat /etc/haproxy/certs/huihui.org.key /etc/haproxy/certs/huihui.org.crt > /etc/haproxy/certs/huihui.pem

[root@haproxy ~]# cat /etc/haproxy/certs/huihui.pem

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen web-https
    bind *:443 ssl crt  /etc/haproxy/certs/huihui.pem
    mode http
    balance roundrobin
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
:wq

[root@haproxy ~]# systemctl restart haproxy.service

[root@haproxy ~]# ll /etc/haproxy/certs/huihui.pem

-rw-r--r-- 1 root root 3144 8月 13 15:55 /etc/haproxy/certs/huihui.pem

[root@haproxy ~]# netstat -antlupe | grep haproxy

查看443端口是否打开

打开浏览器:https://172.25.254.100

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

frontend webcluster
    bind *:80
    mode http
    redirect scheme https if !{ssl_fc}			(全站加密)

backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

backend default-host
   mode http
   server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
   
   
   
 listen web-https
    bind *:443 ssl crt  /etc/haproxy/certs/huihui.pem
    mode http
    balance roundrobin
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

[root@haproxy ~]# systemctl restart haproxy.service

[root@haproxy ~]# netstat -antlupe | grep haproxy

配置文件太多可以放到子配置文件中:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg (主配置文件)

把里面的内容都注释,只剩global和defaults

[root@haproxy ~]# vim /lib/systemd/system/haproxy.service(子配置文件)

[root@haproxy ~]# cd /etc/haproxy/conf.d/

[root@haproxy conf.d]# ls

[root@haproxy conf.d]# vim webcluster.cfg(可以在里面写配置参数)

状态页
listen stats
    mode http
    bind *:9999
    stats enable
    stats refresh 3
    stats uri /status
    stats auth hui:hui
:wq

[root@haproxy conf.d]# systemctl restart haproxy.service

打开浏览器:172.25.254.100:9999/status

建议在子配置文件中写相关配置,互不干扰,容易阅读;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2047169.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

如何将本地组件库上传到npm上

如何把本地开发的组件发布到npm上面&#xff0c;我们需要去了解vue封装组件的原理&#xff0c;利用vue.use(plugin)这个api, 我们需要把封装好的组件打包成vue库&#xff0c;并提供install方法发布到npm上去&#xff0c;Vue.use(plugin)自动执行插件中的install方法。 我们在这…

建立一个能高效记录、整理编程心得,又易检索、回顾的编程笔记系统

构建一个既高效记录编程心得又便于快速回顾的编程笔记系统至关重要。此系统不仅是知识管理的基石&#xff0c;还能显著提升学习效率&#xff0c;确保在关键时刻迅速获取所需信息。 基于我超过十年的软件编程、项目管理及项目运维的深厚经验&#xff0c;我深刻体会到&#xff0c…

[000-01-030].第3节 :搭建Zookeeper集群环境

1.搭建Zookeeper集群环境 1.1.集群安装&#xff1a; a.集群规划&#xff1a; 在 hadoop103(192.168.2.3)、hadoop104(192.168.2.4) 和 hadoop105&#xff08;(192.168.2.5&#xff09; 三个节点上都部署 Zookeeper b.解压安装&#xff1a; 1.下载zookeeper压缩版本&#x…

文件批量上传,oss使用时间戳解决同名问题 以及一些sql bug

1.文件批量上传 ApiOperation(value "文件批量上传")PostMapping("/multipleImageUpload")Transactional(rollbackFor Exception.class)public Result multipleImageUpload(ApiParam(name "files",value "文件",required true) R…

2024新型数字政府综合解决方案(六)

新型数字政府综合解决方案通过融合人工智能、大数据、区块链和云计算技术&#xff0c;构建了一个全方位智能化的政务平台&#xff0c;旨在提升政府服务的效率、透明度和公众参与度。该方案实现了跨部门的数据互联互通与实时更新&#xff0c;利用先进的数据分析和自动化处理技术…

38-PCB布局实战实战及优化

1.先对布局好的器件进行锁定 1.根据模块化布局 2.电容尽量靠近ic附近&#xff0c;可以起到很好的滤波效果 3.复位按键尽量摆在容易按键的地方&#xff0c;比如周围 。。。。 最后进行对齐

KubeSphere 社区双周报| 2024.08.02-08.15

KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过 commit 的贡献者&#xff0c;并对近期重要的 PR 进行解析&#xff0c;同时还包含了线上/线下活动和布道推广等一系列社区动态。 本次双周报涵盖时间为&#xff1a;2024.08.02-08.15…

机器学习(2)-- KNN算法之手写数字识别

KNN算法 KNN&#xff08;K-Nearest Neighbor&#xff0c;K最近邻&#xff09;算法是一种用于分类和回归的非参数统计方法&#xff0c;尤其在分类问题中表现出色。在手写数字识别领域&#xff0c;KNN算法通过比较测试样本与训练样本之间的距离&#xff0c;找到最近的K个邻居&am…

智能监控,无忧仓储:EasyCVR视频汇聚+AI智能分享技术为药品仓库安全保驾护航

随着科技的飞速发展&#xff0c;药品仓库的安全管理正迎来前所未有的变革。药品作为直接关系到公众健康的重要物资&#xff0c;其安全存储和监管显得尤为重要。在这个背景下&#xff0c;视频汇聚平台EasyCVR视频智能管理系统的应用&#xff0c;为药品仓库的安全监管提供了强有力…

【Bug记录】友元函数不能访问私有成员?

项目场景&#xff1a; 问题源码&#xff1a; class Person { public:friend void Display(const Person& p, const Student& s);protected:string _name "1"; // 姓名 };class Student : public Person { protected:string _s "2"; };void Disp…

HTML静态网页成品作业(HTML+CSS)——自行车介绍网页设计制作(1个页面)

&#x1f389;不定期分享源码&#xff0c;关注不丢失哦 文章目录 一、作品介绍二、作品演示三、代码目录四、网站代码HTML部分代码CSS部分代码 五、源码获取 一、作品介绍 &#x1f3f7;️本套采用HTMLCSS&#xff0c;未使用Javacsript代码&#xff0c;共有1个页面。 &#x1…

在业务增长、风险管理和网络安全之间取得平衡

疫情过后&#xff0c;商业环境和文化发生了重大变化&#xff0c;值得我们仔细考虑。我们都变得更加依赖技术&#xff0c;我们的工作方法也发生了变化&#xff0c;网络风险变得更加突出和持久。 监管领域变得更加积极和严格&#xff0c;特别是在安全和数据隐私问题方面。人工智…

OpenCV图像滤波(18)空间梯度计算函数spatialGradient()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 计算图像在x和y两个方向上的一阶导数&#xff0c;使用Sobel算子。 等价于调用&#xff1a; Sobel( src, dx, CV_16SC1, 1, 0, 3 ); Sobel( src,…

AI赋能软件测试:从自动化到智能化,让测试工作事半功倍

引言 在当今这个日新月异的数字时代&#xff0c;人工智能&#xff08;AI&#xff09;正以不可阻挡之势渗透并重塑着各行各业&#xff0c;其中&#xff0c;软件开发与测试领域更是迎来了前所未有的变革。随着软件系统的复杂性日益增加&#xff0c;用户对软件质量、性能及安全性的…

全息光存储技术能否取代硬盘?

随着云计算的迅猛发展及其支持的应用程序的日益丰富&#xff0c;现有数据存储技术面临着前所未有的挑战。目前&#xff0c;云存储主要依赖于固态硬盘&#xff08;SSD&#xff09;和硬盘驱动器&#xff08;HDD&#xff09;。其中&#xff0c;HDD因其成本低廉&#xff0c;在大容量…

LVGL系列2--linux + lvglv8 + vscode 移植

LVGL系列 一、LVGL移植 LVGL系列1–AT32移植LVGL_V8具体步骤 LVGL系列2–linux lvglv8 vscode 移植 二、输入设备 LVGL系列3–纯物理(外部)按键&#xff0c;数字键盘控制控件 文章目录 LVGL系列一、LVGL移植二、输入设备 一、新建文件夹并克隆源码官方仓库 7.11.0官方仓库…

BL196MQTT远程IO模块助力智能楼宇自动化升级

在智能楼宇自动化领域&#xff0c;每一个细节的优化都能带来整体效率与舒适度的显著提升。钡铼技术的BL196MQTT远程IO模块&#xff0c;以其卓越的灵活性和强大的性能&#xff0c;正在成为这一领域中推动楼宇自动化升级的关键力量。 钡铼技术IOy系列&#xff1a;创新与灵活性的…

R语言统计分析——OLS回归2

参考资料&#xff1a;R语言实战【第2版】 1、简单线性回归 本例使用R语言中基础安装中的数据集women&#xff0c;来通过身高预测体重&#xff0c;获得一个等式帮助我们分辨出那些过重或过轻的个体。 # 拟合数据 fit<-lm(weight~height,datawomen) # 查看数据拟合结果 summ…

HTTPS通讯全过程

HTTPS通讯全过程 不得不说&#xff0c;https比http通讯更加复杂惹。在第一次接触https代码的时候&#xff0c;不知道为什么要用用证书&#xff0c;公钥是什么&#xff1f;私钥是什么&#xff1f;他们作用是什么&#xff1f;非对称加密和对称加密是啥&#xff1f;天&#xff0c;…

Redis 单线程为何还能处理速度那么快?

Redis 单线程为何还能处理速度那么快&#xff1f; &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收藏不迷路&#x1f496; Redis&#xff0c;作为一款单进程单线程的内存型数据库&#xff0c;其卓越的处理速度令人印象深刻。那么&#xff0c;它是如何实现这一点的呢…