3

我正在使用 Chrome 和 Firefox 下的 Yslow 工具审查我的开发站点,其中一个建议是我 gzip 适当的内容。作为一个起点,我刚刚将“tools.gzip.on = True”添加到我的 [/] 配置中。我知道配置文件和块被正确解析,因为我还在响应标头中添加了禁用缓存的选项,因为我在开发站点时经常更改文件。我在回复中看到“Expires”和“Pragma: no-cache”标题。

出于某种原因,即使在更改了配置文件(并重新启动进程,这不是绝对必要的)之后,Yslow 仍然报告我没有使用 gzip。我也一直在使用 wget 并没有看到 Content-Encoding 标头。

谁能建议我还能如何验证发生了什么?我想知道问题是cherrypy 忽略了gzip 设置,还是Yslow 只是弄错了事实。我以前从来没有遇到过 Yslow 的问题,所以我倾向于前者。

我要补充一点,Yslow 只报告我的外部 CSS 和 JavaScript 文件(由同一个cherrypy 进程提供服务)需要压缩,即使“wget -S”显示的标头即使在主文件上也不显示 gzip 编码页面本身(这是动态内容)。

我尝试将“tools.gzip.on = True”添加到我的 [/css] 和 [/js] 块中,并且我还尝试在所有相同的块中设置“tools.encode.on = True”,认为也许必须启用编码才能使 gzip 工作。

提前致谢。

4

2 回答 2

10

cherrypy.lib.gzip 的 3.2 文档字符串:

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/*']
于 2011-06-10T23:55:20.963 回答
1

为了使这项工作适用于 Javascript,我还必须将 'application/*' 作为 mime_type 包括在内。

我的配置的相关部分如下所示:

'tools.gzip.on': True,    
'tools.gzip.mime_types': ['text/*', 'application/*'],
于 2016-06-30T00:00:12.850 回答