为了优化网站的访问速度,我们可以通过对静态内容进行压缩,从而减少网页加载的时间,大大节省用户的带宽。在这篇文章中,我将介绍如何使用 Apache 和.htaccess文件进行静态内容压缩。 首先让我介绍一下,我们可以使用两种不同的方法压缩内容: GZip 和 deflate 。 介绍
		
		
		
		    
 
  
 
 
为了优化网站的访问速度,我们可以通过对静态内容进行压缩,从而减少网页加载的时间,大大节省用户的带宽。在这篇文章中,我将介绍如何使用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代码  
  
    
        
            | 01 | <ifmodulemod_deflate.c="">  | 
    
 
    
        
            | 02 |   # force deflate for mangled headers  | 
    
 
    
        
            | 03 |   # developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/  | 
    
 
    
        
            | 04 |   <ifmodulemod_setenvif.c="">  | 
    
 
    
        
            | 05 |     <ifmodulemod_headers.c="">  | 
    
 
    
        
            | 06 |       SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding  | 
    
 
    
        
            | 07 |       RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding  | 
    
 
    
        
            | 11 |   # HTML, TXT, CSS, JavaScript, JSON, XML, HTC:  | 
    
 
    
        
            | 12 |   <ifmodulefilter_module="">  | 
    
 
    
        
            | 13 |     FilterDeclare   COMPRESS  | 
    
 
    
        
            | 14 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/html  | 
    
 
    
        
            | 15 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/css  | 
    
 
    
        
            | 16 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/plain  | 
    
 
    
        
            | 17 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/xml  | 
    
 
    
        
            | 18 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/x-component  | 
    
 
    
        
            | 19 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/javascript  | 
    
 
    
        
            | 20 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/json  | 
    
 
    
        
            | 21 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xml  | 
    
 
    
        
            | 22 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xhtml+xml  | 
    
 
    
        
            | 23 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/rss+xml  | 
    
 
    
        
            | 24 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/atom+xml  | 
    
 
    
        
            | 25 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/vnd.ms-fontobject  | 
    
 
    
        
            | 26 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/svg+xml  | 
    
 
    
        
            | 27 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/x-font-ttf  | 
    
 
    
        
            | 28 |     FilterProvider  COMPRESS  DEFLATE resp=Content-Type $font/opentype  | 
    
 
    
        
            | 30 |     FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no  | 
    
 
    
        
            | 33 |   <ifmodule!mod_filter.c="">  | 
    
 
    
        
            | 34 |     # Legacy versions of Apache  | 
    
 
    
        
            | 35 |     AddOutputFilterByType DEFLATE text/html text/plain text/css application/json  | 
    
 
    
        
            | 36 |     AddOutputFilterByType DEFLATE application/javascript  | 
    
 
    
        
            | 37 |     AddOutputFilterByType DEFLATE text/xml application/xml text/x-component  | 
    
 
    
        
            | 38 |     AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml   | 
    
 
    
        
            | 39 |     AddOutputFilterByType DEFLATE application/atom+xml  | 
    
 
    
        
            | 40 |     AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject   | 
    
 
    
        
            | 41 |     AddOutputFilterByType DEFLATE application/x-font-ttf font/opentype  | 
    
 
 
 
 
 
 
 
将上面的代码放置在你的htaccess文件之后,看看你网站的请求头部。你可以看到一个额外的头“Accept-Encoding“。这意味着请求的客户端能够处理给定的压缩类型的内容,并将提供压缩内容。  
    
        
            | 1 | Accept-Encoding:gzip,deflate,sdch | 
    
 
 
 
 
 
  
结果 
 
  
看看下面的图片,有多少被压缩了。  
  
 
  
 
 
从上面的图片可以看出,实际页面大小707KB,使用压缩后是401KB。因此,它最终会提高你的网站的性能。 
 
 
 
结论  
  
我强烈建议你把网站静态内容做压缩处理,因为没有理由不这么做,这是Web开发的一个最佳实践。  
延伸阅读:告诉你如何关闭GZIP,多种程序方法文件压缩成Gzip格式的类使用mod_gzip加速你的html页面PHP使用zlib扩展实现页面GZIP压缩输出php对gzip文件或者字符串解压实例参考使用PHP和GZip压缩网站JS/CSS文件加速网站访问速度php启用gzip页面压缩示例Linux空间用Gzip对网页内容进行压缩什么是GZIPIIS或Apache启用GZIP压缩优化网站  apache性能优化之启用gzip压缩lighttpd开启gzip压缩Lighttpd1.4.20服务端启用GZIP压缩mod_compress: Lighttpd Gzip Compression To Improve Download and Browsing Speed