299

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

時間: 2015-05-12 10:07:22   點擊數(shù): 32473   來源: 耐思智慧

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


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

# 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類型可以根據(jù)自己情況添加,至于PDF 、圖片、音樂文檔之類的這些本身都已經(jīng)高度壓縮格式,重復(fù)壓縮的作用不大,反而可能會因為增加CPU的處理時間及瀏覽器的渲染問題而降低性能,所以就沒必要再通過Gzip壓縮。


下面具體說下如何開啟這個功能:

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

httpd -M

為了開啟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、重新啟動httpd程序

service httpd restart

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

httpd -M

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


<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)頁中會用到的html、xml、php、css、js等格式檔案輸出,雖然會占用掉服務(wù)器處理器的一點點處理器時間,瀏覽者在接收 網(wǎng)頁數(shù)據(jù)時也會消耗極短暫的一點點處理器時間,不過卻可以大幅減少數(shù)據(jù)傳輸量,減少網(wǎng)絡(luò)帶寬被吃掉的情形。


3、重啟httpd。

service httpd restart

4,檢驗是否已經(jīng)開啟成功。

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

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

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

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

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

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

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