GZip Compression On IIS 7.5 is not working
Asked Answered
T

7

62

I am trying to support GZip compression for my static files under IIS (which should be enabled by default but not) but not working so far. Here is the the section under <system.webServer> node inside the web.config file of the web app;

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/atom+xml" enabled="true" />
    <add mimeType="application/xaml+xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
</httpCompression>

<urlCompression doStaticCompression="true" />

I tried it with Google Chrome. Here are the Request Headers;

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Cache-Control:no-cache

Connection:keep-alive

Host:my-website-url

Pragma:no-cache

User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30

These are the Response Headers;

Accept-Ranges:bytes

Content-Length:232651

Content-Type:application/x-javascript

Date:Thu, 04 Aug 2011 08:58:19 GMT

ETag:"a69135734a50cc1:0"

Last-Modified:Mon, 01 Aug 2011 12:56:37 GMT

Server:Microsoft-IIS/7.5

X-Powered-By:ASP.NET

I check the applicationHost.config file and found some nodes like below;

----

<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />

----

<section name="urlCompression" overrideModeDefault="Allow" />

----

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>

----

<urlCompression />

What am I missing here?

Tocopherol answered 4/8, 2011 at 9:4 Comment(3)
Should I have asked this question on serverfault.com?Tocopherol
I'm having the saame problem. No matter what I do, it will not compress anything, according to Wireshark.Statutable
did you get final solution with @Statutable answer ?Moussorgsky
S
48

After a lot of searching, I finally found what got compression working on my IIS 7.5. To start with, IIS will not compress a file unless it loaded often enough. That brings up the question "what does IIS consider often enough?" Well, the defaults are 2 times every 10 seconds. Yikes!

This setting can be changed in web.config, but the section needs to be unlocked first in applicationHost.config. Here are the commands:

First unlock the section:

C:\Windows\System32\inetsrv\appcmd.exe unlock config /section:system.webServer/serverRuntime

Unlocked section "system.webServer/serverRuntime" at configuration path "MACHINE/WEBROOT/APPHOST".

Now that is done, edit the web.config file and add the serverRuntime element:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <system.webServer>
      <serverRuntime frequentHitThreshold="1" frequentHitTimePeriod="10:00:00" />
...

In this case, I set it to hit the file once in a 10 hour period. You can adjust the values as necessary. Here is the document that explains the serverRuntime element:

http://www.iis.net/configreference/system.webserver/serverruntime

I hope this helps get your compression working as well.

Note: you can also set the serverRuntime element up in the applicationHost.config file, but I chose to change it in the web.config because we have a number of servers and farms with various sites, and it is easier for me to control it from this level of granularity.

Statutable answered 25/3, 2013 at 23:53 Comment(7)
Turns out you can avoid the "frequently requested" issue altogether with this param: staticCompressionIgnoreHitFrequency under httpCompression. Set to True.Raymonderaymonds
@Raymonderaymonds is this available under all operating systems? Or just newer, such as 2012R2?Statutable
I don't think it's limited by OS but perhaps by IIS version. The www.iis.net site has it in their doc from at least iis7 and I can see it on my dev machine which is Win 8.1. iis.net/configreference/system.webserver/httpcompressionRaymonderaymonds
...Link above states: The staticCompressionIgnoreHitFrequency attribute was added in IIS 8.5.Episodic
And if you decide to edit %windir%\System32\inetsrv\config\applicationHost.config on x64 bit Windows via a Notepad++ or any other 32bit editor, you may need to use another path, see: forums.iis.net/t/1151982.aspxMuddy
What do you mean with set it to hit the file once in a 10 hour? What happens after the file has not been requested for 10 hours? Will IIS clear the cached file afterwards, and if so, how can I configure IIS to never clear the cache for my static content?Chiro
If you include the serverRuntime element on a site and host it on an IIS 10/Win Server 2016 that has not had the element unlocked yet, the site will 500 with no error details no logging and no other clues as to why/how your site is failing.Uprise
L
29

One thing to keep in mind is that the first hit usually is returned uncompressed immediately, but spins up a thread to compress the file in the background in order to service the response with compression for future requests.

Also, have you tried using a different client (e.g. IE)?

Lobell answered 6/8, 2011 at 13:49 Comment(6)
woww, it is good to get an answer from you sir :) yeah, tried different things but no chance at all. tried fiddler as well :) Thinking that it could be a permission issue on '%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files' folder but couldn't sort it out as well. Do I need to set permission for all of the users of my web apps on the iis? because I am nearly 20 apps running and every app has their own user account on the server. "IIS Temporary Compressed Files" folder has IIS_IUSRS users full control permission. What is missing I am really wondering here.Tocopherol
how can I get Failed Request Tracing for this action to see the actual error?Tocopherol
Technically, compression only happens for "frequently requested" content, which is defined under the system.webServer / serverRuntime element with the frequentHitThreshold and frequentHitTimePeriod attributes. See more here.Construe
I was having the same problem, so I opened browser windows in Firefox, Chrome, and IE at the same time. After hitting the same page several times, the content was coming back encoded. Previously, I had been Shift + F5 refreshing the page each time, which was getting the uncompressed content (the first, uncompressed version, that you mentioned). I was really scratching my head until your comment, so thanks. Microsoft would do well to be a little clearer explaining this behavior.Drusilla
Damn it!! I was crazy trying to find the problem and after read your post I cleared cache, reload twice and it was there! Is there anyway to trigger this automatically? ThanksMock
Turns out you can avoid the "frequently requested" issue altogether with this param: staticCompressionIgnoreHitFrequency under httpCompression. Set to True.Raymonderaymonds
G
14

Make sure you install Dynamic Compression on the server. Add/Remove Features under IIS.

Gunthar answered 13/2, 2013 at 2:21 Comment(1)
I cannot believe this, why can you select Static Compression if it REQUIRES Dynamic Compression to be enabled ? But that was the fix.Peremptory
G
6

Took me a while to figure this out too. Setting the frequentHitThreshold attribute to 1 on the system.webServer/serverRuntime node in the applicationHost.config file should do the trick, as documented at http://www.iis.net/ConfigReference/system.webServer/serverRuntime.

You can do this by executing the following command as an administrator:

%windir%\system32\inetsrv\appcmd set config /section:serverRuntime /frequentHitThreshold:1 /commit:apphost

A word of warning - the "frequent hit" concept does not seem specific to compression. I have no idea whether there are other consequences as a result of setting this!

Gibraltar answered 2/4, 2012 at 3:15 Comment(2)
This gives me Unknown attribute "frequentHitThreshold".Exploitation
I think the setting you really want is <httpCompression staticCompressionIgnoreHitFrequency="true" />Satterlee
S
1

"system.webServer configuration does not allows httpCompression at the Web site level" https://serverfault.com/questions/125139/iis7-dynamic-compression-not-success-reason-12

Why do you use configuration files? Just try to create new dummy web site with some txt file more than 2700 bytes. Also you may try to install dynamic compression module and turn it on for server ant this dummy site.

Saxecoburggotha answered 12/8, 2011 at 21:22 Comment(4)
I think what you mean to say is that httpCompression cannot be defined in the Web.config. But, it does belong under system.webServer in the applicationHost.config file.Construe
so there is a location node inside the applicationHost.config file for every each site. Do I need to configure it from there? Also I need to say that the compression is working fine under iis express during the development stage.Tocopherol
I am not trying this for dynamic compression. actually I didn't install it as well. only static compression is enabled. Honestly, It should be but not.Tocopherol
OK. You could try to trace your fault zip request. Check this forums.iis.net/t/1162238.aspx#1922938 . Also first of all, please, check the access permissions for 'temp' folder. See this forums.iis.net/t/1172925.aspxSaxecoburggotha
C
1

One thing we found was that our Azure website was hitting it's max CPU usage due to having a high resource WebJob running. We had tried all the settings above and nothing worked. Then we checked the Resource CPU usage and found it was 80%+. At 80% CPU load the gzip stops working!

Companion answered 7/10, 2016 at 17:37 Comment(0)
A
0

I believe it is wort adding this as response because it is only mentioned in comments but this is the real solution, if you want to always serve compressed resources. Fiddling with staticCompressionIgnoreHitFrequency is a viable solution only if you really don't care about having someone to download big files.

The above setting will always compress, as long as the CPU utilization didn't trigger staticCompressionDisableCpuUsage.

To be 100% sure that resources are compressed you should use:

  staticCompressionIgnoreHitFrequency true 
  staticCompressionDisableCpuUsage 100
  staticCompressionEnableCpuUsage 100

For example if you wish to change it using the web.config, you can use:

<configuration>
  <system.webServer>
      <urlCompression doStaticCompression="true" doDynamicCompression="false" />
      <httpCompression staticCompressionIgnoreHitFrequency="true" staticCompressionDisableCpuUsage="100" staticCompressionEnableCpuUsage="100"/>
  </system.webServer>
</configuration>

Doing so even the first request is going to be compressed, even when the CPU has reached 100% utilization.

Remember that IIS automatically recycles the app pool based on configuration so even if you set 10 hours but your app pool resets every 1 hour, this will cause your first call after the reset to be not compressed.

Make sure you know what you are doing as GZip is still o costly operation and depending on the type of traffic you have you might cause performance issues fiddling with these settings.

For more information: https://learn.microsoft.com/en-us/IIS/wmi-provider/httpcompressionsection-class?redirectedfrom=MSDN

Moreover if interest consider using Brotli instead of Gzip, it provides higher level of compression with a lower impact on CPU performance. https://learn.microsoft.com/en-us/iis/extensions/iis-compression/iis-compression-overview

Albuquerque answered 26/7, 2022 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.