LAMP架构应用实战—Apache服务mod_deflate模块介绍

时间:2022-07-25
本文章向大家介绍LAMP架构应用实战—Apache服务mod_deflate模块介绍,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

LAMP架构应用实战—Apache服务

mod_deflate压缩模块介绍

前面介绍了缓存模块mod_expires一些相关信息,今天来介绍下另一个比较常用的模块mod_deflate压缩模块

一:mod_deflate模块介绍

mod_deflate压缩模块提供了DEFLATE输出过滤器,允许服务器将内容发送给客户端之前进行压缩,节省带宽资源

二:mod_deflate模块查看

直接编译安装查看如下

[root@Centos modules]# /application/apache/bin/apachectl -l|grep mod_deflate
mod_deflate.c

以DSO方式编译安装查看如下

[root@Centos modules]# ls -l|grep defla
-rwxr-xr-x. 1 root root  86591 Sep 18 15:26 mod_deflate.so

但是以上两种方式不能同时安装,否则会出现错误

[root@Centos conf]# /application/apache/bin/apachectl -M|grep deflate
 deflate_module (shared)

查看帮助

[root@Centos httpd-2.4.23]# ./configure --help|grep deflate    
  --enable-deflate        Deflate transfer encoding support

编译、或以DSO安装方式安装与前面的缓存模块安装相同,这里就不再介绍了

三:mod_deflate配置

配置前看下HTTP头的信息

[root@Centos modules]# curl -I http://blog.abc.com:9999/
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 07:37:58 GMT
Server: Apache/2.4.23 (Unix)
Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT
ETag: "1f-53c122a061992"
Accept-Ranges: bytes
Content-Length: 31
Cache-Control: max-age=31104000
Expires: Wed, 13 Sep 2017 07:37:58 GMT
Content-Type: text/html

备份并配置模块

[root@Centos conf]# cp httpd.conf httpd.conf.0918
[root@Centos conf]# vi httpd.conf 

中间内容省略

<IfModule mod_deflate.c>
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain /text/xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/css
</IfModule>
"httpd.conf" 521L, 18903C written

语法检查与服务重启

[root@Centos conf]# /application/apache/bin/apachectl -t
Syntax OK
[root@Centos conf]# /application/apache/bin/apachectl graceful
[root@Centos conf]# lsof -i tcp:9999
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd    2156   root    8u  IPv6  16255      0t0  TCP *:distinct (LISTEN)
httpd   13911 daemon    8u  IPv6  16255      0t0  TCP *:distinct (LISTEN)
httpd   13912 daemon    8u  IPv6  16255      0t0  TCP *:distinct (LISTEN)
httpd   13913 daemon    8u  IPv6  16255      0t0  TCP *:distinct (LISTEN)
[root@Centos conf]# ps -ef|grep http
root 2156 10 15:13 ? 00:00:00 /application/apache2.4.23/bin/httpd -k restart
daemon13911 2156  0 15:49  00:00:00 /application/apache2.4.23/bin/httpd -k restart
daemon 13912 2156  0 15:49   00:00:00 /application/apache2.4.23/bin/httpd -k restart
daemon 13913  2156  0 15:49 ?00:00:00 /application/apache2.4.23/bin/httpd -k restart
root  14003  1941 015:50 pts/0   00:00:00 grep http

测试配置内容

[root@Centos conf]# curl -I http://blog.abc.com:9999/deflate.html
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 09:32:39 GMT
Server: Apache/2.4.23 (Unix)
Last-Modified: Sun, 18 Sep 2016 08:10:40 GMT
ETag: "309-53cc3bb2de520"
Accept-Ranges: bytes
Content-Length: 777
Vary: Accept-Encoding       出现这个提示表明已启用压缩
Cache-Control: max-age=31104000
Expires: Wed, 13 Sep 2017 09:32:39 GMT
Content-Type: text/html

把之前的配置内容注释掉

[root@Centos conf]# /application/apache/bin/apachectl -t
Syntax OK
[root@Centos conf]# /application/apache/bin/apachectl graceful   
[root@Centos conf]# ps -ef |grep http
root 14492 1  0 17:03 ?  00:00:00 /application/apache2.4.23/bin/httpd -k restart
daemon  14998  14492  0 17:39 ?  00:00:00 /application/apache2.4.23/bin/httpd -k restart
daemon 14999  14492  0 17:39 ?  00:00:00 /application/apache2.4.23/bin/httpd -k restart
daemon 15000  14492  0 17:39 ?00:00:00 /application/apache2.4.23/bin/httpd -k restart
root  15083 148  0 17:39 pts/0  00:00:00 grep http
[root@Centos conf]# lsof -i tcp:9999
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   14492   root    8u  IPv6  28964      0t0  TCP *:distinct (LISTEN)
httpd   14998 daemon    8u  IPv6  28964      0t0  TCP *:distinct (LISTEN)
httpd   14999 daemon    8u  IPv6  28964      0t0  TCP *:distinct (LISTEN)
httpd   15000 daemon    8u  IPv6  28964      0t0  TCP *:distinct (LISTEN)
[root@Centos conf]# curl -I http://blog.abc.com:9999/deflate.html
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 09:40:08 GMT
Server: Apache/2.4.23 (Unix)
Last-Modified: Sun, 18 Sep 2016 08:10:40 GMT
ETag: "309-53cc3bb2de520"
Accept-Ranges: bytes
Content-Length: 777
Cache-Control: max-age=31104000
Expires: Wed, 13 Sep 2017 09:40:08 GMT
Content-Type: text/html

发现没有配置的时候是不会出现相关提示内容

提示:如果编译安装后,主配置文件中没有加入deflate此模块,那么我们可以手要增加一个这样的模块

LoadModule deflate_module modules/mod_deflate.so