要确保 AWS CloudFront 不缓存对象,可以通过以下两种方法之一来解决:
app.get('/path/to/your/object', function(req, res) {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
// other response headers and content
});
在这个示例中,Cache-Control
头的值设置为 'no-store, no-cache, must-revalidate, max-age=0'
。这将告诉 CloudFront 不要缓存该对象,并在每次请求时都从源服务器获取最新的内容。
aws cloudfront create-cache-policy --cache-policy-config file://cache-policy-config.json
在 cache-policy-config.json
文件中,您可以定义无缓存行为的路径模式。例如:
{
"CachePolicyConfig": {
"Name": "NoCache",
"Comment": "No caching policy",
"DefaultTTL": 0,
"MaxTTL": 0,
"MinTTL": 0,
"ParametersInCacheKeyAndForwardedToOrigin": {
"CookiesConfig": {
"CookieBehavior": "none"
},
"HeadersConfig": {
"HeaderBehavior": "none"
},
"QueryStringsConfig": {
"QueryStringBehavior": "none"
}
},
"CachePolicy": {
"Comment": "No caching policy",
"DefaultTTL": 0,
"MaxTTL": 0,
"MinTTL": 0,
"ParametersInCacheKeyAndForwardedToOrigin": {
"CookiesConfig": {
"CookieBehavior": "none"
},
"HeadersConfig": {
"HeaderBehavior": "none"
},
"QueryStringsConfig": {
"QueryStringBehavior": "none"
}
}
}
}
}
在这个示例中,无缓存行为被命名为 "NoCache"
,并且所有的缓存时间都被设置为 0
,以确保 CloudFront 不会缓存任何对象。
无论您选择哪种方法,都应该根据您的具体需求和应用场景来选择适合的解决方案。