IIS7: Add expires header or cache-control header
I am running a website. I am using IIS7 and Apache HTTP Server for my website. I recently noticed that a lot of load on my server due to continuous request from some users who visit my website regularly. This creates a massive load to my server and even increases log file size. I would like to reduce this request from the users side. Is this possible? I think I should cache some information at the users computers to decrease the load. But I am confused between what to use: expires header or cache-control header. Can you guide me in this matter?
Re: IIS7: Add expires header or cache-control header
A new visitor generates several HTTP requests to download all files from your website, but you can reduce this usage for regular visitors with the help of "Expires header". It allows you to cache those files. This stops unnecessary HTTP requests. But the point is that you should use it on all your components.
Code:
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Fri, 10 Mar 2011 20:00:00 GMT"
</FilesMatch>
The above code goes in your root .htaccess file.
Re: IIS7: Add expires header or cache-control header
With Cache-Control headers, you can define which proxies can cache your site content, and also determine how long files can be cached. You can also force to check if the cache is having the most recent information.
Code:
# 720 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290804000, public"
</FilesMatch>
# 3 DAYS
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=171800, public, must-revalidate"
</FilesMatch>
# 5 HOURS
<FilesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=9600, must-revalidate"
</FilesMatch>
The above code goes into your root .htaccess file
Re: IIS7: Add expires header or cache-control header
You can easily add the expires header in your web.config’s system.webServer section using IIS7:
Code:
<staticContent>
<clientCache httpExpires="Mon, 1 May 2010 05:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>
This allows all the static content to have an expires HTTP header set to 2010. The static content may refer to anything which is not served through ASP.NET engine such as photos, script files and stylesheets.