.net - GZip 压缩在 IIS 7.5 上不起作用

标签 .net asp.net iis gzip iis-7.5

我正在尝试在 IIS 下支持静态文件的 GZip 压缩(默认情况下应该启用,但没有),但到目前为止还没有工作。这是 <system.webServer> 下的部分Web 应用程序的 web.config 文件内的节点;

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

我用谷歌浏览器尝试过。这是请求 header ;

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Cache-Control:no-cache

Connection:keep-alive

Host:my-website-url

Pragma:no-cache

User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30

这些是响应 header ;

Accept-Ranges:bytes

Content-Length:232651

Content-Type:application/x-javascript

Date:Thu, 04 Aug 2011 08:58:19 GMT

ETag:"a69135734a50cc1:0"

Last-Modified:Mon, 01 Aug 2011 12:56:37 GMT

Server:Microsoft-IIS/7.5

X-Powered-By:ASP.NET

我检查了 applicationHost.config 文件并发现了一些如下所示的节点;

----

<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />

----

<section name="urlCompression" overrideModeDefault="Allow" />

----

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

----

<urlCompression />

我在这里缺少什么?

最佳答案

经过大量搜索,我终于找到了在 IIS 7.5 上进行压缩的方法。 首先,除非文件加载得足够频繁,否则 IIS 不会压缩文件。这就提出了一个问题:“IIS 经常考虑什么?”嗯,默认值是每 10 秒 2 次。哎呀!

此设置可以在 web.config 中更改,但需要首先在 applicationHost.config 中解锁该部分。以下是命令:

首先解锁该部分:

C:\Windows\System32\inetsrv\appcmd.exe unlock config /section:system.webServer/serverRuntime

Unlocked section "system.webServer/serverRuntime" at configuration path "MACHINE/WEBROOT/APPHOST".

现在已经完成,编辑 web.config 文件并添加 serverRuntime 元素:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <system.webServer>
      <serverRuntime frequentHitThreshold="1" frequentHitTimePeriod="10:00:00" />
...

在本例中,我将其设置为每 10 小时命中一次文件。您可以根据需要调整这些值。以下是解释 serverRuntime 元素的文档:

http://www.iis.net/configreference/system.webserver/serverruntime

我希望这也有助于您的压缩工作。

注意:您还可以在 applicationHost.config 文件中设置 serverRuntime 元素,但我选择在 web.config 中更改它,因为我们有许多具有不同站点的服务器和场,并且它是我更容易从这个粒度级别控制它。

关于.net - GZip 压缩在 IIS 7.5 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6938713/

相关文章:

.net - 如何安装 XamlPad?

asp.net - <authorization> 节点的 Web.config 错误

c# - 如何获取 SharePoint 2010 Web 应用程序范围的功能以仅在一个 Web 应用程序上激活

IIS 7.5 应用程序初始化(预热)和 HTTPS

c# - 使用 ASP.Net Web 应用程序进行单点登录

powershell - PowerShell无法删除Web服务器(IIS)支持角色?

c# - .NET 中的垃圾收集(代)

.net - 如何让 Visual Studio ToolBox 自动填充项目组件?

c# - 嵌套 UpdatePanel 中的下拉列表

c# - 如何使nodejs代码在asp.net core(带有JavascriptService)应用程序中调用csharp代码?