Apache的mod_deflate模組安裝配置步驟詳解

來源:互聯網
上載者:User

最近把部落格從虛擬機器主機搬到 VPS 上,自然一番折騰。估計圍繞這一過程,寫三四篇部落格不是夢。
這是第一篇,伺服器端的壓縮功能 – 伺服器在返回內容前先對內容做 gzip 壓縮,以減小傳輸的檔案大小 – 照 Google 的說法,能減小 90%,但這也不是重點,重點是伺服器端不開啟 gzip 壓縮的話,Google PageSpeed 的測試就會扣分 – 我個人特別在意這個分數。
Apache 下,壓縮功能由 mod_deflate 模組控制。
安裝#

我的 VPS 系統裝的是 openSUSE 13.1 64 位元系統,Apache 版本為 2.4。首先查看下系統中是否已經安裝 mod_deflate模組,我知道的有兩種方法:
目前使用者下 執行命令 httpd2 -M,輸出的內容大致如下:

 代碼如下 複製代碼
chenxsan@zfanw.com:~> httpd2 -M
[Fri Oct 31 13:13:59.278203 2014] [so:warn] [pid 9292] AH01574: module deflate_module is already loaded, skipping
Loaded Modules:
core_module (static)
access_compat_module (static)
so_module (static)
http_module (static)
mpm_prefork_module (static)
unixd_module (static)
systemd_module (static)
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
dir_module (shared)
env_module (shared)
expires_module (shared)
include_module (shared)
log_config_module (shared)
mime_module (shared)
negotiation_module (shared)
setenvif_module (shared)
ssl_module (shared)
userdir_module (shared)
reqtimeout_module (shared)
authn_core_module (shared)
authz_core_module (shared)
php5_module (shared)
rewrite_module (shared)
deflate_module (shared)
查看 /etc/sysconfig/apache2 檔案內容,可以直接開啟檔案,也可以使用 grep 命令:
grep "APACHE_MODULES=" /etc/sysconfig/apache2
得出的結果大致如下:
APACHE_MODULES=”actions alias auth_basic authn_file authz_host authz_groupfile authz_user autoindex cgi dir env expires include log_config mime negotiation setenvif ssl userdir reqtimeout authn_core authz_core mod-userdir php5 mod_rewrite mod_deflate deflate”

我這裡顯示的結果是已經安裝載入了 mod_deflate 模組。
假如沒有,則使用 a2enmod 來啟用:

 代碼如下 複製代碼
sudo a2enmod deflate

等等,為什麼沒說安裝直接進入啟用階段?因為 mod_deflate 模組在安裝 Apache 時已經捎帶裝上,所以可以跳過安裝這個步驟。
啟用 mod_deflate 模組後,需要重啟 Apache 伺服器:

 代碼如下 複製代碼
sudo rcapache2 restart

啟用#

如上所述。
配置#

啟用 mod_deflate 模組後,就可以開始配置了。
可以照 openSUSE 的操作說明一步步來,也可以粗野直接點,修改 /etc/apache2/httpd.conf 檔案,在檔案末加入如下代碼:

 代碼如下 複製代碼

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \
    \.(?:exe|t?gz|zip|bz2|sit|rar|7z)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

或者可以考慮 h5bp 提供的配置。
然後重啟 Apache:

 代碼如下 複製代碼
sudo rcapache2 restart

再跑一趟 PageSpeed,就不會再提伺服器壓縮的事 – 恭喜,你已經壓縮掉 90% 的傳輸檔案大小,為使用者節省大量頻寬與時間。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.