299

Linux服務(wù)器下Apache軟件如何開(kāi)啟gzip壓縮

時(shí)間: 2015-05-12 10:07:22   點(diǎn)擊數(shù): 32474   來(lái)源: 耐思智慧

Apache上利用Gzip壓縮算法進(jìn)行壓縮的模塊有兩種,分別是mod_gzip 和mod_deflate。要使用Gzip Web壓縮,請(qǐng)首先確定你的服務(wù)器開(kāi)啟了對(duì)這兩個(gè)組件之一的支持。雖然使用Gzip同時(shí)也需要客戶(hù)端瀏覽器的支持,不過(guò)不用擔(dān)心,目前大部分瀏覽器都已經(jīng)支持Gzip了,如IE、Mozilla Firefox、Opera、Chrome等。


如果服務(wù)器開(kāi)啟了對(duì)Gzip組件的支持,那么我們就可以在http.conf或.htaccess里面進(jìn)行定制,下面是一個(gè).htaccess配置的簡(jiǎn)單實(shí)例:

# mod_gzip:
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

# mod_deflate:
<ifmodule mod_deflate.c>
DeflateCompressionLevel 6 #壓縮率, 6是建議值.
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg+xml
</ifmodule>

里面的文件MIME類(lèi)型可以根據(jù)自己情況添加,至于PDF 、圖片、音樂(lè)文檔之類(lèi)的這些本身都已經(jīng)高度壓縮格式,重復(fù)壓縮的作用不大,反而可能會(huì)因?yàn)樵黾覥PU的處理時(shí)間及瀏覽器的渲染問(wèn)題而降低性能,所以就沒(méi)必要再通過(guò)Gzip壓縮。


下面具體說(shuō)下如何開(kāi)啟這個(gè)功能:

1、首先得確認(rèn)apache是否已經(jīng)加載了mod_deflate模塊。

httpd -M

為了開(kāi)啟apache服務(wù)器中的gzip壓縮功能,mod_deflate模塊是必須安裝加載,如下方法:

1.1、進(jìn)入到mod_deflate.c目錄

cd /lamp/httpd-2.2.20/modules/filters

1.2、執(zhí)行編譯加載程序

/usr/local/apache/bin/apxs -i -a -c mod_deflate.c

1.3、重新啟動(dòng)httpd程序

service httpd restart

1.4、查看是否已經(jīng)加載成功

httpd -M

2、編譯配置文件httpd.conf,增加支持gzip的文件類(lèi)型,如下:


<Location />
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE text/html
</Location>


這樣可以壓縮一般網(wǎng)頁(yè)中會(huì)用到的html、xml、php、css、js等格式檔案輸出,雖然會(huì)占用掉服務(wù)器處理器的一點(diǎn)點(diǎn)處理器時(shí)間,瀏覽者在接收 網(wǎng)頁(yè)數(shù)據(jù)時(shí)也會(huì)消耗極短暫的一點(diǎn)點(diǎn)處理器時(shí)間,不過(guò)卻可以大幅減少數(shù)據(jù)傳輸量,減少網(wǎng)絡(luò)帶寬被吃掉的情形。


3、重啟httpd。

service httpd restart

4,檢驗(yàn)是否已經(jīng)開(kāi)啟成功。

IDC資訊:http://www.www.testsystem.com.cn/zt/
上一篇:云主機(jī)或VPS服務(wù)器如何設(shè)置404頁(yè)面 下一篇:IIS服務(wù)器如何啟用Gzip頁(yè)面壓縮

旗下網(wǎng)站:耐思智慧 - 淘域網(wǎng) - 我的400電話 - 中文域名:耐思尼克.cn 耐思尼克.top

耐思智慧 © 版權(quán)所有 Copyright © 2000-2024 www.testsystem.com.cn,Inc. All rights reserved

備案號(hào)碼: 粵ICP備09063828號(hào)  公安備案號(hào): 公安備案 粵公網(wǎng)安備 44049002000123號(hào)  域名注冊(cè)服務(wù)機(jī)構(gòu)許可:粵D3.1-20240003 CN域名代理自深圳萬(wàn)維網(wǎng)

聲明:本網(wǎng)站中所使用到的其他各種版權(quán)內(nèi)容,包括但不限于文章、圖片、視頻、音頻、字體等內(nèi)容版權(quán)歸原作者所有,如權(quán)利所有人發(fā)現(xiàn),請(qǐng)及時(shí)告知,以便我們刪除版權(quán)內(nèi)容

本站程序界面、源代碼受相關(guān)法律保護(hù), 未經(jīng)授權(quán), 嚴(yán)禁使用; 耐思智慧 © 為我公司注冊(cè)商標(biāo), 未經(jīng)授權(quán), 嚴(yán)禁使用

法律顧問(wèn):珠海知名律師 廣東篤行律師事務(wù)所 夏天風(fēng) 律師