7

I am adding in gzipping to all my static content, and html outputs from my .net 4 site.

I also have compression enabled in IIS 7.5 (both static and dynamic), and what I am finding is that enabling compression in IIS, overwrites my Vary: Accept-Encoding header for these resources.

So, what I am wondering is, is there really a need to enable compression in IIS, since I already am gzipping things?

So, I've done a some testing, and what I found follows:

Utilizing IIS Static and Dynamic Compression, with code compression:
CPU Load: 35%
Memory Load: 28M

Utilizing IIS Static and Dynamic Compression, without code compression:
CPU Load: 34%
Memory Load: 28M

Non-Utilizing Static and Dynamic Compression In IIS, with code compression:
CPU Load: 14%
Memory Load: 32M

So, based on my findings, I agree, there is no need to utilize IIS compression, when doing this in code. Even though memory consumption is a bit higher, CPU Load is significantly enough lower to make the in-code compression much more efficient for serving the files.

Now, really my whole point of this was to find out and get rid of the IIS overwriting of the Vary: Accept-Encoding header. Which, it seems to have no effect when IIS compression is enabled or not. The header still does not get added... so, can you help with that?

Here is the code for the caching that I am implementing, please note that prior to firing the method containing this code, I am clearing the headers via, context.Response.ClearHeaders():

    With context.Response
        .AddHeader("Cache-Control", "store, cache")
        .AddHeader("Pragma", "cache")
        .AddHeader("Cache-Control", "max-age=21600")
        .AddHeader("ETag", Date.Now.Ticks)
        .AddHeader("Expires", DateTime.Now.AddYears(1).ToString("ddd, dd MMM yyyy hh:mm:ss") + " GMT")
        .AddHeader("Vary", "Accept-Encoding")
        .AppendHeader("Vary", "Accept-Encoding")
        .Cache.SetVaryByCustom("Accept-Encoding")
        .Cache.SetOmitVaryStar(True)
        .Cache.VaryByParams.IgnoreParams = True
        .Cache.SetAllowResponseInBrowserHistory(True)
        .Cache.SetCacheability(Web.HttpCacheability.Public)
        .Cache.SetValidUntilExpires(True)
        .Cache.SetLastModified(DateTime.Now.AddYears(-1).ToString("ddd, dd MMM yyyy hh:mm:ss") + " GMT")
        .CacheControl = "public" '
        .Expires = 24 * 60 * 366
        .ExpiresAbsolute = DateTime.Now.AddYears(1).ToString("ddd, dd MMM yyyy hh:mm:ss") + " GMT"
    End With
4

1 回答 1

5

您需要明确知道要在代码中应用压缩的 MIME 类型。然后,您可以为这些 MIME 类型禁用 IIS 压缩。

您的.config文件应类似于以下文件。您会注意到应用压缩的 MIME 类型已全部列出。明智地在您的代码或 IIS 中关闭 MIME 类型的压缩,以便代码中压缩的内容不会被 IIS 压缩,反之亦然。

例如,如果您的 HTML 全部压缩在代码中,您可以指定:

    <add mimeType="text/html" enabled="false" />

文件摘录applicationHosts.config

<system.webServer>
<httpCompression
    directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
    dynamicCompressionDisableCpuUsage="90"
    dynamicCompressionEnableCpuUsage="80"
    maxDiskSpaceUsage="100" minFileSizeForComp="2700"
    noCompressionForRange="true"
    sendCacheHeaders="false"
    staticCompressionDisableCpuUsage="100"
    staticCompressionEnableCpuUsage="80"
    >
    <scheme name="gzip"
        dll="%Windir%\system32\inetsrv\gzip.dll"
        dynamicCompressionLevel=”4”
        staticCompressionLevel=”7” />
    <dynamicTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/rss+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="application/xml" enabled="true" />
        <add mimeType="image/svg+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>
<urlCompression doDynamicCompression="true"
    dynamicCompressionBeforeCache=”true” />
</system.webServer>

请注意,如果您修改applicationHosts.config它会影响您服务器上的所有网站,因此您需要注意,任何未在代码中应用压缩的网站都不会被压缩。

另请注意,通常不应压缩二进制内容(即图像、视频)。这些资源已经被压缩在它们各自的容器中(即.JPG、.MP4)。确保您没有压缩已压缩的内容类型。

我已经写了有关压缩设置的更多详细信息,您可能想在我的答案中查看:https ://stackoverflow.com/a/10051876/733805 。

于 2012-04-07T03:09:33.960 回答