GZip压缩不适用于Azure App Service上托管的ASP.NET Core 2.2应用程序

时间:2019-09-25 12:02:40

标签: asp.net-core azure-web-sites asp.net-core-2.2

我有一个正在Azure应用服务上运行的ASP.NET Core 2.2应用程序。

我遇到的问题是来自应用程序的响应没有被压缩(没有 Content-Encoding 标头)。

我对App Service的理解是,我的应用程序将在IIS后面用完进程,因此IIS应该是负责压缩响应的事物。我的理解也是,默认情况下,App Service上的IIS应该配置为使用gzip自动压缩JavaScript和CSS之类的文件类型,但这不会发生。

发送到服务器的请求都具有正确的 accept-encoding 标头,因此浏览器没有问题(我也使用多种浏览器进行过检查)。

我尝试在web.config中显式设置urlCompression:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <urlCompression doStaticCompression="true" doDynamicCompression="true" />
    </system.webServer>
  </location>
</configuration>

但这没什么区别。

我还尝试将IIS.Compression.SiteExtension安装到App Service,但这也没有启用压缩。

我知道我可以直接在应用程序中使用Response Compression Middleware,但我希望由IIS处理。

因此,简而言之,我的问题是如何在App Service中为我的应用程序使用压缩功能(GZIP或Brotli)?

1 个答案:

答案 0 :(得分:0)

我已经尝试过了。默认情况下,即使使用HTTPS协议,也在Azure上启用压缩。

要解决此问题,您需要捕获失败的请求跟踪日志。在日志中,它将告诉您压缩为何不起作用,如下所示:

enter image description here

原因说明可在this page的底部找到。

要捕获失败的请求跟踪日志,请执行以下步骤:

a)在Azure门户中启用“失败的请求跟踪”: enter image description here

b)在web.config的“ system.webServer”部分中添加以下配置

<tracing>
 <traceFailedRequests>
 <remove path="*" />
 <add path="*">
  <traceAreas>
   <add provider="ASP" verbosity="Verbose" />
   <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" />
   <add provider="ISAPI Extension" verbosity="Verbose" />
   <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,FastCGI" verbosity="Verbose" />
  </traceAreas>
  <failureDefinitions statusCodes="200" />
 </add>
</traceFailedRequests>
</tracing>

使用此设置,它将捕获带有200状态代码的所有请求。

3,通过FTP或KUDU在Web应用程序的root \ LogFiles \ W3SVCXXX文件夹中找到日志(推荐)。文件名类似于“ fr000001.xml”。 在KUDU页面中,单击“下载”按钮将在友好视图中打开日志。在日志中搜索“压缩”。这是一个成功的例子: enter image description here