gpt4 book ai didi

nginx - 仅为所选文件定义特定的缓存控制 header

转载 作者:行者123 更新时间:2023-12-03 19:44:33 25 4
gpt4 key购买 nike

我正在为 Gatsby 设置 Nginx 服务器(版本 1.17.1),按照 https://www.gatsbyjs.org/docs/caching/ 上的建议进行操作.

下面的片段是我的 server {} 的部分阻止尝试实现推荐的缓存配置;

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 statement代替 location {} 用于定义 service worker 文件缓存配置的块, sw.js , 如下;
if ($request_uri ~ ^sw\.(?:js)$) {
set $no_cache 1;
}

不幸的是,除 sw.js 外,所有文件都按预期成功缓存。 .

我做错了什么以及如何修复它以便有效地为 sw.js 设置缓存控制 header 至 public, max-age=0, must-revalidate ?

最佳答案

我最终得到了以下有关 Gatsby.js 缓存的 nginx 配置:

        location ~* \.(?:html)$ {
add_header Cache-Control "public, max-age=0, must-revalidate";
}

location /page-data {
add_header Cache-Control "public, max-age=0, must-revalidate";
}

location = /sw.js {
add_header Cache-Control "public, max-age=0, must-revalidate";
}

location /static {
add_header Cache-Control "public, max-age=31536000, immutable";
}

location ~* \.(?:js|css)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}

@OP:您最终采用哪种配置?也许您可以编辑此答案以匹配完美的解决方案;对于搜索“缓存 nginx gatsby”的人。

关于nginx - 仅为所选文件定义特定的缓存控制 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com