当前位置: 移动技术网 > 网络运营>服务器>Linux > 在Apache服务器上启用GZip压缩静态内容的方法

在Apache服务器上启用GZip压缩静态内容的方法

2019年06月05日  | 移动技术网网络运营  | 我要评论

为了优化网站的访问速度,我们可以通过对静态内容进行压缩,从而减少网页加载的时间,大大节省用户的带宽。在这篇文章中,我将介绍如何使用apache和.htaccess文件进行静态内容压缩。

首先让我介绍一下,我们可以使用两种不同的方法压缩内容:gzip 和 deflate。

介绍

gzip方法在早期的apache版本中使用(在apache 1.3之前)。但在那之后apache引入了deflate方法,相比gzip并没有太大的效果(但仍是非常好的)。然而,gzip在apache 1.3之后不再提供更多的支持。因此,你的apache版本必须大于1.3,如果没有,你必须升级到最新版本的apache。

在使用压缩之前,你必须启用apache的mod_deflate模块。要启用这个模块,你只需要从httpd.conf文件去掉这个模块行。

启用这个模块后,你的服务器准备好提供压缩的内容。但是,服务器只有当它接收到来自客户端的相应头文件时,才会创建压缩内容。所以,现在你需要将下面的代码放置到你网站的htaccess文件,才能通知服务器提供压缩的内容。

.htaccess代码

<ifmodule mod_deflate.c="">
 # force deflate for mangled headers
 # developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
 <ifmodule mod_setenvif.c="">
 <ifmodule mod_headers.c="">
  setenvifnocase ^(accept-encodxng|x-cept-encoding|x{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[x~-]{4,13}$ have_accept-encoding
  requestheader append accept-encoding "gzip,deflate" env=have_accept-encoding
 </ifmodule>
 </ifmodule>
 
 # html, txt, css, javascript, json, xml, htc:
 <ifmodule filter_module="">
 filterdeclare compress
 filterprovider compress deflate resp=content-type $text/html
 filterprovider compress deflate resp=content-type $text/css
 filterprovider compress deflate resp=content-type $text/plain
 filterprovider compress deflate resp=content-type $text/xml
 filterprovider compress deflate resp=content-type $text/x-component
 filterprovider compress deflate resp=content-type $application/javascript
 filterprovider compress deflate resp=content-type $application/json
 filterprovider compress deflate resp=content-type $application/xml
 filterprovider compress deflate resp=content-type $application/xhtml+xml
 filterprovider compress deflate resp=content-type $application/rss+xml
 filterprovider compress deflate resp=content-type $application/atom+xml
 filterprovider compress deflate resp=content-type $application/vnd.ms-fontobject
 filterprovider compress deflate resp=content-type $image/svg+xml
 filterprovider compress deflate resp=content-type $application/x-font-ttf
 filterprovider compress deflate resp=content-type $font/opentype
 filterchain  compress
 filterprotocol compress deflate change=yes;byteranges=no
 </ifmodule>
 
 <ifmodule !mod_filter.c="">
 # legacy versions of apache
 addoutputfilterbytype deflate text/html text/plain text/css application/json
 addoutputfilterbytype deflate application/javascript
 addoutputfilterbytype deflate text/xml application/xml text/x-component
 addoutputfilterbytype deflate application/xhtml+xml application/rss+xml 
 addoutputfilterbytype deflate application/atom+xml
 addoutputfilterbytype deflate image/svg+xml application/vnd.ms-fontobject 
 addoutputfilterbytype deflate application/x-font-ttf font/opentype
 </ifmodule>
</ifmodule>

将上面的代码放置在你的htaccess文件之后,看看你网站的请求头部。你可以看到一个额外的头“accept-encoding“。这意味着请求的客户端能够处理给定的压缩类型的内容,并将提供压缩内容。
 
accept-encoding:gzip,deflate,sdch

结果

看看下面的图片,有多少被压缩了。

201585103717458.jpg (533×211)

从上面的图片可以看出,实际页面大小707kb,使用压缩后是401kb。因此,它最终会提高你的网站的性能。

我强烈建议你把网站静态内容做压缩处理,因为没有理由不这么做,这是web开发的一个最佳实践。

 

然后附上gzip的一些基本的常用命令:

1.命令格式:

gzip[参数][文件或者目录]

2.命令功能:

gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出".gz"的扩展名。

3.命令参数:

-a或--ascii  使用ascii文字模式。

-c或--stdout或--to-stdout  把压缩后的文件输出到标准输出设备,不去更动原始文件。

-d或--decompress或----uncompress  解开压缩文件。

-f或--force  强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接。

-h或--help  在线帮助。

-l或--list  列出压缩文件的相关信息。

-l或--license  显示版本与版权信息。

-n或--no-name  压缩文件时,不保存原来的文件名称及时间戳记。

-n或--name  压缩文件时,保存原来的文件名称及时间戳记。

-q或--quiet  不显示警告信息。

-r或--recursive  递归处理,将指定目录下的所有文件及子目录一并处理。

-s<压缩字尾字符串>或----suffix<压缩字尾字符串>  更改压缩字尾字符串。

-t或--test  测试压缩文件是否正确无误。

-v或--verbose  显示指令执行过程。

-v或--version  显示版本信息。

-num 用指定的数字num调整压缩的速度,-1或--fast表示最快压缩方法(低压缩比),-9或--best表示最慢压缩方法(高压缩比)。系统缺省值为6。


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网