HTTP压缩:某些外部脚本/ CSS在某些时候没有正确解压缩

时间:2010-07-09 07:12:15

标签: asp.net http-compression

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

我试图实现既狂欢又邪恶的HttpCompress,但最终得到了相同的结果。这似乎只影响Firefox,我已经在Chrome和IE上测试过。

当我第一次请求页面所有外部资源解压缩时,会发生什么。第二次或第三次页面出错,因为资源似乎没有解压缩。我得到像unicode字符:

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

(实际上这里无法正常显示)

通过firebug检查页面将响应标题显示为:

  

Cache-Control private

     

Content-Type text / html;字符集= UTF-8

     

Content-Encoding gzip

     

服务器Microsoft-IIS / 7.5

     

X-AspNetMvc-Version 2.0

     

X-AspNet-Version 2.0.50727

     

X-Compressed-By HttpCompress

     

X-Powered-By ASP.NET Date Fri,7月9日

     

2010 06:51:40 GMT Content-Length 2622

这清楚地表明资源是通过gzip压缩的。所以客户端的deflate方面似乎出现了问题?

我在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"/>

任何帮助?

1 个答案:

答案 0 :(得分:1)

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

如果您手动设置Content-Lenght,只需删除它,然后让模块设置它。

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

@ptutt 如果您使用的是共享iis,那么可能已经准备好了设置压缩,因此对另一个进行压缩,您只需删除自己的压缩。如果这是问题,那么肯定内容长度是假的,因为在第一次压缩之后,第二次是打破它。

检查使用此网站http://www.pipeboost.com/report.asp,如果您的网页已默认由iis压缩。

如果没有默认压缩,那么你可以很容易地做到这一点。在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