我使用以下 Web.config 在 Azure 上托管一个 Web 应用程序:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".text" mimeType="text/plain" />
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
这可行,但我想通过扩展来改变缓存年龄。我想将 .html 文件的最长期限设置为 1 天,将其他所有文件的最长期限设置为 365 天。原因是 html 中的所有资源的文件名都会在更改时更新,并由 CDN 提供服务,因此它们永远不需要过期,但 html 页面本身需要始终保持新鲜状态,以便用户可以看到新内容。
我看到 <location>
元素允许将 Web.config 过滤到特定位置,但我没有找到将其限制为某些扩展名的方法。
请注意,我不要求能够在 Web.config 中执行此操作:使用 Azure Web 应用程序的任何可能方法都可以。
最佳答案
正如其他人提到的那样,这是不可能的,所以我想建议一种解决方法来完成这项工作。
您可以通过创建出站规则来替换 html 文件的 Cache-Control
header ,从而利用 URL 重写模块的功能。
这是配置。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".text" mimeType="text/plain" />
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<rewrite>
<outboundRules>
<rule name="RewriteCacheControlForHTMLFiles" preCondition="FileEndsWithHtml">
<match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
<action type="Rewrite" value="max-age=86400" />
</rule>
<preConditions>
<preCondition name="FileEndsWithHtml">
<add input="{REQUEST_FILENAME}" pattern="\.html$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
我的测试截图:
关于azure - 如何将 <clientCache/> 设置应用于 IIS 中的特定扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36783181/