我在Azure上使用以下Web.config托管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> element允许将Web.config过滤到特定位置,但我没有看到将其限制为某些扩展的方法.
请注意,我不需要在Web.config中执行此操作:使用Azure Web App可能的任何方法都可以.
解决方法
正如其他人所说,这是不可能的,所以我想建议一个解决方法来完成这项工作.
您可以通过创建出站规则来替换html文件的Cache-Control标头,从而利用URL重写模块的功能.
您可以通过创建出站规则来替换html文件的Cache-Control标头,从而利用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>
我测试的截图: