【问题标题】:IIS7 GZIP Compression - httpCompression sectionIIS7 GZIP 压缩 - httpCompression 部分
【发布时间】:2013-03-18 09:50:52
【问题描述】:

我试图在 IIS7 上配置 httpCompression。通过谷歌搜索,我发现可以使用配置中的httpCompression 部分来制作它。问题是,我无法通过 web.config 使其工作。

当我在applicationHost.config 中进行配置时,一切都按需要进行,但我希望能够为每个应用程序而不是全局进行此配置。

我将applicationHost.config 中的部分定义更改为<section name="httpCompression" overrideModeDefault="Allow" />,并将httpCompression 部分移至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>  

我错过了什么?看起来 IIS 根本没有从 web.config 中读取压缩配置。

每次更改后,我都会让应用程序池回收,所以这不是问题。

【问题讨论】:

    标签: iis-7 web-config gzip


    【解决方案1】:

    根据这个 ServerFault 答案:https://serverfault.com/a/125156/117212 - 您不能在 web.config 中更改 httpCompression,它需要在 applicationHost.config 文件中完成。这是我在 Azure Web 角色中用于修改 applicationHost.config 文件并添加 mime 类型以进行压缩的代码:

    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 来自 NuGet 中的 Microsoft.Web.Administration 包。

    【讨论】:

      【解决方案2】:

      你应该检查整个config file hierarchy

      如果您从 applicationHost 中删除了该部分,您可能会从 machine.config 或父目录的 web.config 继承。

      【讨论】:

      • 我找到了解决方案。我将 applicationHost.config 中的部分定义从
        修改为
        (allowDefinition="Everywhere"解决了问题)。谢谢
      • @AlexDn:我想您应该将您的解决方案作为实际答案发布。
      • @sharptooth 实际上我只能在 IIS 服务器级别上配置它。没有找到每个应用程序配置的解决方案。
      • @AlexDn:无论如何,您的解决方案及其局限性描述在作为答案发布时会更有用。
      猜你喜欢
      • 2012-09-19
      • 2011-02-08
      • 2010-10-21
      • 2011-03-18
      • 2015-07-14
      • 2011-02-12
      • 1970-01-01
      • 2011-10-22
      • 2012-06-24
      相关资源
      最近更新 更多