How to configure static content cache per folder and extension in IIS7?
Asked Answered
P

3

149

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.

Peracid answered 3/2, 2010 at 20:38 Comment(0)
I
227

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.

Innovate answered 4/2, 2010 at 1:50 Comment(15)
Great. Would you recommend me a book about IIS 7? I would like to learn about these things. Thanks.Bowsprit
@Bowsprit - you can't go wrong with the [IIS7 resource kit](: amazon.co.uk/dp/0735624410), it's actually quite useful. The Wrox Pro IIS7 book isn't bad either. TBH I learned mostly from the IIS.NET config reference site: iis.net/ConfigReference and from poking about the %systemroot%\system32\inetsrv\config\applicationhost.config file and related friends.Innovate
Does anyone know if this is recursive? e.g. If you have sub folders under you images path, will it also cache those?Saccharometer
One thing to note is the browser appears to see the path="" as case sensitiveVendetta
@Saccharometer 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".Ambush
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?Flyaway
Ok, Kev. I tried this and some other solutions. I get max-age=0. Enviroment: IIS 8, Classic application pool...Sophiasophie
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.Sophiasophie
@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 :)Innovate
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: #21074698 I opened this question and offered bounty because this is important to me...Sophiasophie
@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.Innovate
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....Sophiasophie
@Innovate I provided much more information, including web config file: #21074698Sophiasophie
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 i get 500 error internal server error.Misanthropy
and Backend/html response?? ops we forgot about that?Frager
P
72

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>
Pteryla answered 27/1, 2011 at 20:13 Comment(3)
And if you want to apply it to all files of a specific extension ? would .xml or *.xml alone work ?Murrell
@Murrell to apply it to a specific extension you can use outbound rewrite rules: #32987986Terricolous
@Murrell 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?Task
S
-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]

Sanctitude answered 29/11, 2017 at 5:22 Comment(1)
Please consider adding some information from the link to your answer as per 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.Intromit

© 2022 - 2024 — McMap. All rights reserved.