简体   繁体   English

对于经典应用程序池,Gzip压缩在IIS7.5中不起作用

[英]Gzip compression doesn't work in IIS7.5 for classic application pool

I am trying to figure out why one site using classic application pool won't get compressed. 我试图弄清楚为什么使用经典应用程序池的一个站点不会被压缩。 This application pool has 32bit enabled. 此应用程序池已启用32位。 I have the following settings in applicationHost.config. 我在applicationHost.config中有以下设置。

  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
        <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"  dynamicCompressionLevel="4" staticCompressionLevel="9"/>
        <scheme name="deflate" dll="%Windir%\system32\inetsrv\gzip.dll"  dynamicCompressionLevel="4" staticCompressionLevel="9"/>

        <staticTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/x-javascript" enabled="true" />
            <add mimeType="application/atom+xml" enabled="true" />
            <add mimeType="application/xaml+xml" enabled="true" />
            <add mimeType="*/*" enabled="false" />
        </staticTypes>
        <dynamicTypes>
          <add mimeType="text/*" enabled="true" />
          <add mimeType="message/*" enabled="true" />
          <add mimeType="application/javascript" enabled="true" />
          <add mimeType="*/*" enabled="false" />
       </dynamicTypes>
    </httpCompression>

And in global.asax file Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs) 并在global.asax文件Sub Application_PreRequestHandlerExecute(ByVal sender As Object,ByVal e As EventArgs)

    Dim app As HttpApplication = TryCast(sender, HttpApplication)
    Dim acceptEncoding As String = app.Request.Headers("Accept-Encoding")
    Dim prevUncompressedStream As Stream = app.Response.Filter

    If app.Context.CurrentHandler Is Nothing Then
        Return
    End If

    If Not (TypeOf app.Context.CurrentHandler Is System.Web.UI.Page) OrElse app.Context.CurrentHandler.[GetType]().Name = "SyncSessionlessHandler" OrElse app.Request("HTTP_X_MICROSOFTAJAX") IsNot Nothing Then
        Return
    End If

    If acceptEncoding Is Nothing OrElse acceptEncoding.Length = 0 Then
        Return
    End If

    acceptEncoding = acceptEncoding.ToLower()

    If acceptEncoding.Contains("gzip") Then
        ' gzip
        app.Response.Filter = New GZipStream(prevUncompressedStream, CompressionMode.Compress)
        app.Response.AppendHeader("Content-Encoding", "gzip")
    ElseIf acceptEncoding.Contains("deflate") OrElse acceptEncoding = "*" Then
        ' deflate
        app.Response.Filter = New DeflateStream(prevUncompressedStream, CompressionMode.Compress)
        app.Response.AppendHeader("Content-Encoding", "deflate")
    End If
End Sub

I doubled checked that I have dynamicCompression feature installed. 我加倍检查我是否安装了dynamicCompression功能。 In the final output Content-Encoding header was removed all the time. 在最终输出中,Content-Encoding标头一直被删除。 What else should I look at? 我还应该看看什么? I read that some functions won't work for classic application pool. 我读到一些函数不适用于经典应用程序池。 Is this causing the problem? 这会导致问题吗?

IIS 7 improves internal compression functionality dramatically making it much easier than previous versions to take advantage of compression that's built-in to the Web server. IIS 7显着改进了内部压缩功能,使其比以前的版本更容易利用内置于Web服务器的压缩。 IIS 7 also supports dynamic compression which allows automatic compression of content created in your own applications (ASP.NET or otherwise!). IIS 7还支持动态压缩,允许自动压缩在您自己的应用程序(ASP.NET或其他!)中创建的内容。 The scheme is based on content-type sniffing and so it works with any kind of Web application framework. 该方案基于内容类型嗅探,因此它适用于任何类型的Web应用程序框架。

