对响应给客户端的报文进行压缩,以节省网络带宽,但是会占用部分CPU性能,建议在后端服务器开启压缩功能,而非在HAProxy上开启压缩
配置选项
compression algo <algorithm> ... #启用http协议中的压缩机制,常用算法有gzip,deflate
<algorithm>支持下面类型:
identity #debug调试使用的压缩方式
gzip #常用的压缩方式,与各浏览器兼容较好
deflate #有些浏览器不支持
raw-deflate #新式的压缩方式
compression type <mime type> ... #要压缩的文件类型
#示例:
compression algo gzip deflate
compression type text/html text/csstext/plain
配置示例
listen web_host
bind 10.0.0.7:80
mode http
balance roundrobin
log global
option httplog
compression 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 5
server web2 10.0.0.27:80 cookie web2 check inter 3000 fall 3 rise 5
#后端服务器准备一个文本文件
[root@centos7 ~]#ll /var/www/html/m.txt -h
-rwxr-xr-x 1 root root 772K Apr 2 12:56 /var/www/html/m.txt
验证压缩功能
[root@centos6 ~]#curl -is --compressed 10.0.0.7/m.txt|less
HTTP/1.1 200 OK
date: Thu, 02 Apr 2020 05:00:26 GMT
server: Apache/2.4.6 (CentOS) PHP/5.4.16
last-modified: Thu, 02 Apr 2020 04:56:25 GMT
etag: W/"c0ef6-5a2479f7aee68"
accept-ranges: bytes
content-type: text/plain; charset=UTF-8
set-cookie: WEBSRV=web1; path=/
cache-control: private
content-encoding: deflate
transfer-encoding: chunked
vary: Accept-Encoding
Feb 2 18:49:27 centos7 journal: Runtime journal is using 6.0M (max allowed 48.6M, trying to leave 72.9M free of 480.1M available → current limit 48.6M).
Feb 2 18:49:27 centos7 kernel: Initializing cgroup subsys cpuset
Feb 2 18:49:27 centos7 kernel: Initializing cgroup subsys cpu
Feb 2 18:49:27 centos7 kernel: Initializing cgroup subsys cpuacct
......
本文链接:https://www.yunweipai.com/35293.html
网友评论comments