為了優(yōu)化網(wǎng)站的訪問速度,我們可以通過對靜態(tài)內(nèi)容進(jìn)行壓縮,從而減少網(wǎng)頁加載的時(shí)間,大大節(jié)省用戶的帶寬。在這篇文章中,我將介紹如何使用Apache和.htaccess文件進(jìn)行靜態(tài)內(nèi)容壓縮。
首先讓我介紹一下,我們可以使用兩種不同的方法壓縮內(nèi)容:GZip 和 deflate。
介紹
GZip方法在早期的apache版本中使用(在Apache 1.3之前)。但在那之后apache引入了deflate方法,相比GZip并沒有太大的效果(但仍是非常好的)。然而,GZip在apache 1.3之后不再提供更多的支持。因此,你的Apache版本必須大于1.3,如果沒有,你必須升級到最新版本的Apache。
在使用壓縮之前,你必須啟用apache的mod_deflate模塊。要啟用這個(gè)模塊,你只需要從httpd.conf文件去掉這個(gè)模塊行。
啟用這個(gè)模塊后,你的服務(wù)器準(zhǔn)備好提供壓縮的內(nèi)容。但是,服務(wù)器只有當(dāng)它接收到來自客戶端的相應(yīng)頭文件時(shí),才會(huì)創(chuàng)建壓縮內(nèi)容。所以,現(xiàn)在你需要將下面的代碼放置到你網(wǎng)站的htaccess文件,才能通知服務(wù)器提供壓縮的內(nèi)容。
.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文件之后,看看你網(wǎng)站的請求頭部。你可以看到一個(gè)額外的頭“Accept-Encoding“。這意味著請求的客戶端能夠處理給定的壓縮類型的內(nèi)容,并將提供壓縮內(nèi)容。
Accept-Encoding:gzip,deflate,sdch
結(jié)果
看看下面的圖片,有多少被壓縮了。
結(jié)論
我強(qiáng)烈建議你把網(wǎng)站靜態(tài)內(nèi)容做壓縮處理,因?yàn)闆]有理由不這么做,這是Web開發(fā)的一個(gè)最佳實(shí)踐。