IIS7 GZIP Compression - httpCompression section

2019-03-31 13:07发布

问题:

I trying to configure httpCompression on IIS7. By googling, I found that it can be made using httpCompression section in config. The problem, that I can't make it work from web.config.

When I make the configuration in applicationHost.config everything works as needed, but I want to be able to make this configuration per application and not globally.

I changed section definition in applicationHost.config to <section name="httpCompression" overrideModeDefault="Allow" /> and moved httpCompression section to web.config:

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
      <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/x-javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/json; charset=utf-8" enabled="true" />
        <add mimeType="*/*" enabled="false" />
      </dynamicTypes>
    </httpCompression>  

What am I missing? It looks like IIS not reads compression configurations from web.config at all.

After each change, I make application pool recycle, so it not a problem.

回答1:

You should check the whole config file hierarchy.

If you removed the section from applicationHost you may be inheriting from machine.config or a web.config of a parent directory.



回答2:

as per this ServerFault answer: https://serverfault.com/a/125156/117212 - you can't change httpCompression in web.config, it needs to be done in applicationHost.config file. Here is the code I use in my Azure web role to modify applicationHost.config file and add mime types for compression:

using (var serverManager = new ServerManager())
{
    var config = serverManager.GetApplicationHostConfiguration();
    var httpCompressionSection = config.GetSection("system.webServer/httpCompression");
    var dynamicTypesCollection = httpCompressionSection.GetCollection("dynamicTypes");

    Action<string> fnCheckAndAddIfMissing = mimeType =>
    {
        if (dynamicTypesCollection.Any(x =>
        {
            var v = x.GetAttributeValue("mimeType");
            if (v != null && v.ToString() == mimeType)
            {
                return true;
            }

            return false;
        }) == false)
        {
            ConfigurationElement addElement = dynamicTypesCollection.CreateElement("add");
            addElement["mimeType"] = mimeType;
            addElement["enabled"] = true;
            dynamicTypesCollection.AddAt(0, addElement);
        }
    };

    fnCheckAndAddIfMissing("application/json");
    fnCheckAndAddIfMissing("application/json; charset=utf-8");

    serverManager.CommitChanges();
}

ServerManager comes from Microsoft.Web.Administration package in NuGet.