【问题标题】:tools.gzip appears not to compress content in cherrypytools.gzip 似乎不压缩cherrypy中的内容
【发布时间】:2011-09-12 17:02:48
【问题描述】:

我正在使用 Chrome 和 Firefox 下的 Yslow 工具查看我的开发站点,其中一个建议是我压缩了适当的内容。作为一个起点,我刚刚将“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 工作。

提前致谢。

【问题讨论】:

    标签: gzip cherrypy yslow


    【解决方案1】:

    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/*']
    

    【讨论】:

    • 我正在使用 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 正在为我的CSS 文件提供mime 类型text/css,因此mime 类型是正确的。有什么想法我可能会错过吗?如果没有,我将只备份 encoding.py 文件并在其中添加一些调试逻辑来告诉我发生了什么。
    • 我终于有更多时间玩这个了。我不确定我最初在做什么,但我在我的 [/] 配置块中添加了 tools.gzip.on = Truetools.gzip.mime_types = ['text/html', 'text/plain', 'text/css', 'text/javascript']。 YSlow 现在很开心。所有需要压缩的东西都被压缩了。感谢您的帮助。
    • 澄清一下:除了需要tools.gzip.on = True来设置MIME类型。
    • 为了使这项工作适用于 JavaScript,我还必须包含 'application/*'。所以它最终看起来像这样:
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2015-03-21
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多