mod_deflate模块提供了DEFLATE输出过滤器,允许服务器在将输出内容发送到客户端以前进行压缩,以节约带宽了,下面我们来介绍在linux中mod_deflate模块安装配置.最近把博客从虚拟主机搬到 VPS 上,自然一番折腾,估计围绕这一过程,写三四篇博客不是梦,这是第一篇,服务器端的压缩功能 – 服务器在返回内容前先对内容做 gzip 压缩,以减小传输的文件大小 – 照 Google 的说法,能减小 90%,但这也不是重点,重点是服务器端不开启 gzip 压缩的话,Google PageSpeed 的测试就会扣分 – 我个人特别在意这个分数.
Apache 下,压缩功能由 mod_deflate 模块控制.安装#:我的 VPS 系统装的是 openSUSE 13.1 64 位系统,Apache 版本为 2.4,首先查看下系统中是否已经安装 mod_deflate模块,我知道的有两种方法.
当前用户下 执行命令 httpd2 -M,输出的内容大致如下:
chenxsan@zfanw.com:~> httpd2 -M
[Fri Oct 31 13:13:59.278203 2014] [so:warn] [pid 9292] AH01574: module deflate_module is already loaded, skipping
Loaded Modules:
core_module (static)
access_compat_module (static)
so_module (static)
http_module (static)
mpm_prefork_module (static)
unixd_module (static)
systemd_module (static)
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
dir_module (shared)
env_module (shared)
expires_module (shared)
include_module (shared)
log_config_module (shared)
mime_module (shared)
negotiation_module (shared)
setenvif_module (shared)
ssl_module (shared)
userdir_module (shared)
reqtimeout_module (shared)
authn_core_module (shared)
authz_core_module (shared)
php5_module (shared)
rewrite_module (shared)
deflate_module (shared)
得出的结果大致如下:
APACHE_MODULES=”actions alias auth_basic authn_file authz_host authz_groupfile authz_user autoindex cgi dir env expires include log_config mime negotiation setenvif ssl userdir reqtimeout authn_core authz_core mod-userdir php5 mod_rewrite mod_deflate deflate”
我这里显示的结果是已经安装加载了 mod_deflate 模块,假如没有,则使用 a2enmod 来启用:sudo a2enmod deflate 等等,为什么没说安装直接进入启用阶段?因为 mod_deflate 模块在安装 Apache 时已经捎带装上,所以可以跳过安装这个步骤.
启用 mod_deflate 模块后,需要重启 Apache 服务器:sudo rcapache2 restart;启用#如上所述m配置#,启用 mod_deflate 模块后m就可以开始配置了,可以照 openSUSE 的操作说明一步步来,也可以粗野直接点,修改 /etc/apache2/httpd.conf 文件,在文件末加入如下代码:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar|7z)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html --phpfensi.com
或者可以考虑 h5bp 提供的配置.
然后重启 Apache:sudo rcapache2 restart 再跑一趟 PageSpeed,就不会再提服务器压缩的事 。