mod_deflate模塊提供了DEFLATE輸出過濾器,允許服務器在將輸出內容發送到客戶端以前進行壓縮,以節約帶寬了,下面我們來介紹在linux中mod_deflate模塊安裝配置.
最近把博客從虛擬主機搬到 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
啟用#如上所述m配置#,啟用 mod_deflate 模塊后m就可以開始配置了,可以照 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 --Vevb.com
或者可以考慮 h5bp 提供的配置.
然后重啟 Apache:sudo rcapache2 restart 再跑一趟 PageSpeed,就不會再提服務器壓縮的事 – 恭喜,你已經壓縮掉 90% 的傳輸文件大小,為用戶節省大量帶寬與時間.
新聞熱點
疑難解答