HTTP 压缩:某些外部脚本/CSS 有时无法正确解压缩

发布于 2024-09-08 22:33:55 字数 1467 浏览 5 评论 0原文

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

我尝试过同时实现blowery和wicked HttpCompress,但最终得到了相同的结果。这似乎只影响 Firefox,我已经在 Chrome 和 IE 上进行了测试。

发生的情况是我第一次请求该页面时所有外部资源都解压正常。第二次或第三次页面出现错误,因为资源似乎没有解压。我得到如下 unicode 字符:

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

(实际上它们不能在这里正确显示)

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

缓存控制私有

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

内容编码gzip

服务器 Microsoft-IIS/7.5

X-AspNetMvc-版本 2.0

X-AspNet-版本2.0.50727

X-压缩-通过 HttpCompress

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

2010 06:51:40 GMT 内容长度 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"/>

有帮助吗?

I am implementing page/resource compression to improve website performance.

I have tried to implement both blowery and wicked HttpCompress but end up getting the same result. This only seems to affect Firefox, I have tested on Chrome and IE.

What happens is the first time I request the page all the external resources decompress ok. The 2nd or 3rd time the page has errors because the resource doesn't seem to be decompressed. I get unicode characters like:

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

(actually they can't be displayed properly here)

Inspecting the page via firebug displays the response header as:

Cache-Control private

Content-Type text/html; charset=utf-8

Content-Encoding gzip

Server 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, 09 Jul

2010 06:51:40 GMT Content-Length 2622

This clearly states that the resource is compressed by gzip. So something seems to be going wrong on the deflate side on the client?

I have added the following sections (in the appropriate locations) in the 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"/>

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-09-15 22:33:55

这是我以前遇到过的一个问题,问题是内容长度不正确。为什么不正确?因为它可能在压缩之前计算。

如果您手动设置 Content-Lenght,只需将其删除并让模块设置它(如果可以的话)。

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

@ptutt 如果您使用的是共享iis,那么可能已经有所有准备好的压缩集,因此存在一种压缩优于另一种压缩,您只需删除您的压缩即可。如果这是问题,那么内容长度肯定是错误的,因为在第一次压缩后,第二次压缩会破坏它。

使用此网站查看 https://www.giftofspeed.com/ gzip-test/ 如果你的页面已经被 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 网站有时会冻结和/或在页面顶部显示奇怪的文本

This is an issue that I have face before and the problem is that the Content-Length is not correct. Why is not correct ? because its probably calculate before the compression.

If you set Content-Lenght by hand, just remove it and let the module set it if he can.

I note that you use the Blowery compression. Probably this is a bug/issue inside Blowery. If you can not locate it and fix it, why not use the Ms compression ?

@ptutt if you are on shared iis, then maybe there have all ready set compression, so there is one compression over the other, and you only need to remove yours. If this is the issue then for sure the content-lenght is false because after the first compression, the second is break it.

Check it out using this site https://www.giftofspeed.com/gzip-test/ if your pages is all ready compressed by default by iis.

If not compressed by default then you can do it very easy. On 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");
        }       
    }
}

Please note, I just write this code and have not tested. My code is a little more complicate, so I just create a simple verion of it.

Find more examples:
http://www.google.com/search?q=Response.Filter+GZipStream

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文