仅为所选文件定义特定的缓存控制标头 [英] Define specific cache control header for selected file only
问题描述
我正在为Gatsby设置Nginx服务器(版本1.17.1),以遵循 https://www.gatsbyjs.org/docs/caching/.
I'm setting up a Nginx sever (version 1.17.1) for Gatsby following up the recommendation at https://www.gatsbyjs.org/docs/caching/.
下面的代码段是我的 server {}
块的一部分尝试实施建议的缓存配置;
The snippet below is the portion my server {}
block attempting implementing the recommended caching configuration;
location ~* \.(?:html)$ {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location /static {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location ~* \.(?:css|js)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /sw\.js {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
同样尝试使用 if语句代替 location {}
块,用于定义服务工作程序文件
Equally tried an if statement in place of the location {}
block for defining cache configuration for the service worker file, sw.js
, as below;
if ($request_uri ~ ^sw\.(?:js)$) {
set $no_cache 1;
}
不幸的是,除sw.js
以外,所有文件都已按预期成功缓存.
Unfortunately, all files get cached successfully as expected except sw.js
.
我在做什么错了,我该如何解决才能有效地将sw.js
的缓存控制标头设置为public, max-age=0, must-revalidate
?
What am I doing wrong and how can I fix it so as to effectively set cache control header for sw.js
to public, max-age=0, must-revalidate
?
推荐答案
location
的优先顺序在此处找到精确匹配项(使用=
修饰符)后,搜索将终止,并且不会检查正则表达式,因此可以将其用于sw.js
:
When an exact match is found (using the =
modifier) the search terminates and regular expressions will not be checked, so you can use that for your sw.js
:
location = /sw.js {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
这篇关于仅为所选文件定义特定的缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!