While static compression on IIS 7 is super easy to set up and turned on by default for most text content ( text/* , which includes HTML and CSS, as well as for JavaScript, Atom, XAML, XML), setting up dynamic compression is a bit more involved, mostly because the various default compression settings are set in multiple places down the IIS –> ASP.NET hierarchy. 虽然对于大多数文本内容( text/* ,包括HTML和CSS,以及JavaScript,Atom,XAML,XML),IIS 7上的静态压缩非常容易设置和打开,但设置动态压缩是更多涉及,主要是因为各种默认压缩设置在IIS - > ASP.NET层次结构中的多个位置设置。

Let's take a look at each of the two approaches available: 我们来看看两种方法中的每种方法:

  • Static Compression 静态压缩
    Compresses static content from the hard disk. 压缩硬盘中的静态内容。 IIS can cache this content by compressing the file once and storing the compressed file on disk and serving the compressed alias whenever static content is requested and it hasn't changed. IIS可以通过压缩文件一次并将压缩文件存储在磁盘上并在请求静态内容且未更改时为压缩别名提供服务来缓存此内容。 The overhead for this is minimal and should be aggressively enabled. 这方面的开销很小,应该积极地启用。
  • Dynamic Compression 动态压缩
    Works against application generated output from applications like your ASP.NET apps. 适用于ASP.NET应用程序等应用程序的应用程序生成输出。 Unlike static content, dynamic content must be compressed every time a page that requests it regenerates its content. 与静态内容不同,每当请求它的页面重新生成其内容时,都必须压缩动态内容。 As such dynamic compression has a much bigger impact than static caching. 因此,动态压缩比静态缓存具有更大的影响。

How Compression is configured 如何配置压缩

Compression in IIS 7.x is configured with two .config file elements in the <system.WebServer> space. IIS 7.x中的压缩在<system.WebServer>空间中配置了两个.config文件元素。 The elements can be set anywhere in the IIS/ASP.NET configuration pipeline all the way from ApplicationHost.config down to the local web.config file. 可以在IIS / ASP.NET配置管道中的任何位置设置元素,从ApplicationHost.config一直到本地web.config文件。 The following is from the the default setting in ApplicationHost.config (in the %windir%\\System32\\inetsrv\\config folder) on IIS 7.5 with a couple of small adjustments (added json output and enabled dynamic compression): 以下是来自IIS 7.5上的ApplicationHost.config(在%windir%\\System32\\inetsrv\\config文件夹中)的默认设置,并进行了一些小的调整(添加了json输出并启用了动态压缩):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>

    <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
      <dynamicTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="*/*" enabled="false" />
      </dynamicTypes>
      <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
      </staticTypes>
    </httpCompression>

    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

  </system.webServer>
</configuration>

You can find documentation on the keys here respectively: 您可以在此处找到关于密钥的文档:

The httpCompression Element – What and How to compress httpCompression元素 - 什么以及如何压缩

Basically httpCompression configures what types to compress and how to compress them. 基本上, httpCompression配置要压缩的类型以及如何压缩它们。 It specifies the DLL that handles gzip encoding and the types of documents that are to be compressed. 它指定处理gzip编码的DLL以及要压缩的文档类型。 Types are set up based on mime-types which looks at returned Content-Type headers in HTTP responses. 类型是基于mime类型设置的,它们查看HTTP响应中返回的Content-Type标头。 For example, I added the application/json to mime type to my dynamic compression types above to allow that content to be compressed as well since I have quite a bit of AJAX content that gets sent to the client. 例如,我将mime类型的application / json添加到上面的动态压缩类型中,以允许压缩内容,因为我有相当多的AJAX内容被发送到客户端。

The urlCompression Element – Enables and Disables Compression urlCompression元素 - 启用和禁用压缩

The urlCompression element is a quick way to turn compression on and off. urlCompression元素是打开和关闭压缩的快速方法。 By default static compression is enabled server wide, and dynamic compression is disabled server wide. 默认情况下,服务器范围内启用静态压缩,并在服务器范围内禁用动态压缩。 This might be a bit confusing because the httpCompression element also has a doDynamicCompression attribute which is set to true by default, but the urlCompression attribute by the same name actually overrides it. 这可能有点令人困惑,因为httpCompression元素还具有doDynamicCompression属性,默认情况下该属性设置为true,但同名的urlCompression属性实际上覆盖了它。

The urlCompression element only has three attributes: doStaticCompression , doDynamicCompression , and dynamicCompressionBeforeCache . urlCompression元素只有三个属性: doStaticCompressiondoDynamicCompressiondynamicCompressionBeforeCache The doCompression attributes are the final determining factor whether compression is enabled, so it's a good idea to be explcit! doCompression属性是否是启用压缩的最终决定因素,因此最好是explcit! The default for doDynamicCompression='false” , but doStaticCompression="true" ! doDynamicCompression='false”的默认doDynamicCompression='false” ,但是doStaticCompression="true" Static Compression is enabled by Default, Dynamic Compression is not 默认情况下启用静态压缩,而不启用动态压缩

Because static compression is very efficient in IIS 7 it's enabled by default server wide and there probably is no reason to ever change that setting. 由于静态压缩在IIS 7中非常有效,因此默认情况下在服务器范围内启用静态压缩,因此可能没有理由更改该设置。 Dynamic compression however, since it's more resource intensive, is turned off by default. 然而,动态压缩由于资源密集程度较高,因此默认情况下处于关闭状态。 If you want to enable dynamic compression there are a few quirks you have to deal with, namely that enabling it in ApplicationHost.config doesn't work. 如果要启用动态压缩,则必须处理一些怪癖,即在ApplicationHost.config中启用它不起作用。 Setting: 设置:

<urlCompression doDynamicCompression="true" />

in applicationhost.config appears to have no effect and I had to move this element into my local web.config to make dynamic compression work. 在applicationhost.config中似乎没有任何效果,我不得不将此元素移动到我的本地web.config中以使动态压缩工作。 This is actually a smart choice because you're not likely to want dynamic compression in every application on a server. 这实际上是一个明智的选择,因为您不太可能希望在服务器上的每个应用程序中进行动态压缩。 Rather dynamic compression should be applied selectively where it makes sense. 相反,动态压缩应该在有意义的地方有选择地应用。 However, nowhere is it documented that the setting in applicationhost.config doesn't work (or more likely is overridden somewhere and disabled lower in the configuration hierarchy). 但是,无法记录applicationhost.config中的设置不起作用(或者更有可能在某处覆盖并在配置层次结构中禁用更低)。

So: Remember to set doDynamicCompression=”true” in web.config!!! 所以: 记得在web.config中设置doDynamicCompression=”true”

How Static Compression works 静态压缩如何工作

Static compression works against static content loaded from files on disk. 静态压缩适用于从磁盘上的文件加载的静态内容。 Because this content is static and not bound to change frequently – such as .js , .css and static HTML content – it's fairly easy for IIS to compress and then cache the compressed content. 因为这个内容是静态的并且不会经常更改 - 例如.js.css和静态HTML内容 - 所以IIS很容易压缩然后缓存压缩内容。 The way this works is that IIS compresses the files into a special folder on the server's hard disk and then reads the content from this location if already compressed content is requested and the underlying file resource has not changed. 这种方式的工作方式是IIS将文件压缩到服务器硬盘上的特殊文件夹中,然后如果已请求压缩内容并且基础文件资源未更改,则从该位置读取内容。 The semantics of serving an already compressed file are very efficient – IIS still checks for file changes, but otherwise just serves the already compressed file from the compression folder. 提供已经压缩的文件的语义非常有效 - IIS仍然会检查文件更改,但是只是从压缩文件夹中提供已经压缩的文件。

The compression folder is located at: 压缩文件夹位于:
%windir%\\inetpub\\temp\\IIS Temporary Compressed Files\\ApplicationPool\\

If you look into the subfolders you'll find compressed files: 如果查看子文件夹,您将找到压缩文件:

CompressedFileFolder CompressedFileFolder

These files are pre-compressed and IIS serves them directly to the client until the underlying files are changed. 这些文件是预先压缩的,IIS直接将它们提供给客户端,直到更改基础文件。

As I mentioned before – static compression is on by default and there's very little reason to turn that functionality off as it is efficient and just works out of the box. 正如我之前提到的 - 默认情况下静态压缩是打开的,并且没有理由关闭该功能,因为它是有效的并且只是开箱即用。 The one tweak you might want to do is to set the compression level to maximum. 您可能想要做的一个调整是将压缩级别设置为最大值。 Since IIS only compresses content very infrequently it would make sense to apply maximum compression. 由于IIS仅很少压缩内容,因此应用最大压缩是有意义的。 You can do this with the staticCompressionLevel setting on the scheme element: 您可以使用scheme元素上的staticCompressionLevel设置执行此操作:

<scheme name="gzip" dll="%Windir%\\system32\\inetsrv\\gzip.dll" staticCompressionLevel="9" />

Other than that the default settings are probably just fine. 除此之外,默认设置可能还不错。

Dynamic Compression – not so fast! 动态压缩 - 不是那么快!

By default dynamic compression is disabled and that's actually quite sensible – you should use dynamic compression very carefully and think about what content you want to compress. 默认情况下,动态压缩被禁用,这实际上非常合理 - 您应该非常小心地使用动态压缩,并考虑要压缩的内容。 In most applications it wouldn't make sense to compress all generated content as it would generate a significant amount of overhead. 在大多数应用程序中,压缩所有生成的内容是没有意义的,因为它会产生大量的开销。 Scott Fortsyth has a great post that details some of the performance numbers and how much impact dynamic compression has. Scott Fortsyth有一篇很棒的文章,详细介绍了一些性能数字以及动态压缩的影响程度。 Depending on how busy your server is you can play around with compression and see what impact it has on your server's performance. 根据服务器的繁忙程度,您可以使用压缩功能,看看它对服务器性能的影响。

There are also a few settings you can tweak to minimize the overhead of dynamic compression. 您还可以调整一些设置,以最大限度地减少动态压缩的开销。 Specifically the httpCompression key has a couple of CPU related keys that can help minimize the impact of Dynamic Compression on a busy server: 具体来说,httpCompression键有几个与CPU相关的键,可以帮助最小化动态压缩对繁忙服务器的影响:

  • dynamicCompressionDisableCpuUsage
  • dynamicCompressionEnableCpuUsage

By default these are set to 90 and 50 which means that when the CPU hits 90% compression will be disabled until CPU utilization drops back down to 50%. 默认情况下,它们设置为9050 ,这意味着当CPU达到90%时,压缩将被禁用,直到CPU利用率下降到50%。 Again this is actually quite sensible as it utilizes CPU power from compression when available and falling off when the threshold has been hit. 同样,这实际上是非常明智的,因为它在可用时利用来自压缩的CPU功率,并且在达到阈值时下降。 It's a good way some of that extra CPU power on your big servers to use when utilization is low. 在利用率较低时,大型服务器上的一些额外CPU功率是一种很好的方式。 Again these settings are something you likely have to play with. 这些设置也是你可能需要玩的东西。 I would probably set the upper limit a little lower than 90% maybe around 70% to make this a feature that kicks in only if there's lots of power to spare. 我可能会将上限设置为略低于90%,可能约为70%,这使得这项功能只有在有足够的电量时才能启动。 I'm not really sure how accurate these CPU readings that IIS uses are as Cpu usage on Web Servers can spike drastically even during low loads. 我不确定IIS使用的这些CPU读数有多精确,因为即使在低负载期间,Web服务器上的CPU使用率也会急剧上升。 Don't trust settings – do some load testing or monitor your server in a live environment to see what values make sense for your environment. 不要信任设置 - 在实时环境中进行一些负载测试或监控服务器,以查看哪些值对您的环境有意义。

Finally for dynamic compression I tend to add one Mime type for JSON data, since a lot of my applications send large chunks of JSON data over the wire. 最后,对于动态压缩,我倾向于为JSON数据添加一个Mime类型,因为我的很多应用程序通过线路发送大块JSON数据。 You can do that with the application/json content type: 您可以使用application / json内容类型执行此操作:

<add mimeType="application/json" enabled="true" />

What about Deflate Compression? 减压压缩怎么样?

The default compression is GZip. 默认压缩是GZip。 The documentation hints that you can use a different compression scheme and mentions Deflate compression. 该文档提示您可以使用不同的压缩方案,并提及Deflate压缩。 And sure enough you can change the compression settings to: 当然,您可以将压缩设置更改为:

<scheme name="deflate" dll="%Windir%\\system32\\inetsrv\\gzip.dll" staticCompressionLevel="9" />

to get deflate style compression. 获得deflate样式压缩。 The deflate algorithm produces slightly more compact output so I tend to prefer it over GZip but more HTTP clients (other than browsers) support GZip than Deflate so be careful with this option if you build Web APIs. deflate算法产生稍微更紧凑的输出,所以我倾向于优先于GZip,但是更多HTTP客户端(除了浏览器)支持GZip而不是Deflate,因此如果构建Web API,请小心使用此选项。

I also had some issues with the above value actually being applied right away. 我也有一些问题,上面的值实际上正在应用。 Changing the scheme in applicationhost.config didn't show up on the site right away. 在applicationhost.config中更改方案并没有立即显示在该站点上。 It required me to do a full IISReset to get that change to show up before I saw the change over to deflate compressed content. 它要求我做一个完整的IISReset,以便在我看到更改以缩小压缩内容之前显示该更改。 Content was slightly more compressed with deflate – not sure if it's worth the slightly less common compression type, but the option at least is available. 内容稍微压缩一点 - 不确定它是否值得稍微不那么常见的压缩类型,但至少可以选择。

IIS 7 finally makes GZip Easy IIS 7终于制作了GZip Easy

In summary IIS 7 makes GZip easy finally, even if the configuration settings are a bit obtuse and the documentation is seriously lacking. 总之,IIS 7最终使GZip变得简单,即使配置设置有点迟钝且文档严重缺乏。 But once you know the basic settings I've described here and the fact that you can override all of this in your local web.config it's pretty straight forward to configure GZip support and tweak it exactly to your needs. 但是一旦你知道我在这里描述的基本设置以及你可以在本地web.config中覆盖所有这些的事实,就可以直接配置GZip支持并根据你的需要进行调整。

Static compression is a total no brainer as it adds very little overhead compared to direct static file serving and provides solid compression. 静态压缩总是毫无疑问,因为与直接静态文件服务相比,它增加了非常少的开销,并提供了可靠的压缩。 Dynamic Compression is a little more tricky as it does add some overhead to servers, so it probably will require some tweaking to get the right balance of CPU load vs. compression ratios. 动态压缩有点棘手,因为它会给服务器增加一些开销,因此可能需要进行一些调整才能在CPU负载与压缩比之间取得适当的平衡。 Looking at large sites like Amazon, Yahoo, NewEgg etc. – they all use 看看亚马逊,雅虎,NewEgg等大型网站 - 他们都在使用


Reference : 参考:

http://weblog.west-wind.com/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x http://weblog.west-wind.com/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM