在 IIS (7+) 下:
在 IIS 管理器中,选择您的网站,双击 HTTP 响应标头,然后选择操作 Set Common Headers。
这将打开对话框窗口设置通用 HTTP 响应标头。
如果您选中使网页内容过期并选择立即,这会将Cache-Control: no-cache发送到浏览器以获取所有文件。
Download 并安装 IIS 的 URL 重写扩展。这将添加 URL 重写 功能。
添加一个新的空白规则并设置如下:
Name: ResponseNoCache (or whatever You like)
Match: Server Variable
Variable Name: RESPONSE_CACHE_CONTROL
Variable value: Matches the pattern
Using: Wildcards
Pattern: *
Action type: Rewrite
Value: no-cache, no-store, must-revalidate
现在,为了获得更多粒度,您可以设置前置条件。
点击Precondition,选择Create New Precondition...
例如,我可以按文件扩展名过滤:
Name: NoCacheRequestFiles (or whatever You like)
Using: Regular Expressions
点击Add..按钮,Add Condition对话框窗口将会打开。
Condition input: {REQUEST_FILENAME}
Check if input string: Matches the pattern
Pattern: \.html|\.js|\.css
之后,您需要点击Apply操作并重新启动 IIS。
web.config 然后看起来像这样:
<rewrite>
<outboundRules>
<rule name="ResponseNoCache" preCondition="NoCacheRequestFiles" patternSyntax="Wildcard">
<match serverVariable="RESPONSE_CACHE_CONTROL" pattern="*" />
<action type="Rewrite" value="no-cache, no-store, must-revalidate" />
</rule>
<preConditions>
<preCondition name="NoCacheRequestFiles">
<add input="{REQUEST_FILENAME}" pattern="\.html|\.js|\.css" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
现在,您可以在 Chrome 开发者工具(或任何您喜欢的工具)中检查 IIS 是否有效地发送了 Cache-Control: no-cache, no-store, must-revalidate 响应标头。
附加信息:
我也在使用静态和动态压缩。因此,我注意到 IIS 正在缓存 g 压缩文件,具体取决于两个附加参数:
- frequentHitThreshold
- frequentHitTimePeriod
我没有进一步研究压缩内容和缓存控制标头之间的交互,因为整体是一个相当频繁变化的complex topic 和hotfixes。但是,如果此解决方案也适用于移动设备浏览器,那么很高兴听到您的反馈。