【问题标题】:IIS 7 dynamic content compression not workingIIS 7 动态内容压缩不起作用
【发布时间】:2013-10-25 07:49:28
【问题描述】:

我的 IIS 7 动态内容压缩将无法通过服务器日志验证...发送/接收的字节与打开和关闭压缩相同。

让我回顾一下我到目前为止所做的事情,以确保正确完成:

1) 安装动态压缩模块(duh)
2) 启用动态压缩
3) 在 system.webserver/httpCompression 下的 web.config 中,我添加了 DynamicCompressionDisableCpuUsage=100 和 DynamicCompressionEnableCpuUsage=99 以确保尽可能频繁地启用压缩。服务器负载通常是 0% 到 2% 的 CPU,所以这应该不是问题。
4) 我将 system.webserver/httpCompression/scheme dynamicCompressionLevel 从 0 更改为 7,因为默认值为 0
5) 我添加了 mime 类型并在 system.webserver/httpCompression/dynamicTypes 下设置了 enabled=true 并通过请求分析器确保 mimetype 确实正确
6)在此之后,我什至重新启动了站点/回收的应用程序池。
7) 我什至添加了 mime-types 以包含字符集,我读过的地方有时会影响动态压缩。

我的流量仍然没有减少!是什么赋予了!?我什至将 system.webserver/httpCompression/minFileSizeForComp 设置为 1000B,尽管这仅用于静态压缩,我认为它可能会以某种方式延续到动态压缩。日志中发送的字节数与未开启压缩时相同。

这是我的 web.config 部分仅供参考:

<system.webServer>
    <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" dynamicCompressionDisableCpuUsage="100" dynamicCompressionEnableCpuUsage="99" minFileSizeForComp="1000">
        <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" dynamicCompressionLevel="7" staticCompressionLevel="7"/>
        <dynamicTypes>
            <add mimeType="text/*" enabled="true"/>
            <add mimeType="message/*" enabled="true"/>
            <add mimeType="application/javascript" enabled="true"/>
            <add mimeType="application/x-javascript" enabled="true"/>
            <add mimeType="application/xml" enabled="true"/>
            <add mimeType="application/json" enabled="true"/>
            <add mimeType="application/json; charset=utf-8" enabled="true"/>
            <add mimeType="application/json; charset=UTF-8" enabled="true"/>
            <add mimeType="*/*" enabled="false"/>
        </dynamicTypes>
    </httpCompression>
    <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>

我参考了其他几个问题来提出这些设置...似乎我已经尝试了书中的所有技巧。

How can I get gzip compression in IIS7 working?
https://serverfault.com/questions/200041/how-do-determine-the-dynamiccompressiondisablecpuusage-setting-on-iis7

【问题讨论】:

    标签: iis iis-7 web-config


    【解决方案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 包。

    【讨论】:

    • 我没有使用它,因为我早在您发布之前就已经找到了答案,但您的答案是正确的。我只是在 IIS 管理器中编辑了设置,而不是像您那样以编程方式进行设置,但我不明白为什么这也不起作用。干杯!
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2011-04-14
    • 1970-01-01
    • 2015-05-22
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多