Enabling Http caching and compression in IIS 7 for asp.net websites
- by anil.kasalanati
Caching –
There are 2 ways to set Http caching
1-
Use Max age property
2-
Expires header.
Doing the changes via IIS Console –
1.
Select the website for which you want to enable
caching and then select Http Responses in the features tab
2.
Select the Expires webcontent and on changing
the After setting you can generate the max age property for the cache control
3.
Following is the screenshot of the headers
Then you can use some tool like fiddler and see 302 response
coming from the server.
Doing it web.config
way –
We can add static content section in the system.webserver
section
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
Compression -
By default static compression is enabled on IIS 7.0 but the
only thing which falls under that category is CSS but this is not enough for
most of the websites using lots of javascript.
If you just thought by enabling dynamic compression would fix this then
you are wrong so please follow following steps –
In some machines the dynamic compression is not enabled and
following are the steps to enable it –
Open
server manager
Roles >
Web Server (IIS)
Role Services
(scroll down) > Add Role Services
Add
desired role (Web Server > Performance > Dynamic Content
Compression)
Next,
Install, Wait…Done!
? Roles > Web Server (IIS)
? Role Services (scroll down) > Add
Role Services
Add
desired role (Web Server > Performance > Dynamic Content
Compression)
Next,
Install, Wait…Done!
Enable -
? Open server manager
? Roles > Web Server (IIS) >
Internet Information Services (IIS) Manager
Next pane:
Sites > Default Web Site > Your Web Site
Main pane:
IIS > Compression
Then comes the custom configuration for encrypting
javascript resources.
The problem is that the compression in IIS 7 completely
works on the mime types and by default there is a mismatch in the mime types
Go to
following location
C:\Windows\System32\inetsrv\config
Open applicationHost.config
The mimemap is as follows
<mimeMap fileExtension=".js" mimeType="application/javascript" />
So the
section in the staticTypes should be changed
<add mimeType="application/javascript" enabled="true" />
Doing the web.config way
–
We can add following section in the system.webserver section
<system.webServer>
<urlCompression doDynamicCompression="false" doStaticCompression="true"/>
More Information/References –
·
http://weblogs.asp.net/owscott/archive/2009/02/22/iis-7-compression-good-bad-how-much.aspx
·
http://www.west-wind.com/weblog/posts/98538.aspx