简体   繁体   中英

Nginx proxy_cache_background_update purge cache on origin error

When using proxy_cache_background_update on in conjunction with proxy_cache_use_stale updating clients will keep receiving stale content when the origin server returns errors.

Is it possible to purge a cache item when the background update receives an error from the origin server?

My goal with this is to improve response time, I do not want to keep serving stale content when the origin server is down. Only when updating cache.

While not the perfect solution. I was able to achieve this behavior by turning off proxy_cache_use_stale updating and have the origin server respond with stale-while-revalidate in the cache-control header instead.

For example; my origin server responds with header:

cache-control: public, max-age=60, stale-while-revalidate=60

When a client requests the resource after 1min (max-age=60), nginx will return a stale response for another minute (state-while-revalidate=60) while executing a background update request.

If a client requests the resource after 2min and it is still not updated, it will go directly to the origin and thus receive an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM