IIS7 Cache-Control

Chris Meek picture Chris Meek · Mar 13, 2009 · Viewed 104.4k times · Source

I'm trying to do something which I thought would be fairly simple. Get IIS 7 to tell clients they can cache all images on my site for a certain amount of time, let's say 24 hours.

I have tried the step on http://www.galcho.com/Blog/post/2008/02/27/IIS7-How-to-set-cache-control-for-static-content.aspx but to no avail. I still get requests going to the server with 304s being returned.

Does anyone have a way of doing this? I have a graphically intensive site and my users are being hammered (so is my server) every time they request a page. Wierdly the images seem to have "Cache-Control private,max-age=3600" showing up in Firebug but the browser is still requesting them when I press F5.

Answer

Jeff Atwood picture Jeff Atwood · Aug 20, 2009

If you want to set the Cache-Control header, there's nothing in the IIS7 UI to do this, sadly.

You can however drop this web.config in the root of the folder or site where you want to set it:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

That will inform the client to cache content for 7 days in that folder and all subfolders.

You can also do this by editing the IIS7 metabase via appcmd.exe, like so:

\Windows\system32\inetsrv\appcmd.exe 
  set config "Default Web Site/folder" 
  -section:system.webServer/staticContent 
  -clientCache.cacheControlMode:UseMaxAge

\Windows\system32\inetsrv\appcmd.exe 
  set config "Default Web Site/folder" 
  -section:system.webServer/staticContent 
  -clientCache.cacheControlMaxAge:"7.00:00:00"