Skip to content

Instantly share code, notes, and snippets.

@IDisposable
Created September 17, 2015 06:01
Show Gist options
  • Save IDisposable/949f47c6b1d165038200 to your computer and use it in GitHub Desktop.
Save IDisposable/949f47c6b1d165038200 to your computer and use it in GitHub Desktop.
Enabling static file compression on IIS
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
<!--minification requires dynamicCompressionBeforeCache off, order of operations error?-->
<httpCompression noCompressionForHttp10="false" noCompressionForProxies="false" staticCompressionIgnoreHitFrequency="true" />
<!--If your IIS server has this unlocked, uncomment
<serverRuntime frequentHitThreshold="1" frequentHitTimePeriod="00:00:10" enabled="true" />
-->
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="49.00:00:00" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
</staticContent>
<caching enabled="true">
<profiles>
<add extension=".ico" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".eot" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".ttf" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".woff" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".woff2" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".htc" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".svg" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Cookies, Accept-Encoding" />
<add extension=".xml" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
<add extension=".txt" policy="CacheUntilChange" kernelCachePolicy="DontCache" duration="49.00:00:00" location="Downstream" varyByQueryString="*" varyByHeaders="Accept-Encoding" />
</profiles>
</caching>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<clear />
<remove name="Access-Control-Allow-Origin" />
<add name="Access-Control-Allow-Origin" value="*" />
<remove name="Access-Control-Allow-Headers" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
@IDisposable
Copy link
Author

If you're in a bare IIS site, you might have to be explicit about the modules you want to run, also make sure your vary-bys match your usage... these are for no-cookie, etc.

Lastly you may need to unlock the serverRuntime element of your machine configuration, I do this with these commands in a cmd file

@echo off
%SystemRoot%\System32\inetsrv\appcmd.exe unlock config /section:system.webServer/serverRuntime
%SystemRoot%\System32\inetsrv\appcmd set config /section:httpCompression /+staticTypes.[mimeType='image/*',enabled='true'] /commit:apphost
%SystemRoot%\System32\inetsrv\appcmd set config /section:httpCompression /+dynamicTypes.[mimeType='image/*',enabled='true'] /commit:apphost
%SystemRoot%\System32\inetsrv\appcmd set config /section:httpCompression /+staticTypes.[mimeType='application/octet-stream',enabled='true'] /commit:apphost
%SystemRoot%\System32\inetsrv\appcmd set config /section:httpCompression /+dynamicTypes.[mimeType='application/octet-stream',enabled='true'] /commit:apphost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment