145

I would like to set up rules in IIS7 for static content caching in my ASP.NET website.

I have seen these articles, which details how to do it using the <clientCache /> element in web.config:

Client Cache <clientCache> (IIS.NET)
Add Expires or Cache Control Header to static content in IIS (Stack Overflow)

However, this setting appears to apply globally to all static content. Is there a way to do this just for certain directories or extensions?

For example, I may have two directories which need separate cache settings:

/static/images
/content/pdfs

Is it possible to set up rules for sending cache headers (max-age, expires, etc) based on extensions and folder paths?

Please note, I must be able to do this via web.config because I don't have access to the IIS console.

Community
  • 1
  • 1
frankadelic
  • 19,333
  • 31
  • 105
  • 161

3 Answers3

221

You can set specific cache-headers for a whole folder in either your root web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!-- Note the use of the 'location' tag to specify which 
       folder this applies to-->
  <location path="images">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

Or you can specify these in a web.config file in the content folder:

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

I'm not aware of a built in mechanism to target specific file types.

Maxime Rouiller
  • 13,212
  • 9
  • 53
  • 104
Kev
  • 112,868
  • 50
  • 288
  • 373
  • 1
    Great. Would you recommend me a book about IIS 7? I would like to learn about these things. Thanks. – vtortola Nov 09 '11 at 18:02
  • 5
    @vtortola - you can't go wrong with the [IIS7 resource kit](: http://www.amazon.co.uk/dp/0735624410), it's actually quite useful. The [Wrox Pro IIS7](http://www.amazon.co.uk/dp/0470152532) book isn't bad either. TBH I learned mostly from the IIS.NET config reference site: http://www.iis.net/ConfigReference and from poking about the `%systemroot%\system32\inetsrv\config\applicationhost.config` file and related friends. – Kev Nov 09 '11 at 18:11
  • 7
    Does anyone know if this is recursive? e.g. If you have sub folders under you images path, will it also cache those? – StuffandBlah Jun 28 '12 at 07:47
  • 1
    One thing to note is the browser appears to see the `path=""` as case sensitive – Chris S Jul 09 '12 at 15:31
  • 9
    @StuffandBlah Yes, it is recursive. Just tried it myself and IIS applied the same cache control settings to all requests for files in subfolders of the folder I specified as "location". – Erik Öjebo Mar 30 '13 at 21:38
  • I am trying to cache my images using your method. Now when I analyze using HttpFox I see 2 requests made for each image. 1. First one gives an aborted result with (NS_BINDING_ABORTED) error 2. second request is a cached image. Any thoughts? – Mithil May 02 '13 at 21:26
  • Ok, Kev. I tried this and some other solutions. I get max-age=0. Enviroment: IIS 8, Classic application pool... – Vlado Pandžić Jan 16 '14 at 00:01
  • Kev...you don't have to be angry....I can see you like to talk much but really it is not working for me...and for collegeus from other company....I believe you are helful but I can't say it is working if it isn't. – Vlado Pandžić Jan 16 '14 at 00:16
  • @impeRAtoR - apologies, thought you were another low-rep drive-by twit downvoting users who have put a decent amount of effort into testing and validating their answers before posting them. I am here to help :) – Kev Jan 16 '14 at 00:18
  • No problem, I would like that you help me. I belive you are passionate programmer and that you are real enthusiast....as you can see: http://stackoverflow.com/questions/21074198/leverage-browser-caching-in-iis-google-pagespeed-issue I opened this question and offered bounty because this is important to me... – Vlado Pandžić Jan 16 '14 at 00:20
  • @impeRAtoR - do you mind if I have a look at this tomorrow, it's 00:20 my time and I'm very tired (has been a very long day), as you can probably tell. How about opening a new question, referring to this one and explain that it doesn't work. Please provide as much information about your environment, proxies, any upstream caching server etc. And also directory structure and where you've placed these settings in web.config. – Kev Jan 16 '14 at 00:24
  • Sounds great. It is 1:20 my time :), it was long day really. I will do my best to provide as much information as possible.... – Vlado Pandžić Jan 16 '14 at 00:26
  • @Kev I provided much more information, including web config file: http://stackoverflow.com/questions/21074198/leverage-browser-caching-in-iis-google-pagespeed-issue – Vlado Pandžić Jan 16 '14 at 09:06
  • i can see stack overflow set both "Expires" and "max-age" for their images and css etc , like: `Cache-Control:public, max-age=60` `Expires:Sun, 13 Mar 2016 08:05:18 GMT ` how do they to that ? when i use both in web.config for image folder as done [here](http://stackoverflow.com/q/20826035/2218697) i get 500 error internal server error. – Shaiju T Mar 13 '16 at 09:23
70

You can do it on a per file basis. Use the path attribute to include the filename

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <location path="YourFileNameHere.xml">
        <system.webServer>
            <staticContent>
                <clientCache cacheControlMode="DisableCache" />
            </staticContent>
        </system.webServer>
    </location>
</configuration>
Jeff Cuscutis
  • 10,487
  • 6
  • 25
  • 21
  • 1
    And if you want to apply it to all files of a specific extension ? would .xml or *.xml alone work ? – Zulgrib Jul 25 '16 at 14:59
  • 3
    @Zulgrib to apply it to a specific extension you can use outbound rewrite rules: http://stackoverflow.com/questions/32987486/iis-setting-cache-control-header-per-file-type/33444897#33444897 – jotap Sep 22 '16 at 20:58
  • @Zulgrib How do we use the location tag for more than one file but not all files of a type. For example couple of jpg files in the root folder but not all? – Rahatur Jan 23 '19 at 18:41
-2

I had the same issue.For me the problem was how to configure a cache limit to images.And i came across this site which gave some insights to the procedure on how the issue can be handled.Hope it will be helpful for you too Link:[https://varvy.com/pagespeed/cache-control.html]

  • 2
    Please consider adding some information from the link to your answer as per https://stackoverflow.com/help/how-to-answer: Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Greg the Incredulous Nov 29 '17 at 05:54