2011年2月18日 星期五

設定 apache2 和 django 壓縮輸出內容

Google Page Speed 分析後, 發現 javascript 和 css 可以壓個七八成, 實在滿可觀的, 就試著來設壓縮。官網 mod_deflate 看看就會設了, 下面的範例是壓縮 css 和 javascript:
AddOutputFilterByType DEFLATE text/css text/javascript application/javascript application/x-javascript
可以查 MIME 列表, 看要壓那些類型的檔案。或是先設全部都有, 再加條件不壓圖。

比較擾人的是, 早期的一些瀏覽器不能正確處理壓縮文件, 官網教學說要這麼設:
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
說明如下:
The third BrowserMatch directive fixes the guessed identity of the user agent, because the Microsoft Internet Explorer identifies itself also as "Mozilla/4" but is actually able to handle requested compression. Therefore we match against the additional string "MSIE" (\b means "word boundary") in the User-Agent Header and turn off the restrictions defined before.

剛看始看看不懂, 後來看到 Internet Explorer User Agent Strings 才知道 IE 會自稱 Mozilla/4, 但它可以處理壓縮, 所以要再加第三行取消前兩行的限制。How To: Optimize Your Apache Site with Mod Deflate
文末提到 IE6 聽說有問題, 但他測不出來, 所以他沒有另外關掉 IE6。

Django 的話, 可以讓 apache2 協助處理, 加個 text/html, 或是加入 GZipMiddleWare, 都很簡單。

測試資料是否有壓縮有很多方法, 最保險的是用 Firefox Live HTTP Headers 看回傳結果有沒有 gzip。不會看 header 的話, 可以透過 Page Speed 或 GIDZipTest 測看看。

沒有留言:

張貼留言

在 Fedora 下裝 id-utils

Fedora 似乎因為執行檔撞名,而沒有提供 id-utils 的套件 ,但這是使用 gj 的必要套件,只好自己編。從官網抓好 tarball ,解開來編譯 (./configure && make)就是了。 但編譯後會遇到錯誤: ./stdio.h:10...