Gonzalioz
Gonzalioz

Reputation: 622

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.

Upvotes: 1

Views: 567

Answers (1)

Gonzalioz
Gonzalioz

Reputation: 622

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.

Upvotes: 1

Related Questions