2011-06-10 20 views
3

內容,我回顧我的開發本站Chrome和Firefox下的YSlow的工具,其中一條建議是,我gzip壓縮適當的內容。作爲一個起點,我剛剛在我的[/]配置中添加了「tools.gzip.on = True」。我知道正在解析配置文件和塊,因爲我還添加了選項來禁用響應頭中的緩存,因爲我在開發網站時經常更改文件。我在回覆中看到「Expires」和「Pragma:no-cache」標題。tools.gzip似乎沒有壓縮在CherryPy的

出於某種原因,甚至更改配置文件(和重新啓動的過程中,這不是絕對必要的)後,仍然YSlow的報道,我沒有使用gzip。我也一直在使用wget,並且沒有看到Content-Encoding標頭。

有人能想出是怎麼回事,我可能驗證這是怎麼回事?我想知道這個問題是否是無視gzip設置的cherrypy,或者Yslow只是弄錯了它的事實。我以前從未遇到過伊斯洛的麻煩,所以我傾向於前者。

我會補充說Yslow只報告我的外部CSS和JavaScript文件(由相同的cherrypy進程提供)需要壓縮,即使「wget -S」顯示的頭不顯示gzip編碼即使在主頁面上(這是動態內容)。

我試圖加入「tools.gzip.on =真正的」我的[/ ​​CSS]和[/ JS]塊,我也試着設置「tools.encode.on =真」在所有的相同的塊,想到編碼必須啓用gzip工作。

在此先感謝。

回答

9

3.2文檔字符串爲cherrypy.lib.gzip:

def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False): 
    """Try to gzip the response body if Content-Type in mime_types. 

    cherrypy.response.headers['Content-Type'] must be set to one of the 
    values in the mime_types arg before calling this function. 

    The provided list of mime-types must be of one of the following form: 
     * type/subtype 
     * type/* 
     * type/*+subtype 

    No compression is performed if any of the following hold: 
     * The client sends no Accept-Encoding request header 
     * No 'gzip' or 'x-gzip' is present in the Accept-Encoding header 
     * No 'gzip' or 'x-gzip' with a qvalue > 0 is present 
     * The 'identity' value is given with a qvalue > 0. 

    """ 

我的錢是在MIME類型,因爲你提到的JS和CSS。可以說正是如此改變:

[/static] 
tools.gzip.mime_types: ['text/html', 'text/plain', 'text/javascript', 'text/css'] 

在CherryPy的3.2+,可以縮短,爲:

[/static] 
tools.gzip.mime_types: ['text/*'] 
+0

我使用的CherryPy 3.1.2,附帶的Fedora 14,我已經添加到了我的[/ ​​CSS]配置塊構建: 'tools.gzip.on = True tools.gzip.mime_types = ['text/html','text/plain','text/css'] 即使在重新啓動過程後,yslow仍然報告我的CSS文件沒有被壓縮。 「wget -S」確認cherrypy正在使用MIME類型文本/ css來提供我的CSS文件,所以MIME類型是正確的。 任何想法,我可能會失蹤?如果不是的話,我只需要對encoding.py文件進行備份,然後在其中引入一些調試邏輯,以顯示發生了什麼。 – 2011-06-11 18:05:10

+1

我終於有更多時間玩這個了。我不確定最初是在做什麼,但我添加了'tools.gzip.on = True'和'tools.gzip.mime_types = ['text/html','text/plain','text/css ','text/javascript']'到我的[/]配置區塊。 YSlow現在很開心。所有需要壓縮的內容都被壓縮。謝謝你的幫助。 – 2011-06-18 23:32:16

+0

只是爲了闡明:在* addition *中需要'tools.gzip.on = True'來設置MIME類型。 – nikow 2011-11-03 15:25:17

1

爲了使這項工作爲Javascript我必須還包括「應用/ *」作爲MIME_TYPE 。

我的配置的相關部分看起來是這樣的:

'tools.gzip.on': True,  
'tools.gzip.mime_types': ['text/*', 'application/*'],