Azure 网站提供静态 JS/CSS 但不是二进制的速度慢

Azure Website slow to serve static JS/CSS but not binary

我有一个 Azure Website/Web 应用程序,它提供静态 JS 和 CSS 文件的速度非常慢,但提供二进制文件似乎非常好。

为了测试这个问题,我上传了两个 30MB 的文件,一个 big.js 和另一个 big.rar。如果幸运的话,JS 文件的下载速度约为 100KB/s。 RAR 文件的下载速度约为 4,000KB/s。结果非常一致。

我已经在 Fiddler 中检查过,并且在这两种情况下都进行了 gzip 压缩。正如预期的那样,JS 文件以 MIME 类型 application/x-javascript 发送,而 RAR 文件以 application/octet-stream.

我很难理解这一点 - 为什么 IIS 提供一种类型的静态内容比另一种慢得多?

我们遇到了这个问题,并且能够在 Azure 支持团队的帮助下解决这个问题。问题是慢速文件会使用 TransferEncoding: Chuncked。他们建议我们强制静态压缩来解决这个问题。

我们必须将以下内容添加到 <system.webServer>

<serverRuntime enabled="true"  frequentHitThreshold="1"  frequentHitTimePeriod="00:00:20" />

看起来这是 IIS 8.5 上的问题,而不仅仅是 Azure 特定的问题。

现在应用程序服务 upgrade to Windows Server 2016 looks complete 并且不需要此解决方法。

详细阐述John Tseng的回答:(来自here

As you saw earlier, IIS 7 caches the compressed versions of static files. So, if a request arrives for a static file whose compressed version is already in the cache, it doesn’t need to be compressed again.

But what if there is no compressed version in the cache? Will IIS 7 then compress the file right away and put it in the cache? The answer is yes, but only if the file is being requested frequently. By not compressing files that are only requested infrequently, IIS 7 saves CPU usage and cache space.

By default, a file is considered to be requested frequently if it is requested two or more times per 10 seconds.

因此,向您的用户提供 javascript 文件的未压缩版本的原因是因为它没有达到压缩的默认阈值;换句话说,javascript 文件在 10 秒内没有被请求 2 次。

要控制这一点,我们必须更改 <serverRuntime> 元素上的一个属性,该属性控制压缩:frequentHitThreshold。为了在请求一次时压缩文件,请将 <serverRuntime> 元素更改为如下所示:

<serverRuntime enabled="true" frequentHitThreshold="1" />

如果您有许多 javascript 文件正在提供并且您的用户经常使用,这将稍微影响您的 CPU 性能,但如果您的用户足够频繁以影响 [=24] =] 从压缩这些文件开始,那么它们已经被压缩和缓存了!