利用 .HTACCESS 启用 GZIP 压缩静态内容
2012-05-29 12:07
447 查看
![](http://static.oschina.net/uploads/space/2012/0529/120037_OMaw_157182.jpg)
为了优化网站的访问速度,我们可以通过对静态内容进行压缩,从而减少网页加载的时间,大大节省用户的带宽。在这篇文章中,我将介绍如何使用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
结果
看看下面的图片,有多少被压缩了。
![](http://static.oschina.net/uploads/space/2012/0529/120424_nJKX_157182.jpg)
从上面的图片可以看出,实际页面大小707KB,使用压缩后是401KB。因此,它最终会提高你的网站的性能。
结论
我强烈建议你把网站静态内容做压缩处理,因为没有理由不这么做,这是Web开发的一个最佳实践。
原文链接,OSChina.NET翻译
相关文章推荐
- 启用对静态内容的 HTTP (GZIP)压缩 (IIS 7)(源自官方以及搜索整理)
- 在Apache服务器上启用GZip压缩静态内容的方法
- PHP+.htaccess实现全站静态HTML文件GZIP压缩传输(一)
- IIS 7 启用 gzip 静态压缩 压缩js和css文件
- IIS启用GZip压缩JS等extjs静态文件
- IIS启用GZip压缩JS/CSS等静态文件
- 利用HttpWebRequest获取网页内容,由于Gzip压缩导致乱码的情况
- apache启用gzip压缩方法
- 全面解析Linux空间用Gzip对网页内容进行压缩
- 启用IIS的Gzip压缩功能
- 通过Response.Filter属性实现网站内容的动态GZIP压缩
- Apache启用mod_deflate的gzip压缩
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 在IIS上启用Gzip压缩(HTTP压缩)
- 在IIS上启用Gzip压缩 (HTTP压缩)第1/3页
- 启用IIS的Gzip压缩功能
- nginx的一个配置,nginx gzip压缩,nginx静态缓存,nginx负载均衡,nginx限制ip
- IIS上启用Gzip压缩(HTTP压缩) 详解(转自:http://www.jzxue.com/fuwuqi/http-iis-apache/200905/17-2128.html)
- 在IIS上启用Gzip压缩(HTTP压缩)