在Azure应用服务上启用gzip压缩

14

我有一个托管在微软Azure中的Web应用程序。由于本地IIS对静态和动态内容都使用压缩,所以我期望这也适用于Azure平台。但似乎压缩并未生效,例如json和css文件返回时未经压缩:

请求头

响应头

我已尝试按照几篇文章中提到的设置压缩方法(例如Windows Azure网站上的gzip压缩),但结果没有任何变化:

<system.webServer>
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
  <httpCompression>
    <dynamicTypes>
    <clear />
    <add enabled="true" mimeType="text/*"/>
    <add enabled="true" mimeType="message/*"/>
    <add enabled="true" mimeType="application/x-javascript"/>
    <add enabled="true" mimeType="application/javascript"/>
    <add enabled="true" mimeType="application/json"/>
    <add enabled="false" mimeType="*/*"/>
    <add enabled="true" mimeType="application/atom+xml"/>
    <add enabled="true" mimeType="application/atom+xml;charset=utf-8"/>
  </dynamicTypes>
  <staticTypes>
    <clear />
    <add enabled="true" mimeType="text/*"/>
    <add enabled="true" mimeType="message/*"/>
    <add enabled="true" mimeType="application/javascript"/>
    <add enabled="true" mimeType="application/atom+xml"/>
    <add enabled="true" mimeType="application/xaml+xml"/>
    <add enabled="true" mimeType="application/json"/>
    <add enabled="false" mimeType="*/*"/>
  </staticTypes>
 </httpCompression>
[...]
</system.webServer>

看起来Azure门户没有给我更改压缩的选项。

我需要做什么才能启用压缩,或者只有在使用Azure中的Vserver时才可能实现?

1个回答

13
你可以在 web.config 中进行更改:
<system.webServer>
  <urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

那么:

<httpCompression>
  <dynamicTypes>
    <clear />
    <add enabled="true"  mimeType="text/*"/>
    <add enabled="true"  mimeType="message/*"/>
    <add enabled="true"  mimeType="application/x-javascript"/>
    <add enabled="true"  mimeType="application/javascript"/>
    <add enabled="true"  mimeType="application/json"/>
    <add enabled="false" mimeType="*/*"/>
    <add enabled="true"  mimeType="application/atom+xml"/>
    <add enabled="true"  mimeType="application/atom+xml;charset=utf-8"/>
  </dynamicTypes>
  <staticTypes>
     <clear />
     <add enabled="true" mimeType="text/*"/>
     <add enabled="true" mimeType="message/*"/>
     <add enabled="true" mimeType="application/javascript"/>
     <add enabled="true" mimeType="application/atom+xml"/>
     <add enabled="true" mimeType="application/xaml+xml"/>
     <add enabled="true" mimeType="application/json"/>
     <add enabled="false" mimeType="*/*"/>
   </staticTypes>
 </httpCompression>

来源:Microsoft 论坛


嗨Peter,我更改了web.config文件(更新了我的帖子),使用你提供的示例。我检查了结果,发现压缩没有起作用。在system.webServer标签中添加httpCompression标签是正确的吗? - edesr
1
以上信息是正确的,然而对我来说它也不起作用,你有没有让它工作过? - Darren
6
我在使用与你相同配置的 Azure Web App 上遇到了相同的问题,但它似乎没有起作用。我将其放置一段时间后刷新页面,然后它就起作用了。我猜可能是某个地方出现了缓存问题。此外,我还从 statictypes 部分中删除了应用程序/JSON MIME 类型。我不希望服务器向我提供缓存版本,因为它们大多数情况下都是从数据库中获取的控制器操作结果。您可以查看此链接以获取更多信息 https://www.iis.net/configreference/system.webserver/httpcompression - code-assassin
我也遇到了同样的问题,目前还没有解决方案。 - Pablo Jomer
@LyubomirIvanovValchev 我认为你应该查看这个链接: https://piotrbach.com/enabling-dynamic-gzip-compression-azure-web-app/ - wtct
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接