Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Compression with in code GZipping?

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
like image 946
Kevin Avatar asked Apr 06 '12 13:04

Kevin


People also ask

How do I enable gzip compression in IIS?

Check the httpCompression TypesCheck the httpCompression section in applicationHost. config file, you should find <add mimeType="application/x-javascript" enabled="true" /> . If this is correct, then it should be correctly configured in your IIS. This will start compressing your .

Does IIS support Brotli?

iisbrotli. dll supports Brotli compression, while iiszlib. dll supports both Gzip and Deflate compression.


1 Answers

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.

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.

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

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

Excerpt from applicationHosts.config file:

<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.

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.

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

like image 134
Kevin P. Rice Avatar answered Sep 30 '22 13:09

Kevin P. Rice