[nginx] Upstream: improved stale-if-error handling.
Maxim Dounin
mdounin at mdounin.ru
Sat Jan 31 06:36:55 UTC 2026
details: http://freenginx.org/hg/nginx/rev/d6723d4c4b81
branches:
changeset: 9462:d6723d4c4b81
user: Maxim Dounin <mdounin at mdounin.ru>
date: Sat Jan 31 09:27:12 2026 +0300
description:
Upstream: improved stale-if-error handling.
Following 7702:7015f26aef90, "Cache-Control: stale-if-error=..." is
ignored for HTTP 4xx and 5xx responses, even if these are expected to
be treated as errors per u->conf->next_upstream configuration, notably
when switching to the next upstream server wasn't possible.
This makes it non-trivial to configure things with stale response usage
for a limited time per stale-if-error as long as an actual error is detected
not by the cache itself, but by an intermediate proxy, even if the
particular status code is configured to be mostly equivalent to network
errors, such as with "proxy_next_upstream error timeout http_502".
With this change, as long as the particular error is considered to be
a failure per u->conf->next_upstream, stale-if-error information from
the cached response is now used.
diffstat:
src/http/ngx_http_upstream.c | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)
diffs (14 lines):
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2626,7 +2626,9 @@ ngx_http_upstream_test_next(ngx_http_req
#if (NGX_HTTP_CACHE)
if (u->cache_status == NGX_HTTP_CACHE_EXPIRED
- && (u->conf->cache_use_stale & un->mask))
+ && ((u->conf->cache_use_stale & un->mask)
+ || ((u->peer_state & NGX_PEER_FAILED)
+ && r->cache->stale_error)))
{
ngx_int_t rc;
More information about the nginx-devel
mailing list