在 Azure 网站上覆盖 Cache-Control Header
Overriding Cache-Control Header on Azure Websites
我在 Microsoft Azure 上托管了一个 Ghost 博客。我想确保我可以自己设置 Cache-Control
header,因为现在它的 max-age
设置为 0.
我解决这个问题的方法是删除自定义 web.config
(因为 Azure 网站使用 IIS)。这是我拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
</modules>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
<customHeaders>
<remove name="Cache-Control" />
<add name="Cache-Control" value="public, max-age=99" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
但是,这实际上并没有删除原来的 Cache-Control
header,而是将我的值附加到它上面:
对我所缺少的有什么见解吗?
这个问题的解决方案比我原先想象的要简单。当我在 web.config
中正确设置 headers 时,服务器 node.js 配置正在执行其自己的 Cache-Control
规则,该规则覆盖了我的规则。
在 \core\server\middleware
中有一个名为 cache-control.js
的文件。里面有一个片段:
cacheControl = function cacheControl(options) {
/*jslint unparam:true*/
var profiles = {
public: 'public, max-age=0',
private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
},
output;
我需要做的就是将 'public, max-age=0'
调整为我想要的值。
这完全消除了 web.config
的需要。
我在 Microsoft Azure 上托管了一个 Ghost 博客。我想确保我可以自己设置 Cache-Control
header,因为现在它的 max-age
设置为 0.
我解决这个问题的方法是删除自定义 web.config
(因为 Azure 网站使用 IIS)。这是我拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
</modules>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
<customHeaders>
<remove name="Cache-Control" />
<add name="Cache-Control" value="public, max-age=99" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
但是,这实际上并没有删除原来的 Cache-Control
header,而是将我的值附加到它上面:
对我所缺少的有什么见解吗?
这个问题的解决方案比我原先想象的要简单。当我在 web.config
中正确设置 headers 时,服务器 node.js 配置正在执行其自己的 Cache-Control
规则,该规则覆盖了我的规则。
在 \core\server\middleware
中有一个名为 cache-control.js
的文件。里面有一个片段:
cacheControl = function cacheControl(options) {
/*jslint unparam:true*/
var profiles = {
public: 'public, max-age=0',
private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
},
output;
我需要做的就是将 'public, max-age=0'
调整为我想要的值。
这完全消除了 web.config
的需要。