How to configure NginX to serve Cached Content only when Backend is down (5xx Resp. Codes)?

牧云@^-^@ 提交于 2020-01-03 08:45:12

问题


I've configured my system with NginX listening on port 80, serving static content and proxying dynamic requests to a backend server.

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.

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).

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.

I would appreciate any hints


回答1:


Take a look at proxy_cache_use_stale




回答2:


proxy_intercept_errors might be what you're looking for.

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors




回答3:


Unfortunately, this is a classic instance of the XY Problem where a question is asked about an attempted solution, Y, rather than the actual problem, X.

Solutions to the actual issue are given here: https://stackoverflow.com/a/52232860/891636



来源:https://stackoverflow.com/questions/16756271/how-to-configure-nginx-to-serve-cached-content-only-when-backend-is-down-5xx-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!