如何配置NginX仅在后端关闭(5xx响应代码)时才提供缓存内容? [英] How to configure NginX to serve Cached Content only when Backend is down (5xx Resp. Codes)?
问题描述
我已使用NginX在端口80上侦听系统,以提供静态内容并将动态请求代理到后端服务器.
I've configured my system with NginX listening on port 80, serving static content and proxying dynamic requests to a backend server.
我可以配置NginX缓存后端生成的内容,但是我希望仅当Backend
响应错误http 5xx
或它完全崩溃时才提供此缓存的内容.
I can configure NginX to cache content generated by the backend, but I want this cached content be served only when the Backend
responds with an error http 5xx
, or when it's totally down.
我们尝试了 proxy_cache_use_stale 选项,最长使用时间为1秒,它起作用了,但它有一个不利的方面..这只是在此1秒钟的缓存老化内容期间,从缓存处理了数十个请求.这些从缓存提供的请求将丢失进一步的Backend
处理(例如统计信息).
We tried the proxy_cache_use_stale option with max-age of 1 second, it worked but it has one negative side.. which is simply dozens of requests being served from cache during this 1 second cache-aged-content. These requests served from cache will miss further Backend
processing (Stats for example).
我们只能承受这种负面因素的影响 IF 后端故障,
因此,缓存将充当备份或故障转移解决方案.但是,只要backend
启动并响应,就不应从缓存中满足任何请求.
We can only afford to live with this negativity IF the backend was down,
Thus, the cache will act as a backup or a failover solution. But as long as the backend
is up and responding, no requests should be served from cache.
任何提示,我都会感激
推荐答案
看看 查看全文