Cloudflare不按预期caching

我想知道为什么Cloulflare不会caching某个静态js文件。

鉴于reuqest:

curl -I http://www.testlifeinuk.com/dist/lifeinuk.js 

标题返回如下所示:

 HTTP/1.1 200 OK Date: Mon, 12 Jan 2015 10:59:21 GMT Content-Type: application/javascript Connection: keep-alive Set-Cookie: __cfduid=d11a9f01292153436a211a9d807a3399b1421060361; expires=Tue, 12-Jan-16 10:59:21 GMT; path=/; domain=.testlifeinuk.com; HttpOnly X-Powered-By: Express Etag: W/"507660-1420797981000" Cache-Control: public, max-age=1382400 Last-Modified: Fri, 09 Jan 2015 10:06:21 GMT Vary: Accept-Encoding Via: 1.1 vegur CF-Cache-Status: MISS Expires: Wed, 28 Jan 2015 10:59:21 GMT Server: cloudflare-nginx CF-RAY: 1a78d818b4af0b81-LHR 

令我感到困惑的是,不pipe我做了多less次这个请求,它总是回来

 CF-Cache-Status: MISS 

任何想法为什么?

您必须在CloudFlarepipe理面板中设置页面规则。 使用页面规则的第一步是定义一个模式来定义规则何时被触发。 这些模式可以很简单,比如一个URL,或者包含多个通配符的复杂模式。 想象一下,你有一个内容pipe理系统只有一个URL:

 http://www.testlifeinuk.com 

现在,如果您尝试使用URL模式创build规则:

 http://www.testlifeinuk.com 

只有主页将被caching!

如果你想caching整个网站,你必须以这种方式使用通配符:

  *testlifeinuk.com/* 

这样所有的页面和资源将被Cloudflarecaching,如下所示:

 http://www.testlifeinuk.com/dist/lifeinuk.js http://www.testlifeinuk.com/mypage.html https://www.testlifeinuk.com/mypage.html https://testlifeinuk.com/mypage.html 

而标题的结果将是:

 HTTP/1.1 200 OK Date: Mon, 25 May 2015 10:21:05 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.4.39-0+deb7u2 Expires: Mon, 25 May 2015 10:51:05 GMT Cache-Control: public, max-age=1800 Pragma: no-cache Vary: Accept-Encoding CF-Cache-Status: HIT <<<<< Look this parameter Server: cloudflare-nginx CF-RAY: 1ec082ee43150e60-MXP Content-Encoding: gzip 

问候

除了@Michelangelo提到的有关您需要首先设置CloudFlare页面规则,您还需要确保您正确设置响应标头,以便您不会覆盖默认的caching行为:

如果Cache-Control头设置为“private”,“no-store”,“no-cache”或“max-age = 0”,或者响应中有cookie,则CloudFlare不会caching资源。 否则,如果Cache-Control头部设置为“public”,并且“max-age”大于0,或者将来任何时候设置Expires头部,我们将caching资源。

请参阅: https : //support.cloudflare.com/hc/en-us/articles/202775670- How-Do-I-Tell-Cloudflare-What-to-Cache-

这就是为什么@Rudy李说,不要忘记设置maxAge选项,因为在Express中默认maxAge是0。

请参阅: https : //expressjs.com/en/api.html#express.static