使用mod_deflate模塊
優點:可以壓縮頁面,優化傳輸速度,節省帶寬。
缺點:會額外消耗CPU,同時可能有些較老的瀏覽器不支持壓縮。
NOTE:
適用於適合壓縮的資源,例如文本文件。
配置說明:
1、首先需要啓用mod_deflate模塊,CentOS6中的httpd.conf配置文件裏是默認啓用了mod_deflate模塊的。
如果被禁用,可以在http.conf中進行修改,去掉註釋即可。
2、調用DEFLATE輸出過濾器
設置過濾器,並向過濾器添加明確說明哪些是能通過過濾,允許使用壓縮功能的文件類型
SetOutputFilter DEFLATE # mod_deflate configuration # Restrict compression to there MIME types AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javasrcipt AddOutputFilterByType DEFLATE text/css # Level of compression (Highest 9 - lowest 1) DeflateCompressionLevel 9 //設定壓縮比例,數值越高,壓縮比例越大,耗時越慢 # Netscape 4.x has some problems. BrowserMatch ^Mozilla/4 gzip-only-text/html //基於正則表達式對瀏覽器類型和版本做匹配篩選 # Netscape 4.06-4.08 have some more problems BrowserMatch ^mozilla/4\.0[678] no-gzip # MSIE masquerades as Netsacpe,but it is fine BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
3、配置完成後,可以使用curl命令測試配置效果:
使用-I選項顯示相應報文的首部信息
使用--compressed選項要求服務器端將報文壓縮後發送
可以看到Content-Encoding:gzip
說明配置生效
寫的比較潦草,如有遺漏錯誤和爭議之處,歡迎大家的批評指正和討論,謝謝。