在Azure App Service上启用gzip压缩

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Azure App Service上启用gzip压缩相关的知识,希望对你有一定的参考价值。

我有一个在microsoft azure中托管的Web应用程序。由于本地IIS对静态和动态内容都使用压缩,我希望这也适用于azure平台。因为似乎压缩不起作用,因为例如json和css文件是未压缩的返回:

Request header

Response header

我试图设置压缩,如几个帖子(例如gzip compression in Windows Azure Websites或)中提到的那样,而不对结果进行任何更改:

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

因为看起来天蓝色的门户网站没有给我任何改变压缩的选项。

我需要做什么来启用压缩,或者只有在天蓝色中使用Vserver时才可以这样做?

答案

您可以在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 forum

以上是关于在Azure App Service上启用gzip压缩的主要内容,如果未能解决你的问题,请参考以下文章