How do you set the expiry date or a maximum age in

2020-02-16 12:09发布

问题:

I am using IIS 6 and IIS 7 as a web server.

After running Google page speed online , it remarks that I should be: Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

And it lists a lot of plain images , my javascript files and the style sheets.

How can I set this expiry date for these static files ? I thought this was done automatically by the browser ?

回答1:

Leverage browser caching:

Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching

To set an expiry date or a maximum age in the HTTP headers for static resources

Open IIS manager-> Click your site-> Click the HTTP Response Headers in the home page-> Click the Set Common Headers on the Actions panel -> Check Expire Web Content -> Set After 7 days (as suggested for in the page speed analysis "Specify an expiration at least one week in the future for the following resources"

http://technet.microsoft.com/en-us/library/cc770661%28WS.10%29.aspx



回答2:

Sometimes the google tool complains if you don't set both the Cache-Control max-age and the Expires header, IIS just sets the max-age, which should be fine really. You can confirm by using chrome or firebug and looking at the Net Debug panel and inspecting the response headers.



回答3:

setting expiry date for static files should be done in Web.config file for IIS. I don't have much knowledge in ISS, but found this on web

http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/translate-htaccess-content-to-iis-webconfig

<caching>
   <profiles>
     <add extension=".php" policy="DisableCache" kernelCachePolicy="DisableCache" />
     <add extension=".html" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="14:00:00:00" />
   </profiles>
</caching>

The above code will enable caching for .html files for a maximum of 14 days. For .php files, ensure that no caching is performed.

For more refer http://www.iis.net/configreference/system.webserver/staticcontent/clientcache



标签: caching iis