简体   繁体   English

如何在IIS7中运行gzip压缩?

[英]How can I get gzip compression in IIS7 working?

I have installed Static and dynamic compression for IIS7, as well as setting the two web.config values at my application Virtual Folder level. 我已经为IIS7安装了静态和动态压缩,并在我的应用程序Virtual Folder级别设置了两个web.config值。 As I understand it, I don't need to enable compression at the server, or site level anymore, and I can manage it on a per folder basis using my web.config file. 据我了解,我不需要在服务器或站点级别启用压缩,我可以使用我的web.config文件在每个文件夹的基础上管理它。

I have two settings in my .config file that I have set to customize gzip for my app: 我的.config文件中有两个设置,我已设置为我的应用程序自定义gzip:

<httpCompression dynamicCompressionDisableCpuUsage="90"
    dynamicCompressionEnableCpuUsage="0">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <dynamicTypes>
    <remove mimeType="*/*"/>
    <add mimeType="*/*" enabled="true" />
  </dynamicTypes>
</httpCompression>
<urlCompression doDynamicCompression="true"
    dynamicCompressionBeforeCache="true" />

However, when I run the application, I can clearly see that gzip is not used, because my page sizes are the same. 但是,当我运行应用程序时,我可以清楚地看到没有使用gzip,因为我的页面大小是相同的。 I am also using YSlow for FireFox, which also confirms that my pages are not being gziped. 我也在使用YSlow for FireFox,这也证实我的页面没有被gziped。

What am I missing here? 我在这里错过了什么? In IIS6 it was a simple matter of specifying the file types, and setting the compression level between 0-10. 在IIS6中,只需指定文件类型,并将压缩级别设置在0-10之间即可。 I don't see the need documented to specify the file types or compression level, since the defaults seem to cover the file types, and I'm not seeing the level anywhere. 我没有记录需要记录指定文件类型或压缩级别,因为默认值似乎涵盖了文件类型,我没有在任何地方看到级别。

There was a thread on forums.iis.net about this during the iis 7 beta. 在iis 7 beta期间,forums.iis.net上有一个关于此的帖子。 Turned out the guy didn't have the modules installed, but it sounds like you've ruled that out from your opening sentence. 原来这个家伙没有安装模块,但听起来你已经从你的开头句中排除了这一点。

Microsofts key advice for him was to enable failed request tracing to find out what was going wrong. 微软对他的主要建议是启用失败的请求跟踪以找出出错的地方。 This is possibly one of the most under-appreciated features of IIS7, but certainly one of the most powerful. 这可能是IIS7最不受重视的功能之一,但肯定是最强大的功能之一。

  • Open IIS Manager. 打开IIS管理器。
  • Go to your site, and on the actions pane (the very far right), click 'Failed Request Tracing...' under the 'Configure' section. 转到您的站点,在操作窗格(最右侧)上,单击“配置”部分下的“失败的请求跟踪...”。
  • Click 'enable'. 点击“启用”。
  • Then, in the features view, click 'Failed request tracing rules'. 然后,在功能视图中,单击“失败的请求跟踪规则”。 Click add, next, enter 200 for the status code, next, click finish. 单击“添加”,然后输入200作为状态代码,接下来单击“完成”。

If you don't see "Failed Request Tracing" in the actions pane, you'll need to add the feature to the server - either using the "Add Role Services" wizard (Health and Diagnostics\\Tracing) or through the Web Platform Installer (Products\\Server\\IIS: Tracing), and then close and re-open IIS Manager. 如果在操作窗格中未看到“失败的请求跟踪”,则需要将该功能添加到服务器 - 使用“添加角色服务”向导(运行状况和诊断\\跟踪)或通过Web平台安装程序(Products \\ Server \\ IIS:Tracing),然后关闭并重新打开IIS管理器。

Next, rerun your test. 接下来,重新运行测试。 This will generate some log info for us to examine. 这将生成一些日志信息供我们检查。

