How do I set the cachability of static files in IIS?

雨燕双飞 提交于 2019-12-04 14:07:18

问题


I have some static images in a folder on my IIS 6-based website that I want to be downloaded as little as possible (to preserve bandwidth). I've set the Content Expiration to expire after 30 days. Is there anything else I can do in IIS to try to maximize the caching by browsers, proxy, and gateway caches?

Such as adding a Cache-Control header? Anything else?


回答1:


http://www.galcho.com/Blog/post/2008/02/27/IIS7-How-to-set-cache-control-for-static-content.aspx

This is a blog post covering the following:

  1. Allow overriding static content setting
  2. set cache settings using following commands
  3. Cache it on client



回答2:


Here's my answer from this question: "Expires" in http header for static content? how-to

@ECHO OFF 
REM ---------------------------------------------------------------------------
REM Caching - sets the caching on static files in a web site
REM syntax 
REM     Caching.CMD 1 d:\sites\MySite\WWWRoot\*.CSS
REM 
REM   %1 is the WebSite ID
REM   %2 is the path & Wildcard - for example, d:\sites\MySite\WWWRoot\*.CSS
REM   _adsutil is the path to ADSUtil.VBS
REM ---------------------------------------------------------------------------

SETLOCAL
REM *******
REM SET THIS TO POINT TO adsutil.vbs - TYPICALLY c:\inetpub\adminscripts\adsutil.vbs
REM *******
SET _adsutil=D:\Apps\Scripts\adsutil.vbs

FOR %%i IN (%2) DO (
  ECHO Setting Caching on %%~ni%%~xi
  CSCRIPT %_adsutil% CREATE W3SVC/%1/root/%%~ni%%~xi "IIsWebFile"
  CSCRIPT %_adsutil% SET    W3SVC/%1/root/%%~ni%%~xi/HttpExpires "D, 0x69780"
  ECHO.
)

Which sets the caching value for each CSS file in a web root to 5 days, then run it like this:

Caching.CMD 1 \site\wwwroot\*.css
Caching.CMD 1 \site\wwwroot\*.js
Caching.CMD 1 \site\wwwroot\*.html
Caching.CMD 1 \site\wwwroot\*.htm
Caching.CMD 1 \site\wwwroot\*.gif
Caching.CMD 1 \site\wwwroot\*.jpg

Kind of painful, but workable.

BTW - to get the value for HttpExpires, set the value in the GUI, then run

AdsUtil.vbs ENUM W3SVC/1/root/File.txt

to get the actual value you need



来源:https://stackoverflow.com/questions/287414/how-do-i-set-the-cachability-of-static-files-in-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!