How to set cache headers for files accessed directly - asp.net

I have a server that serves WAV file to Twilio and I am having some problems to set the cache to those files.
My problem is that the files have different expiration date, some shouldn't be cached, some should be cached for a day, some for a month and so on.
At the moment I was able to set to not cache any of the files using the IIS Output Caching, however now I need to set some caches.
Basically Twilio request the WAV files calling it directly, for example, http://mywebsite.com/mysoundwithoutcache.wav as well as http://mywebsite.com/mysoundwith1daycache.wav and so on.
My question is how do I add this headers to those files that are been called directly?
I am using IIS 7 and ASP.NET
Thanks in advance

The way to apply settings just to a certain files is to use the location tag, in your web.config add a new node under configuration:
<location path="cache_me.wav">
<system.webServer>
<caching>
<profiles>
<add extension="*.wav" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="01:00:00" />
</profiles>
</caching>
</system.webServer>
</location>
Unfortunately I don't think you can use wildcards in the path attribute, so you one of these sections for each file. If would be easier to put all files to be cached into a separate folder, then you can get away with a single location node.
Having said all that, you should not worry about output caching for your waves files, static files are cached by IIS automatically.

Related

Rewrite Maps in IIS Make web.config Too Large

I am migrating a website which is/will be running on IIS and I will be using rewrite maps to 301 redirect old ".asp" URLs to a new style of URL. For many thousands of URLs there is no pattern, so it appears I must rely on rewrite maps.
My problem is that the default web.config size limit is 250kb, and in my environment, I don't have access to change this (as can be done at the registry level - if one had access).
I have looked into moving the rewriteMaps section to an external file, but the external files also have the default size limit of 250kg, so this is also not going to work.
I am looking for some other way to handle this... I am sitting at 242kb currently and have over twice the amount of old to new redirect mapping to add.
Thanks in advance.
As I am in a shared environment, there was no solution other than to move it to a single external config file, which was again, limited to 250KB.
So here is what I did:
I filled up the map to capicity with the highest-trafficked redirects and of course any groups of urls that could be redirected using a pattern (so they would be fastest).
For the 100k remaining long-tail of the redirects, I put them into a REDIRECTS table in the db... SO, after the request passes all of the rewrite rules in the file (and of course, doesn't hit any), it defaults the request to a certain script. One of the first things the script does is check the REDIRECTS table, and if an entry exists, I do a redirect in the code... it's slower, but most of the stuff in the table is long tail, and as I said, the most visited redirects are still in the file. This has worked well for me so far, and I can add as many redirects as I want... e.g. if a page title/url gets edited, my admin area automatically adds the redirect, etc.
The Nativerd.dll file uses the value of this registry key to determine the maximum allowed size, in KB, of the Web.config files. The Configuration system assumes a default value of 250 KB in Windows Server 2008 and 100 KB in the release version of Windows Vista.
The reason for the 250KB limit is to reduce attacks for uploading a large web.config file. You can change the the limit by altering the upper value in your registry:
HKLM\SOFTWARE\Wow6432Node\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB (REG_DWORD)
See: Description of the registry keys that are used by IIS 7.0, IIS 7.5, and IIS 8.0
Another option is to split your web.config files into multiple smaller files.
We were stuck on this for a long while, we ended up writing our own 301 redirector. In sitecore it was in a pipeline patched after ItemResolver which consumes the large file (not included in any Web.configs). We could not use the "registry hack" option as it an Azure Service App and there is no (easy and cheap) access to the registry.
You can split up your configuration into a few different files as Neill said.
You will have a main web.config file in which you’ll reference sub config files by adding a configSource attribute onto the sections you would like to split into other files.
For example, if you’d like to split the section “appsettings” in another file, you would change the appSettings section in your web.config file to :
<appSettings configSource="appsettings.config" />
and in the appsettings.config file, you would add all your appsettings entries like they were in the original web.config file, for example;
<appSettings>
<add key="aspnet:RestrictXmlControls" value="true" />
<add key="FeedCacheTime" value="300" />
<add key="FeedPageUrl" value="/_layouts/15/feed.aspx?" />
<add key="FeedXsl1" value="/Style Library/Xsl Style Sheets/Rss.xsl" />
<add key="aspnet:AllowAnonymousImpersonation" value="true" />
</appSettings>
Obviously with the rewrite maps instead.

Two different expirations dates for twilio audio files

I have an that uses Twilio and this app has 2 very different sets of audio files:
Menus (files never change)
Information (files change constantly)
At the moment I had to set all the cache for my files of as I couldn't figure out how to set a different expiration date before send the file to twilio, the problem been that twilio download the files directly from the website, so as far I as understand I cannot set the cache using ASP.net
I am using the xml below in the web.config in the directory where my files are at the moment but I cannot find a way to specify different values to different files, for example:
menu1.wav - max cache
meny2.wav - max cache
info1.wav - no cache
info2.wav - no cache
Is it even possible?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</configuration>
Twilio evangelist here.
Looks like you can use the <location> element to define different folders that have different cache policys on them:
How to configure static content cache per folder and extension in IIS7?
Hope that helps.

IIS and Static content?

According to Ultra-Fast ASP.NET: Chapter 3 - Caching:
Files that the browser retrieves from the server should be stored in
the browser’s cache as long as possible to help minimize server
round-trips.
But how does IIS know what a static content actually is and is not?
Is it just images, CSS, JS and not ASPX, ashx...?
Where can I see in IIS what is already considered to be static and what is not ?
What about the scenario where a page has been declared with <%# OutputCache header (without location)? Are the images, CSS and JS source files inside of it also being output cached with the same properties?
As a best practice, I should set one year into the future as the maximum expiration time. I should use that as the default for all static content on the site
So I did this :
But later, after pressing OK, I can't find any summary menu which shows me: to whom I already put a response header (in this case: the css folder).
Currently, in order to see that css folder has been applied with response headers - I have to go to the css folder again --> Http Response Header-->Set Common Headers --> and then I see it. It isn't written in the web.config.
But if I do it for a file (Login.aspx for example): I do see it in web.config:
<configuration>
<location path="Login.aspx">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseExpires" cacheControlMaxAge="1.00:00:00" httpExpires="Fri, 15 Feb 2013 00:00:00 GMT" />
</staticContent>
</system.webServer>
</location>
</configuration>
I understand your situation. Sometime its confusing how IIS handles a file. Its also different for IIS 6 vs IIS 7 and different for Classic App Pools and Integrated mode app pools. My experience is mostly with Integrated App Pools on IIS 7.5, so thats the environment I can comment on most accurately.
First Question
But how does IIS knows what is actually a static content and what is
not?
Is it just images , css , js and not ASPX , ashx...?
Where can I see in the IIS what is already considered to be static and
what not ?
You can inspect the list of file handlers in IIS by navigating to your website and then click 'Handler Mappings'. By default these are inherited from the .Net base web.config which is in a different location depending on your .Net framework version.
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config
If a file being requested isn't already explicitly mapped to another handler it falls to a catch all handler (*) as the last option (System.Web.DefaultHttpHandler) which determines if it is a static file or a directory browsing request. So Static files are simply files not bound to another handler already. For example you'll see that *.aspx is already mapped to System.Web.UI.PageHandlerFactory prior to this default handler. So its going to be processed by that handler and not be considered a static file. If you removed that mapping you could technically serve *.aspx as a static file if you really wanted to (just for proof of how it works).
But you can also explicitly list a file type as a static file by adding an entry in your web.config's httpHandlers section mapping the file extensions to System.Web.StaticFileHandler in IIS. For example:
<configuration>
<system.webServer>
<handlers>
<add name="StaticHandler" verb="*" path="*.zip" type="System.Web.StaticFileHandler" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
This example is using the <system.webServer> config section, so its for an App Pool running in Integrated Mode.
Second Question
What about the scenario where a page has been declared with <%#
OutputCache header(without location) . does the images,css,js src
files inside of it , are also being output cached with the same
properties?
No. Because the page is being server as a separate request (maybe even by a separate handler) it can have totally different cache headers/hints. The host page and the resources it may use are not related from a caching perspective.
In fact you may even want to have a shorter cache period for *.html and a longer cache period for *.jpg or *.png? Something to consider.
Third Question
As a best prcatice , I should set one year into the future as the
maximum expiration time.I should use that as the default for all
static content on the site
Hmm... I might not go as far as one year. How about one month? I would set a global policy like this:
<configuration>
<system.webServer>
<staticContent>
<!-- Set expire headers to 30 days for static content-->
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
This is the same as the sample you showed above, but is not inside a <location> element, instead it is right in the root <configuration> element so it is the default policy. Again this is for an App Pool running in Integrated Mode. Sometimes you also need to turn on:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<!-- stuff -->
</modules>
</system.webServer>
<system.webServer>
This just makes sure that static files are processed through the managed static file handler which respects the above configuration elements.
Edit to Address Comments
The documentation for the configuration dialog you posted above is located here: Configure the HTTP Expires Response Header (IIS 7)
Apparently these settings are saved in C:\Windows\System32\inetsrv\config\applicationHost.config
I do not have IIS7 and personally develop on IIS 7.5 now. So please post a comment if you can verify this location is accurate!
The static content is the one that IIS is read and send to the browser with out any processing. There you can setup IIS to include some Cache-Control Header to cache it on clients browser computers.
You can do that ether by direct setup IIS, ether by commands on web.config as you say. The commands that you add on web.config and concern the IIS, did not have to do with asp.net it self, but the IIS, and IIS saves his configuration on a different file, so when you change that cache control headers direct on IIS you do not see them on web.config.
Now for the static content like images, CSS, JavaScript, and other similar files they say that you can follow the "never expire" policy by adding 10 years expire.
The issue here is that if you can not change the content of the static file, if for example you cache a javascript file with 10 years, and you make a small change on it, then you need ether to change the file name, ether to add some parameter at the end of it.
Now the <%# OutputCache on a control is referred to the server cache and not to the client, and what is actually do is to cache the render of the control on the server so the next time you ask it to not lose time to renders it again but read it from cache - is still send it to the browser.
And you can also read this answer for some more: What are difference between IIS (Dynamic and Static) cache,OutPutCache and browser cache

Is it possible to control Cache-Control headers in IIS for different static file types?

In IIS, can I configure it to return different cache-control headers for different static file types in the same folder?
I'm aware that I can use the HTTP Headers feature to set Expires Immediately, but that seems to affect all content. Is there a way to do it for specific file extensions for static content?
While not an ideal answer, I did find a work-around. In my application, I have control over where my JS, CSS, and other similar files live in the directory structure. What I did was put a web.config in the IIS root directory with these lines (among others, unrelated):
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" setEtag="true" />
</staticContent>
</system.webServer>
</configuration>
This sets the default in IIS that nothing should be cached. This is where the HTML page (index.html) for my single-page-application (SPA) lives, so that's what I want.
Then, in immediate subdirectories for images, JS, CSS, etc, I have web.config files with the following:
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="14.00:00:00" setEtag="true" />
</staticContent>
</system.webServer>
</configuration>
You can of course set the age to whatever you want. I am starting with 14 days for now until I'm sure I everything right, then I will probably bump that to several months.
This way, the HTML of my SPA will never get cached, but all the JS/CSS/images will live as long as I want. When I rebuild the project for next deployment, web-pack will generate new file names for the packed JS and CSS, so no cache problem there. The HTML file will be the same name of course, but the included JS file names will be different. So it's key that the HTML file not ever be cached, which this setup accomplishes.
IIS should really make this simpler. A cache-by-file-type config would work perfectly and not require strict directory structure management, which not all projects have the flexibility to follow.

Is web.config or app.config cached in memory

if it is cached, what happens if I use multiple web.config in multi-level folders
They all get cached.
Configuration is read once, at startup. With web.config, IIS watches for file changes and restarts the application.
OK, so ya'll are missing a KEY feature in the Web.Config file's area.
Yes, web.config is cached and changing contents of the file will restart your web app. And, all your connected users will not be happy, too, because they'll need to "reconnect" a-new, possibly losing desired information.
So, use an EXTERNAL custom file for your AppSettings, as follows:
<appSettings configSource="MyCustom_AppSettings.config"/>
Then, in the file MyCustom_AppSettings.config file, you have your settings, as such this example has:
<appSettings>
<!-- AppSecurity Settings -->
<add key="AppStatus_Active" value="Active"/>
<!-- Application Info Settings -->
<add key="AppID" value="25"/>
<add key="AppName" value="MyCoolApp"/>
<add key="AppVersion" value="20120307_162344"/>
</appSettings>
Now, if you need to add, change, or remove an AppSetting, when you change it in this file the change is nearly instant in your web-app BUT (and here's the BEST part), your app DOES NOT RESTART!
Everything stays kosher except those settings you've added/modified/removed in the external .config file.
And, yes, the same thing can done for the section as follows:
<connectionStrings configSource="MyCustomApp_ConnectionStrings.config"/>
and the file MyCustomApp_ConnectionStrings.config has all the connection strings you need. Change a connection string in the external .config file and it starts getting used right away and with no web-app restart.
The configSource setting(s) are great when you need to deploy to development, testing, and production on different boxes and need settings pertinent to that given box/environment.
So, now ya know (something that's been around for 7+ years).
It's That Simple. Really.
KC
Web.config (excluding external config files) is read when the application loads. Some config settings have a cascading behavior. For example, the system.web/authorization section can be overridden by configs at deeper levels.
ASP.NET monitors the web.config for changes. When it changes, the web application is forced to restart. Moral is that web.config settings are cached for the life of the application.

Resources