Look in c:\\inetpub\\logs\\FailedReqLogFiles\\w3svcx. 查看c:\\ inetpub \\ logs \\ FailedReqLogFiles \\ w3svcx。 You will see a bunch of files named fr000xx.xml. 您将看到一堆名为fr000xx.xml的文件。 Open up any one of them in your browser. 在浏览器中打开其中任何一个。 (By the way, if you copy these files anywhere, make sure freb.xsl is there. Also, don't delete freb.xsl - if you do, just delete the whole directory or copy it from another location, as IIS only creates it once per folder.) (顺便说一句,如果你在任何地方复制这些文件,请确保freb.xsl在那里。另外,不要删除freb.xsl - 如果你这样做,只需删除整个目录或从其他位置复制它,因为IIS只创建每个文件夹一次。)

Click the 'request details' tab and select 'complete request trace'. 单击“请求详细信息”选项卡,然后选择“完成请求跟踪”。 Search the page for 'compress' - you should find it in several areas; 在页面中搜索“压缩” - 你应该在几个方面找到它; once for static content, and once for dynamic content. 一次用于静态内容,一次用于动态内容。

If you don't find either of them, IIS isn't configured correctly. 如果找不到其中任何一个,则说明IIS配置不正确。 If you do find them, you should see them followed by a compression_success and a compression_do. 如果找到它们,您应该看到它们后跟一个compression_success和一个compression_do。 Success is self explanatory; 成功是自我解释的; the 'do' indicates what it did - in my case, it showed "OriginalSize 1462784 CompressedSize 179482" 'do'表示它做了什么 - 就我而言,它显示了“OriginalSize 1462784 CompressedSize 179482”

Since yours isn't working, hopefully you will see something different that helps you solve the problem. 由于你的工作不起作用,希望你能看到一些不同的东西来帮助你解决问题。

Make sure you turn this off when you're done by disabling failed request tracing in the actions pane for your website. 通过在网站的操作窗格中禁用失败的请求跟踪,确保在完成后关闭此功能。

We had a similar problem and it turns out that IIS7 does some dynamic CPU based throttling here.. 我们遇到了类似的问题,结果发现IIS7在这里做了一些基于动态CPU的限制。

http://www.iis.net/ConfigReference/system.webServer/httpCompression http://www.iis.net/ConfigReference/system.webServer/httpCompression

dynamicCompressionDisableCpuUsage

Optional uint attribute. 可选的uint属性。

Specifies the percentage of CPU utilization at which dynamic compression will be disabled. 指定将禁用动态压缩的CPU利用率百分比。

Note: This attribute acts as an upper CPU limit at which dynamic compression is turned off. 注意:此属性用作关闭动态压缩的CPU上限。 When CPU utilization falls below the value specified in the dynamicCompressionEnableCpuUsage attribute, dynamic compression will be reenabled. 当CPU利用率低于dynamicCompressionEnableCpuUsage属性中指定的值时,将重新启用动态压缩。

The default value is 90. 默认值为90。


dynamicCompressionEnableCpuUsage dynamicCompressionEnableCpuUsage

Optional uint attribute. 可选的uint属性。

Specifies the percentage of CPU utilization below which dynamic compression will be enabled. 指定CPU利用率的百分比,低于该百分比将启用动态压缩。 The value must be between 0 and 100. Average CPU utilization is calculated every 30 seconds. 该值必须介于0和100之间。平均CPU利用率每30秒计算一次。

Note: This attribute acts as a lower CPU limit below which dynamic compression is turned on. 注意:此属性用作较低的CPU限制,低于该限制时将启用动态压缩。 When CPU utilization rises above the value specified in the dynamicCompressionDisableCpuUsage attribute, dynamic compression will be disabled. 当CPU利用率高于dynamicCompressionDisableCpuUsage属性中指定的值时,将禁用动态压缩。

The default value is 50. 默认值为50。

Note the defaults -- if your IIS7 hits 90% CPU usage, it will disable all dynamic gzipped content until CPU usage dips back below 50%! 请注意默认值 - 如果您的IIS7达到90%的CPU使用率,它将禁用所有动态gzip压缩内容,直到CPU使用率回落到50%以下!

Also, some great recommendations and benchmarks here on the real CPU cost of GZIP. 此外,还有一些关于GZIP实际CPU成本的重要建议和基准。

http://weblogs.asp.net/owscott/archive/2009/02/22/iis-7-compression-good-bad-how-much.aspx http://weblogs.asp.net/owscott/archive/2009/02/22/iis-7-compression-good-bad-how-much.aspx

Long story short, unless you regularly have dynamic pages well in excess of 200kb, it's a non-issue. 长话短说,除非你经常有超过200kb的动态页面,否则这不是问题。

Following the excellent advice of JohnW, I too enabled logging to find the culprit, though the reason for the failure turned out to be different: 根据JohnW的出色建议,我也让日志记录找到了罪魁祸首,尽管失败的原因是不同的:

STATIC_COMPRESSION_NOT_SUCCESS 
Reason 14 
Reason NOT_FREQUENTLY_HIT

In short, it appears that if you don't hit the page frequently enough then IIS7 will not deem it worthy of compressing, which seems a little bit odd to me. 简而言之,似乎如果你没有频繁地点击页面,那么IIS7将不会认为它值得压缩,这对我来说似乎有点奇怪。 Nonetheless, makes sense in this case because I was just trying to test it on a local machine. 尽管如此,在这种情况下有意义,因为我只是想在本地机器上测试它。

According to this page , the default appears to be that a page has to be hit 2 times within 10 seconds to be a "frequent hit". 根据这个页面 ,默认似乎是一个页面必须在10秒内被击中2次才能成为“频繁命中”。 If you really want to, you can override the default in applicationHost.config (%systemroot%\\Windows\\System32\\inetsrv\\config). 如果您真的想要,可以覆盖applicationHost.config中的默认值(%systemroot%\\ Windows \\ System32 \\ inetsrv \\ config)。 At least for me it's a locked attribute, so you won't be able to override it in your own web.config. 至少对我来说这是一个锁定属性,所以你将无法在自己的web.config中覆盖它。

<serverRuntime frequentHitThreshold="1" />

Also, I note now that SO already had this answer here: In IIS7, gzipped files do not stay that way . 另外,我现在注意到SO已经有了这个答案: 在IIS7中,gzipped文件不会保持这种状态

我通过在添加/删除程序中安装动态压缩来解决我的问题。

In the system.webServer section of your Web.config file, add the following lines: 在Web.config文件的system.webServer部分中,添加以下行:

<remove fileExtension=".js" />  
<mimeMap fileExtension=".js" mimeType="application/x-javascript" />  

The compression scheme in IIS7 is enabled by default, but it maps only a single javascript mime type to be compressed, application/x-javascript. IIS7中的压缩方案默认启用,但它只映射一个要压缩的javascript mime类型,application / x-javascript。 Adding the line above tells IIS to give all your .js files that mime type, which in turn makes the compression work. 添加上面的行告诉IIS为你的所有.js文件提供mime类型,这反过来使压缩工作。

turn on static compression. 打开静态压缩。 dynamic compression is for dynamic pages like asp, php, aspx, etc. 动态压缩适用于动态页面,如asp,php,aspx等。

Here's a link to the IIS config reference for compression : 这是压缩IIS配置参考的链接:

For me it turned out to be the setting 对我来说,结果证明了这一点

noCompressionForProxies

as we are on a proxy here...took myself off proxy and voila, compression. 因为我们在这里代理...把自己从代理和瞧,压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM