Enable IIS7 gzip
Asked Answered
B

10

230

How can I enable IIS7 to gzip static files like js and css and how can I test if IIS7 is really gziping them before sending to the client?

Benbow answered 31/3, 2009 at 17:23 Comment(0)
N
243

Configuration

You can enable GZIP compression entirely in your Web.config file. This is particularly useful if you're on shared hosting and can't configure IIS directly, or you want your config to carry between all environments you target.

<system.webServer>
  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
      <add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="*/*" enabled="false"/>
    </dynamicTypes>
    <staticTypes>
      <add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="*/*" enabled="false"/>
    </staticTypes>
  </httpCompression>
  <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>

Testing

To test whether compression is working or not, use the developer tools in Chrome or Firebug for Firefox and ensure the HTTP response header is set:

Content-Encoding: gzip

Note that this header won't be present if the response code is 304 (Not Modified). If that's the case, do a full refresh (hold shift or control while you press the refresh button) and check again.

Niphablepsia answered 26/3, 2011 at 18:54 Comment(10)
@DenNukem, sorry I am not really sure. I only tested this on IIS7. Anyone else care to comment?Niphablepsia
Beware - it works only if httpCompression section in applicationhost.config is unlocked. By default, the section is locked for modifications, so overriding in web.config does not work. Wasted several hours on this. https://mcmap.net/q/119943/-how-do-you-change-the-allowdefinition-section-attribute-using-appcmd-in-iis-7, see comment bellow article.Gorlovka
Ensure its an installed feature of the Web Role, and also, from MSDN: "You can also add wildcard entries for the MIME types. However, you can set MIME types for the web server level only. For example, to enable static compression for all MIME types for the default website, first add wildcard entries for the MIME types for the server level, and then enable static compression for the default website."Plumbum
Dynamic compression will also not work unless you have the Dynamic Content Compression module installed on the Server (attainable via the web platform installer). You will need this if you're using Css/Js bundles.Avon
only css is getting compressed, .js doesn't. Also, tried adding application/x-javascript. Using together with tomcat, hence removed the directory parameter.Pipsqueak
If you have access to the IIS installation and the web.config is blocked, the above trick still won't work. Easy way to unlock/enable IIS compression: appcmd set config /section:urlCompression /doStaticCompression:TrueLyte
Beware of IIS 7.5 !! It is not enough to have only sections <httpCompression> and <urlCompression>, because IIS considers a file eligible for compression only if it is frequently accessed. So you need to adjust this setting <serverRuntime frequentHitThreshold="some_val_here" frequentHitTimePeriod="some_val_here" /> See more here: stackoverflow.com/a/15626981 <br> 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.aspxSubrogation
I couldn't get IIS 10 to gzip application/json mimetypes. It would gzip anything else - just not application/json. My workaround was to change the mimetype to text/json then the above config works. See serverfault.com/questions/426285/…Ophiolatry
I have angular build files as a static files to compress but after following this I font find content-encoding as a gzipEliathas
I'm facing one issue is I can see Request header like Content-Encoding:gzip but it automatically comes sometimes and after refreshing it disappearsEliathas
M
58

You will need to enable the feature in the Windows Features control panel:

IIS feature screenshot

Machree answered 28/2, 2014 at 18:28 Comment(1)
In Windows Server 2008 R2, this is located under Server Manager > Roles > Web Server (IIS). Click "Add Role Services" in the "Roles" section. "Dynamic Content Compression" is listed under the "Performance" header.Instigate
R
37

Global Gzip in HttpModule

If you don't have access to the final IIS instance (shared hosting...) you can create a HttpModule that adds this code to every HttpApplication.Begin_Request event :

HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;

Testing

Kudos, no solution is done without testing. I like to use the Firefox plugin "Liveheaders" it shows all the information about every http message between the browser and server, including compression, file size (which you could compare to the file size on the server).

Rhizoid answered 31/3, 2009 at 17:58 Comment(3)
I'm using shared hosting and didn't need to write any code to enable GZIP output compression. It was possible via Web.config alone. See my answer: https://mcmap.net/q/117547/-enable-iis7-gzip/…Niphablepsia
I placed the first three lines at the start of the one aspx page I wanted to compress, and it works! Thank you! This is so much less hassle than any other approach, and it works on IIS 6 for me.Choroid
This is absolutely the way to do it.Massasoit
M
5

under windows 2012 r2 it can be found here:

enter image description here

Merlon answered 16/11, 2016 at 7:56 Comment(0)
S
2

I only needed to add the feature in windows features as Charlie mentioned.For people who cannot find it on window 10 or server 2012+ find it as below. I struggled a bit

Windows 10

enter image description here

windows server 2012 R2

enter image description here

window server 2016

enter image description here

Subalternate answered 24/4, 2017 at 14:46 Comment(0)
A
1

If you use YSlow with Firebug and analyse your page performance, YSlow will certainly tell you what artifacts on your page are not gzip'd!

Adumbrate answered 1/4, 2009 at 11:28 Comment(0)
E
1

If you are also trying to gzip dynamic pages (like aspx) and it isnt working, its probably because the option is not enabled (you need to install the Dynamic Content Compression module using Windows Features):

http://support.esri.com/en/knowledgebase/techarticles/detail/38616

Embolus answered 12/11, 2013 at 2:56 Comment(0)
P
1

For all the poor guys who have to struggle with a german/deutsche Server :)

auf deutsch bitte schön

Pupa answered 7/9, 2018 at 13:16 Comment(0)
F
0

Try Firefox with Firebug addons installed. I'm using it; great tool for web developer.

I have enable Gzip compression as well in my IIS7 using web.config.

Fertilization answered 3/11, 2010 at 3:32 Comment(1)
Google also has a page test available: developers.google.com/speed/pagespeed/insightsElum
B
0

Another easy way to test without installing anything, neither is it dependent on IIS version. Paste your url to this link - SEO Checkup

test gzip

To add to web.config: http://www.iis.net/configreference/system.webserver/httpcompression

Beethoven answered 30/4, 2014 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.