proxy_cache_path /app/cache/ui levels=1:2 keys_zone=ui:10m max_size=1g inactive=30d;
server {
...
location /app/ui/config.json {
proxy_cache ui;
proxy_cache_valid 1d;
proxy_ignore_headers Expires;
proxy_hide_header Expires;
proxy_hide_header Cache-Control;
add_header Cache-Control "max-age=1, public";
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Cache-Date $upstream_http_date;
proxy_pass http://app/config.json;
}
}
但是当上游服务器关闭时,缓存将不起作用,客户端只会得到504网关超时错误。我已经阅读了以下文章:https://nginx.org/ru/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale,如何配置NginX仅在后端关闭(5xx Resp. Codes)时提供缓存内容?,https://serverfault.com/questions/752838/nginx-use-proxy-cache-if-backend-is-down。但这并没有达到我预期的效果。非常感谢您的帮助。
proxy_connect_timeout
和proxy_cache_use_stale timeout
对我很有帮助,现在我终于得到了最初想要的结果。非常感谢! - Anatoli