【发布时间】:2020-02-23 20:22:30
【问题描述】:
我正在尝试为上游服务器(我不管理)设置缓存。大多数文件都可以缓存(并且没有设置Cache-Control),这些都可以正常工作。
但是,服务器上的某些位置是目录列表(并且有Cache-Control: no-store)。我只想在服务器无法访问时缓存它们。
不幸的是,我最终会出现以下情况之一:
- 在没有缓存这些列表的情况下(缓存中没有文件,标题总是显示缓存未命中)。如果服务器无法访问,则(显然)不会返回目录列表
- 在这些列表被缓存但之后永远不会更新的情况下(至少在缓存有效时不会更新)。由于我想长时间缓存所有其他条目,因此目录列表很快就会过时。
我尝试将标题修改为stale-if-error,但这似乎也没有帮助。
map $http_cache_control $http_updated_cache_control {
no-store stale-if-error;
}
server {
...
location /somewhere {
sendfile on;
sendfile_max_chunk 10m;
tcp_nopush on;
proxy_cache keyzone;
# allow using stale requests in case of errors or when updating a file
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_revalidate on;
proxy_cache_background_update on;
# add header to indicate if caching works
add_header X-Cache-Status $upstream_cache_status;
proxy_cache_lock on;
proxy_read_timeout 900;
proxy_pass_header Server;
proxy_ignore_headers Set-Cookie;
# allow caching of non-cacheable entries only when the server is erroring
proxy_hide_header Cache-Control;
add_header Cache-Control $http_updated_cache_control;
# don't ignore the cache control header: some items (like directory listings) are marked as "don't cache")
#proxy_ignore_headers Cache-Control;
}
}
如何使用Cache-Control: no-store 缓存条目,但仅在上游服务器关闭时才使用缓存的条目?
【问题讨论】:
-
添加上游服务器和代理的响应头。 “(至少只要缓存有效)”是什么意思?
Cache-Control: no-store内容的缓存有效性如何定义? -
我的意思是:如果我将proxy_cache_valid设置为一个很大的值(最好是几天),我想要缓存的东西会被缓存很长时间。不幸的是,这意味着我只想在出现错误时缓存的项目也被缓存(和使用)10 天。如果我把它设置为一个非常短的值,那么我要缓存的项目也不会缓存很长。