当前位置: 首页 > news >正文

【HAProxy09】企业级反向代理HAProxy高级功能之压缩功能与后端服务器健康性监测

HAProxy 高级功能

介绍 HAProxy 高级配置及实用案例

压缩功能

对响应给客户端的报文进行压缩,以节省网络带宽,但是会占用部分CPU性能

建议在后端服务器开启压缩功能,而非在HAProxy上开启压缩

注意:默认Ubuntu的包安装nginx开启压缩功能

配置选项

compression algo  <algorithm> ...           #启用http协议中的压缩机制,常用算法有gzip,deflate#压缩算法<algorithm>支持下面类型:identity                                  #debug调试使用的压缩方式gzip                                      #常用的压缩方式,与各浏览器兼容较好deflate                                   #有些浏览器不支持raw-deflate                               #新式的压缩方式
compression type <mime type> ...            #要压缩的文件类型MIME#示例:
compression algo gzip deflate
compression type compression type text/plain text/html text/css text/xml text/javascript application/javascript

配置示例

listen  web_hostbind 10.0.0.7:80mode httpbalance  roundrobinlog globaloption httplogcompression algo gzip deflate   #启用压缩和指定算法compression type compression type text/plain text/html text/css text/xml text/javascript application/javascript    #指定压缩文件类型         server web1 10.0.0.17:80  cookie web1 check inter 3000 fall 3 rise 5server web2 10.0.0.27:80  cookie web2 check inter 3000 fall 3 rise 5#后端服务器准备一个文本文件
[root@web01 html]#ll -h
total 1.2M
-rw-r--r-- 1 nginx nginx 1.2M Nov 12 18:08 1.txt

验证压缩功能

--compressed:这个选项告诉 curl 请求服务器发送压缩的响应(如果服务器支持的话)。通常,服务器会使用 gzip 或 deflate 算法压缩数据以减少传输时间。[root@master-db ~]#curl -b /root/cookie.txt 172.16.1.211/1.txt -Iv --compressed 
* About to connect() to 172.16.1.211 port 80 (#0)
*   Trying 172.16.1.211...
* Connected to 172.16.1.211 (172.16.1.211) port 80 (#0)
> HEAD /1.txt HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 172.16.1.211
> Accept: */*
> Accept-Encoding: deflate, gzip
> Cookie: WEBSRV=web01

没开启压缩功能前

 

开启压缩功能后

后端服务器健康性监测

三种状态监测方式

基于四层的传输端口做状态监测,此为默认方式
基于指定 URI 做状态监测,需要访问整个页面资源,占用更多带宽
基于指定 URI 的 request 请求头部内容做状态监测,占用较少带宽,建议使用此方式 

基于应用层http协议进行健康性检测

基于应用层http协议,采有不同的监测方式,对后端real server进行状态监测

注意: 此方式会导致在后端服务器生成很多的HAProxy发起的访问日志

option httpchk    #支持Listen和backendf块,启用七层健康性检测,对tcp 和 http 模式都支持,默认为:OPTIONS / HTTP/1.0,nginx默认不支持OTIONS
option httpchk <uri>
option httpchk <method> <uri>
option httpchk <method> <uri> <version>
#期望以上检查得到的响应码
http-check expect [!] <match> <pattern>
#示例:
http-check expect status 200
http-check expect ! rstatus ^5 #支持正则表达式
#关于HTTP/1.1的说明
<version> is the optional HTTP version string. It defaults to "HTTP/1.0" but 
some servers might behave incorrectly in HTTP 1.0, so turning it to HTTP/1.1 may 
sometimes help. Note that the Host field is         mandatory in HTTP/1.1, and 
as a trick, it is possible to pass it after "\r\n" following the version string.

配置示例

listen  web_hostbind 10.0.0.7:80mode httpbalance  roundrobin#option httpchk GET /monitor/check.html               #默认HTTP/1.0#option httpchk GET /monitor/check.html HTTP/1.0#option httpchk GET /monitor/check.html HTTP/1.1      #注意:HTTP/1.1强制要求必须有Host字段option httpchk HEAD  /monitor/check.html HTTP/1.1\r\nHost:\ www.wang.org #使用HEAD减少网络流量,新版不支持\r\nHost:\ www.wang.orghttp-check send hdr Host www   #注意:新版要求:HTTP/1.1强制要求必须有Host字段cookie  SERVER-COOKIE  insert  indirect nocacheserver web1 10.0.0.17:80  cookie web1 check inter 3000 fall 3 rise 5server web2 10.0.0.27:80  cookie web2 check inter 3000 fall 3 rise 5#在所有后端服务建立检测页面
[root@backend ~]#mkdir /var/www/html/monitor/[root@backend ~]#echo  monitor > /var/www/html/monitor/check.html#关闭一台Backend服务器
[root@backend1 ~]#systemctl stop httpd

验证http监测

查看到状态页,可以看到启用了七层检测功能:LastChk字段:L7  

listen www.web01.combalance roundrobinbind 0.0.0.0:80log globaloption httplogoption httpchk HEAD  / HTTP/1.1\r\nHost:\ www.web01.comcookie WEBSRV insert nocache indirectserver 10.0.0.52 10.0.0.52:80  check  inter 3000 fall 3 rise 5 cookie web01server 10.0.0.53 10.0.0.53:80  check  inter 3000 fall 3 rise 5 cookie web02

 

http://www.lryc.cn/news/484692.html

相关文章:

  • PostgreSQL中表的数据量很大且索引过大时怎么办
  • 【QML】QML多线程应用(WorkerScript)
  • 认证鉴权框架SpringSecurity-1--概念和原理篇
  • 计算器上的MC、MR、M+、M—、CE是什么意思?
  • 无人机飞手执照处处需要,森林、石油管道、电力巡检等各行业都需要
  • 计算机网络——路由选择算法
  • 【前端】技术演进发展简史
  • 深入解析贪心算法及其应用实例
  • 电子工牌独立双通道定向拾音方案(有视频演示)
  • 举例理解LSM-Tree,LSM-Tree和B+Tree的比较
  • React Native 全栈开发实战班 - 核心组件与导航
  • Leecode热题100-35.搜索插入位置
  • 密码学知识点整理二:常见的加密算法
  • Linux如何将文件或目录打成rpm包?-- rpmbuild打包详解
  • RabbitMQ-死信队列(golang)
  • 爬虫开发工具与环境搭建——环境配置
  • 15.UE5等级、经验、血条,魔法恢复和消耗制作
  • 【Homework】【5】Learning resources for DQ Robotics in MATLAB
  • vue3中 ref和reactive的区别
  • 第十四章 Spring之假如让你来写AOP——雏形篇
  • 群控系统服务端开发模式-应用开发-前端个人资料开发
  • 动态规划技巧点
  • 深度学习之pytorch常见的学习率绘制
  • Spring Boot集成SQL Server快速入门Demo
  • 低代码牵手 AI 接口:开启智能化开发新征程
  • 【已解决】git push一直提示输入用户名及密码、fatal: Could not read from remote repository的问题
  • python语言基础-4 常用模块-4.13 其他模块
  • 微信小程序=》基础=》常见问题=》性能总结
  • JWT深度解析:Java Web中的安全传输与身份验证
  • 使用Java爬虫获取商品订单详情:从API到数据存储