Compression in IIS 8.5 not successful, stating ALREADY_CONTENT_ENCODING
Asked Answered
T

2

3

I am trying to debug the issue of why my pages are not being GZIP'ed or deflated according to YSLOW. I ended up enabling Failed Request Logs on the server and was able to see the failed reason of why it is not compressing, it thinks it is already compressed.

DYNAMIC_COMPRESSION_NOT_SUCCESS Reason="ALREADY_CONTENT_ENCODING"

I have enabled dynamic and static compression in IIS, I have also changed the web.config file to include the following.

<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"/>

In addition on my aspx page I have a method I call before every page (on page load) to run gzip compression (this might be why it is causing the error).

This is how I call the method from the page load

//compress page
Compression.GZipEncodePage();

And this is the method that compresses the page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InitialDataEntry
{
    public static class Compression
    {
        /// <summary>
        /// Sets up the current page or handler to use GZip through a Response.Filter
        /// IMPORTANT:  
        /// You have to call this method before any output is generated!
        /// </summary>
        public static void GZipEncodePage()
        {
            HttpResponse Response = HttpContext.Current.Response;
        if (IsGZipSupported())
        {
            string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            if (AcceptEncoding.Contains("deflate"))
            {
                Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
                                           System.IO.Compression.CompressionMode.Compress);
                Response.AppendHeader("Content-Encoding", "deflate");
            }
            else
            {
                Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
                                          System.IO.Compression.CompressionMode.Compress);
                Response.AppendHeader("Content-Encoding", "gzip");
            }
        }

        // Allow proxy servers to cache encoded and unencoded versions separately
        Response.AppendHeader("Vary", "Content-Encoding");
    }

    /// <summary>
    /// Determines if GZip is supported
    /// </summary>
    /// <returns></returns>
    public static bool IsGZipSupported()
    {
        string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
        string IsPartial = HttpContext.Current.Request.Headers["x-microsoftajax"];

        if (!string.IsNullOrEmpty(AcceptEncoding) &&
             AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))

            //Just checking to see if this is a partial page update
            if (string.Compare("Delta=true", IsPartial, true) == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        else
        {
            return false;
        }
    }
}
}

This used to work by the way, but not sure when it stopped, however my users realized the issue as the pages got bloated in size and what used to be 500k is now 2mb!

Any help would be greatly appreciated.

Thanks,

Tyndareus answered 19/4, 2016 at 18:52 Comment(0)
S
3

We had a similar issue on Windows 2012 R2 server; following below steps solved our problem:

  1. Select an IIS site, and go to Configuration Editor.
  2. Select "system.web/caching/outputCache" section, then set the "omitVaryStar" property to "true"
  3. Select system.webServer, and then select httpCompression.
  4. set "staticCompressionIgnoreHitFrequency" property to "true" enter image description here
  5. iisreset
Sherrillsherrington answered 5/5, 2016 at 17:39 Comment(3)
I cannot find system.webserver --> httpCompression, did the rest but still didn't work.Tyndareus
Added a picture for the second parameterThrombin
found it under the server instead of the site name, however even after making those changes and restarting IIS no success :(Tyndareus
T
2

There is no need to manually implement the gzip and instead you can relay on IIS to do the job for you. If your application is using app pool in Integrated Mode then make sure the httpcompression tag is inside

<System.WebServer> tag

Also along with the httpCompression tag you need to have

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

Thanks it. Remove the compression class and check the Response headers now

Tammietammuz answered 3/5, 2016 at 11:31 Comment(1)
I did verify the app pool is using integrated mode, in addition we do have httpcompression as well as the urlcompression true for static and dynamic commpression. I tried running a demo site without using the gzip class we implemented and still no compression. It's funny that I see compression working when I run the website on that local server however as soon as anyone tries to see it from outside the server it is no longer compressed.Tyndareus

© 2022 - 2024 — McMap. All rights reserved.