【问题标题】:How do I set the cachability of static files in IIS?如何在 IIS 中设置静态文件的可缓存性?
【发布时间】:2008-11-13 16:29:23
【问题描述】:

我在基于 IIS 6 的网站上的文件夹中有一些静态图像,我希望尽可能少地下载这些图像(以节省带宽)。我已将内容过期设置为 30 天后过期。我可以在 IIS 中做些什么来尝试最大化浏览器、代理和网关缓存的缓存吗?

比如加一个Cache-Control头?还有什么?

【问题讨论】:

    标签: iis iis-6 cache-control content-expiration


    【解决方案1】:

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

    这是一篇博文,涵盖以下内容:

    1. 允许覆盖静态内容设置
    2. 使用以下命令设置缓存设置
    3. 在客户端缓存

    【讨论】:

    • 好答案,但我想我应该指定 IIS6
    【解决方案2】:

    这是我对这个问题的回答:"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.
    )
    

    这会将 Web 根目录中每个 CSS 文件的缓存值设置为 5 天,然后像这样运行它:

    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
    

    有点痛苦,但可行。

    顺便说一句 - 要获取 HttpExpires 的值,请在 GUI 中设置值,然后运行 ​​

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

    得到你需要的实际值

    【讨论】: