【问题标题】:HTTP Compression: Some external scripts/CSS not decompressing properly some of the timeHTTP 压缩:某些外部脚本/CSS 有时无法正确解压缩
【发布时间】:2011-03-13 17:41:30
【问题描述】:

我正在实施页面/资源压缩以提高网站性能。

我曾尝试同时实现吹牛和邪恶的 HttpCompress,但最终得到了相同的结果。这似乎只影响 Firefox,我在 Chrome 和 IE 上测试过。

发生的情况是我第一次请求页面所有外部资源解压正常。第 2 次或第 3 次页面出现错误,因为资源似乎没有被解压缩。我得到 unicode 字符,例如:

������í½`I%&/mÊ{JõJ×àt¡`$Ø@ìÁÍæìiG#)«*ÊeVe]f

(其实这里不能正常显示)

通过 firebug 检查页面显示响应头为:

Cache-Control 私有

内容类型 text/html;字符集=utf-8

内容编码 gzip

服务器 Microsoft-IIS/7.5

X-AspNetMvc-2.0 版

X-AspNet-版本 2.0.50727

X-Compressed-By HttpCompress

X-Powered-By ASP.NET 日期 7 月 9 日星期五

2010 06:51:40 GMT 内容长度 2622

这清楚地表明该资源是由 gzip 压缩的。那么客户端的放气方面似乎出了点问题?

我在 web.config 中添加了以下部分(在适当的位置):

<sectionGroup name="blowery.web">
  <section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup>

<blowery.web>
    <httpCompress preferredAlgorithm="gzip" compressionLevel="high">
      <excludedMimeTypes>
        <add type="image/jpeg"/>
        <add type="image/png"/>
        <add type="image/gif"/>
      </excludedMimeTypes>
      <excludedPaths>
        <add path="NoCompress.aspx"/>
      </excludedPaths>
    </httpCompress>
</blowery.web>

<add name="CompressionModule" type="blowery.Web.HttpCompress.HttpModule, blowery.web.HttpCompress"/>

有什么帮助吗?

【问题讨论】:

    标签: asp.net http-compression


    【解决方案1】:

    这是我之前遇到的一个问题,问题是 Content-Length 不正确。为什么不正确?因为它可能在压缩之前计算。

    如果您手动设置 Content-Lenght,只需将其移除并让模块设置即可。

    我注意到您使用 Blowery 压缩。可能这是 Blowery 内部的错误/问题。如果找不到并修复它,为什么不使用Ms压缩?

    @ptutt 如果您在共享 iis 上,那么可能有所有已准备好的压缩集,所以有一个压缩比另一个压缩,您只需要删除您的压缩。如果这是问题所在,那么内容长度肯定是错误的,因为在第一次压缩之后,第二次会破坏它。

    检查一下,如果您的页面已默认被 iis 压缩,请使用此站点 https://www.giftofspeed.com/gzip-test/

    如果默认不压缩,那么你可以很容易地做到这一点。在 Global.asax 上

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        string cTheFile = HttpContext.Current.Request.Path;
        string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
        
        if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
        {
            string acceptEncoding = MyCurrentContent.Request.Headers["Accept-Encoding"].ToLower();;
            
            if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
            {
                // defalte
                HttpContext.Current.Response.Filter = new DeflateStream(prevUncompressedStream,
                    CompressionMode.Compress);
                HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
            } else if (acceptEncoding.Contains("gzip"))
            {
                // gzip
                HttpContext.Current.Response.Filter = new GZipStream(prevUncompressedStream,
                    CompressionMode.Compress);
                HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
            }       
        }
    }
    

    请注意,我只是写了这段代码,还没有测试过。我的代码有点复杂,所以我只创建了一个简单的版本。

    查找更多示例: http://www.google.com/search?q=Response.Filter+GZipStream

    参考: ASP.NET site sometimes freezing up and/or showing odd text at top of the page while loading, on load balanced servers

    【讨论】:

    • 我使用“wicked HttpCompress”得到了完全相同的结果。我不知道 MS 压缩...这是部署在共享服务器上,所以我无权访问 IIS。我的猜测是他们故意不打开压缩,以免 cpu 过载。考虑到它在共享服务器上,我将获得多少好处可能值得商榷。考虑到两种压缩工具都有相同的问题,我认为我做错了。也许它是一个 IIS 配置?我只在本地测试过。
    • 刚刚实现了这段代码,它就可以工作了。有了这么简单的东西,你一定想知道为什么 Blowery 和 httpcompress 有相同的错误(也许它们使用相同的代码库?)。无论如何,感谢您的帮助。
    • @ptutt 错误是我已经告诉你的,标题的长度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2012-02-12
    • 2017-05-02
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多