Possible error on revalidate in ngx_http_upstream

Jiří Setnička jiri.setnicka at cdn77.com
Thu Oct 7 16:37:11 UTC 2021


Hello,

I use nginx as a proxy with enabled cache. I encountered strange 
behavior on revalidate.

When upstream does not return any caching headers it is ok - file is 
cached with default cachetime and on revalidate the r->cache->valid_sec 
is updated to now + default cachetime.

Also when upstream consistently returns caching headers it is still ok - 
file is cached according to caching headers and on revalidate the 
r->cache->valid_sec is updated by value from 304 response caching headers.

Problem is when upstream previously returned absolute caching headers on 
200 response (so the file is cached according to these headers and these 
headers are saved into cache file on disk) but later it changed its 
behavior and on 304 response it does not return any caching headers.
In such case, I would expect that now + default cachetime would be used 
as the new r->cache->valid_sec, but old absolute time is used instead 
and this yields in revalidate on each request.

In ngx_http_upstream_test_next(...) in revalidate part there is firstly 
cache time from upstream 304 response saved to temporal variable (valid 
= r->cache->valid_sec) and then request is reinited and 
r->cache->valid_sec is set according to headers in the cached file.
Problem is when value == 0 (no caching info from upstream) and there is 
an absolute time in the cached file headers.

This patch should fix this behavior - time computed from cached file is 
used only when it is in the future otherwise, time calculated by 
ngx_http_file_cache_valid(...) is used.

Thanks for your feedback
Jiri Setnicka


# HG changeset patch
# User Jiří Setnička <jiri.setnicka at cdn77.com>
# Date 1633624103 -7200
#      Thu Oct 07 18:28:23 2021 +0200
# Node ID 7149a1553b48a7403a8b8cea09580b103aab23b1
# Parent  ae7c767aa491fa55d3168dfc028a22f43ac8cf89
Do not use cache->valid_sec in the past from cached file when revalidating

diff -r ae7c767aa491 -r 7149a1553b48 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c      Wed Oct 06 18:01:42 2021 +0300
+++ b/src/http/ngx_http_upstream.c      Thu Oct 07 18:28:23 2021 +0200
@@ -2606,7 +2606,7 @@
              rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
          }

-        if (valid == 0) {
+        if (valid == 0 && r->cache->valid_sec >= now) {
              valid = r->cache->valid_sec;
              updating = r->cache->updating_sec;
              error = r->cache->error_sec;



More information about the nginx-devel mailing list