使用代码GZipping进行IIS压缩?

时间:2022-12-02 19:59:53

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

我正在添加gzipping到我的.net 4站点的所有静态内容和html输出。

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.

我还在IIS 7.5中启用了压缩(静态和动态),我发现在IIS中启用压缩会覆盖我的Vary:Accept-Encoding标头以获取这些资源。

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

所以,我想知道的是,是否真的需要在IIS中启用压缩,因为我已经在解压缩了东西?

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.

因此,基于我的发现,我同意,在代码中执行此操作时,不需要使用IIS压缩。即使内存消耗略高,CPU负载也要低得多,以使代码内压缩更有效地提供文件。

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?

现在,我的全部意义是找出并摆脱IIS覆盖Vary:Accept-Encoding标头。哪个,似乎在启用或不启用IIS压缩时没有效果。标题仍然没有添加...所以,你能帮忙吗?

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():

这是我正在实现的缓存的代码,请注意,在触发包含此代码的方法之前,我通过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

1 个解决方案

#1


5  

You need to specifically know which MIME types you are applying compression to in code. Then you can disable IIS compression for those MIME types.

您需要具体了解在代码中应用压缩的MIME类型。然后,您可以禁用这些MIME类型的IIS压缩。

Your .config file should look approximately like the one below. You will note that the MIME types compression is applied to are all listed. Judiciously turn off compression for MIME types either in your code or in IIS such that the content compressed in code does not get compressed by IIS, and vise-versa.

您的.config文件看起来应该与下面的文件大致相同。您将注意到所有列出的MIME类型压缩都已应用。明智地在代码或IIS中关闭MIME类型的压缩,使得代码中压缩的内容不会被IIS压缩,反之亦然。

For instance, if your HTML is all compressed in code, you could specify:

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

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

Excerpt from applicationHosts.config file:

摘自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>

Please note that if you modify applicationHosts.config it affects ALL of the web sites on your server, so you need to be aware that any web sites that do not apply compression in code will then not be compressed at all.

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

Also note, binary content generally should not be compressed (i.e., images, video). These resources are already compressed in their respective containers (i.e., .JPG, .MP4). Ensure that you aren't compressing content types that are already compressed.

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

I've written more detail in regards to compression settings that you may want to check out in my answer here: https://*.com/a/10051876/733805.

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

#1


5  

You need to specifically know which MIME types you are applying compression to in code. Then you can disable IIS compression for those MIME types.

您需要具体了解在代码中应用压缩的MIME类型。然后,您可以禁用这些MIME类型的IIS压缩。

Your .config file should look approximately like the one below. You will note that the MIME types compression is applied to are all listed. Judiciously turn off compression for MIME types either in your code or in IIS such that the content compressed in code does not get compressed by IIS, and vise-versa.

您的.config文件看起来应该与下面的文件大致相同。您将注意到所有列出的MIME类型压缩都已应用。明智地在代码或IIS中关闭MIME类型的压缩,使得代码中压缩的内容不会被IIS压缩,反之亦然。

For instance, if your HTML is all compressed in code, you could specify:

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

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

Excerpt from applicationHosts.config file:

摘自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>

Please note that if you modify applicationHosts.config it affects ALL of the web sites on your server, so you need to be aware that any web sites that do not apply compression in code will then not be compressed at all.

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

Also note, binary content generally should not be compressed (i.e., images, video). These resources are already compressed in their respective containers (i.e., .JPG, .MP4). Ensure that you aren't compressing content types that are already compressed.

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

I've written more detail in regards to compression settings that you may want to check out in my answer here: https://*.com/a/10051876/733805.

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