httpd之mod_deflate---传输压缩过程

引言: 以消耗服务器的cpu的资源为代价,在发送http报文前把数据压缩,从而达到节省流量的效果

实现如下
[root@localhost ~]# httpd -M | grep deflate           ##已加载压缩功能模块
	deflate_module (shared)
[root@localhost conf.d]# grep  deflate /etc/httpd/conf.modules.d/00-base.conf 
LoadModule deflate_module modules/mod_deflate.so

[root@localhost ~]# cp /var/log/messages /main/virtualhost/aweb/messages.txt
[root@localhost ~]# chown a+r /main/virtualhost/aweb/messages.txt
[root@localhost ~]# vim /etc/httpd/conf.d/test.conf
<virtualhost *:80>
servername www.a.com
DocumentRoot /main/virtualhost/aweb
CustomLog "logs/asite_access_log" combined
<Directory "/main/virtualhost/aweb">
    Require all granted
</Directory>
addoutputfilterbytype deflate text/plain  #压缩文件格式为plain纯文本或者html,ps:一般对于图片,MP3,MP4等压缩没啥效果
addoutputfilterbytype deflate text/html	
deflatecompressionlevel 9               ##压缩比9最高。
</virtualhost>

验证如下


[root@vm1 ~]# curl -I www.a.com/messages.txt
HTTP/1.1 200 OK
Date: Fri, 16 Apr 2021 14:31:11 GMT
Server: Apache
Last-Modified: Fri, 16 Apr 2021 14:25:34 GMT
ETag: "c1f4-5c017c0cc976a"
Accept-Ranges: bytes
Content-Length: 49652
Content-Type: text/plain; charset=UTF-8

[root@vm1 ~]# curl -I --compressed www.a.com/messages.txt
HTTP/1.1 200 OK
Date: Fri, 16 Apr 2021 14:41:02 GMT
Server: Apache
Last-Modified: Fri, 16 Apr 2021 14:25:34 GMT
ETag: "c1f4-5c017c0cc976a-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 4427              ##不压缩数据体大小为49652
Content-Type: text/plain; charset=UTF-8
很多浏览器默认开启接受压缩的文件,下图为chrome浏览器截图

httpd之mod_deflate---传输压缩过程_第1张图片

你可能感兴趣的:(httpd,http,html,运维,服务器)