Azure Web App Not Using GZip Compression
Asked Answered
C

2

7

I was using WebPageTest to test the performance of my Azure Web App (ASP.Net vNext Web API/Angular). I got an F for both "Compress Transfer" and "Cache Static Content".

After searching StackOverflow and Google, I added the following to my web.config:

<urlCompression doStaticCompression="true" doDynamicCompression="true" />
    <httpCompression>
      <dynamicTypes>
        <clear />
        <remove mimeType="*/*" />
        <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 />
        <remove mimeType="*/*" />
        <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>

and

<staticContent>
      <!-- Set expire headers to 30 days for static content-->
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>

After redeploying my Web App, I re-ran the test and I am still getting an F for both of them. Even though I have added these settings to web.config, it does not appear that Azure Web App is honoring them.

Also, I found out that some Web App tiers do not allow compression but I am running on an S2 and I verified that it does allow compression.

Any help would be appreciated!

Thanks!

Cenobite answered 30/11, 2015 at 19:18 Comment(0)
P
12

gzip compression is enabled by default for Azure Web Apps. You can see the rules in your sites LocalSiteRoot/Config/applicationhost.config. Looking at the response headers (which can easily be done with developer tools) should confirm that gzip is being used. It is possible that one of the resources that your site loads is not compressed, and this is causing the WebPageTest to fail. I would look at a network capture and the response headers, and see if you can find the offending resources if you're concerned.

To go to the local site root, you can use FTP, or go to your SCM site at https://.scm.azurewebsites.net/DebugConsole and then click the globe icon. enter image description here

Also I suspect that your 2 javascript files are not getting compressed since the Content-Type header is not getting populated, so the rule is not capturing it because it does not recognize the mimetype.

Poseur answered 1/12, 2015 at 1:22 Comment(5)
There are 2 files that are not being sent compressed. Both are javascript files served from my Azure Web App. When I open dev tools, if I look at the javascript files that are served from Google CDN it has a response header of "Content Encoding = gzip". However, when I look at the 2 javascript files served by my Azure Web App neither has this response header.Cenobite
How do I access LocalSiteRoot/Config/applicationhost.config? I click on my web app in Azure Portal and then click on "All Settings". I don't see anything that says LocalSiteRoot or config.Cenobite
I've edited my answer to show how to access LocalSiteRoot and added what I think the problem might be.Poseur
Gzip will by default work as well even on new ASP.NET 5 (dnx) apps that are running on Kestrel behind IIS. You still need static file handler in Kestrel, but on the way back IIS will detect it as static content and gzip them or whatever you specify in web.config (works with dynamic content as well)Brownstone
I followed your post and tried to compress a simple html page without any scripts but I have an issue in azure , here is my question.Biddable
C
8

Just to back up @theadriangreen here - it will be a header problem. I've found adding the types in the web.config to be unreliable.

What you need to do instead is edit the applicationHost.config file stored in the deepest dark part of azure. The easiest way to do this, is to install the IIS Manager extension either in the Azure portal or in Kudu. Kudu can be accessed via .scm.azurewebsites.net.

There you can edit the file, and it'll save a xdt for you - which once you restart the app you should find that the xdt gets applied.

Alternatively, you can just add an applicationHost.xdt to your App root and you are good to go. Here is a sample.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <httpCompression>
      <dynamicTypes>
        <add mimeType="application/json;charset=utf-8" enabled="true" xdt:Transform="InsertAfter(/configuration/system.webServer/httpCompression/dynamicTypes/add[(@mimeType='application/json')])" />
      </dynamicTypes>
    </httpCompression>
  </system.webServer>
</configuration>

References:-

Carmelocarmen answered 6/11, 2016 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.