Welcome! Log In Create A New Profile

Advanced

which phase to read cache file metadata "date"

January 14, 2024 05:55AM
context/problem:

I'm using nginx + openresty/lua but the openresty part is not that relevant, I would like to hook into nginx to get the cache file date (https://github.com/nginx/nginx/blob/f255815f5d161fab0dd310fe826d4f7572e141f2/src/http/ngx_http_upstream.c#L3184, https://github.com/nginx/nginx/blob/f255815f5d161fab0dd310fe826d4f7572e141f2/src/http/ngx_http_upstream.c#L2651) every time it's set/updated. once i get the cache date, i send it to some other service for processing (using cosockets.. but not very important)

I've managed to update nginx to expose this value and I've been trying to get it from different phase and facing different issues.

log phase: cache date is set by then but runs too late for my purposes, I want to get it as soon as possible

header phase: cache date is not set by then

body filter phase: seems to run immediately after cache date is set which is what I want but doesn't run if the response doesn't contain a body. In my configuration, it's possible to cache responses with no body (no content length specified, 304 responses, etc..)

questions:
1. Is there a way to configure nginx to run body filter phase even if there is no body so I can hook into it from lua land and get the cache date and send it to my other service for processing?
2. Is it possible the cache ts is not finalized/can change after body filter phase runs? In some cases, I'm seeing results which make me thing that the cache ts is changing after body filter phase runs. For a concrete example, in the body filter phase, for the first chunk only (since body filter phase can run multiple times for each chunk). I get the cache file metadata and print it along with other values. And here is what i see:
1. cache ts/date (from cache file metadata): 1705228467
2. valid_sec (from metadata): 1705228554
3. cache ttl (based on cache-control): 30 seconds
4. body filter phase start ts: 1705228524.867
5. cache status: REVALIDATED
6. response status: 200

notice that valid_sec - cache ts = 87 seconds which is beyond the 30 sec ttl. This suggests the cache ts has not been updated and may actually be the previous cache ts when the asset was first cached. This can further be proven that body filter phase start ts + 30 seconds = valid_sec so had the cache ts been updated, it would have had a similar value as the body filter phase start ts. Is there some gotcha I'm not aware of especially with revalidated?

thanks,

Zack
Subject Author Posted

which phase to read cache file metadata "date"

zack3123 January 14, 2024 05:55AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 146
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready