Welcome! Log In Create A New Profile

Advanced

Possible error on revalidate in ngx_http_upstream

Jiří Setnička
October 07, 2021 12:38PM
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@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;

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

Possible error on revalidate in ngx_http_upstream

Jiří Setnička 246 October 07, 2021 12:38PM

Re: Possible error on revalidate in ngx_http_upstream

Maxim Dounin 86 October 14, 2021 01:34PM

Re: Possible error on revalidate in ngx_http_upstream

Jiří Setnička 61 October 15, 2021 06:16AM

Re: Possible error on revalidate in ngx_http_upstream

Maxim Dounin 57 October 15, 2021 04:36PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 61
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready