Scenario / Questions
Suppose I have this nginx config file
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=myCache:8m max_size=100m inactive=1h;
- What does the
keys_zone=myCache:8mmean? There’s not much said to it in the docs:
In addition, all active keys and information about data are stored in a shared memory zone, whose name and size are configured by the keys_zone parameter.
12h) is higher than the specified
proxy_cache_path? Which rule will it follow? e.g.
proxy_cache_valid 200 302 12h; proxy_cache_valid 404 302 1h;
Find below all possible solutions or suggestions for the above questions..
What does the keys_zone=myCache:8m mean?
As documentation said, nginx will keep all active keys and information about data are stored in a shared memory zone, whose
size are configured by the keys_zone parameter. As a matter of completeness, lets break down per part
/var/cache/nginxis the place where the actual cache stored. Inside the folder, cache file was binary file but you can easily spot the html tag inside it.
levels=1:2is levels parameter sets the number of subdirectory levels in cache.
keys_zone=myCache:8mwas defining shared memory zone named myCache with maximum size 8 MB. It holds all active keys and metadata of the cache. So, whenever nginx checks if a page was cached, it consults the shared memory zone first, then seek the location of actual cache in
/var/cache/nginxif cache exist.
max_sizewas maximum size of cache e.g. files size on
inactive=1hspecify maximum inactive time cache can be stored. Cached data that are not accessed during the time specified by the
inactiveparameter get removed from the cache regardless of their freshness.
How cache validation and deletion works
Taken from nginx mailing lists
Directive proxy_cache_valid specifies how long response will be considered valid (and will be returned without any requests to backend). After this time response will be considered “stale” and
either won’t be returned or will be depending on proxy_cache_use_stale setting.
Argument inactive of proxy_cache_path specifies how long response will be stored in cache after last use. Note that even stale responses will be considered recently used if there are requests to them.
As I understand, here the pseudocode how nginx works
When request coming
if cache.exist AND (now() - cache.first_retrieved) < proxy_cache_valid: use it else: retrieve from backend replace the old ones
In other process, the cache manager perform this logic
if (now() - cache.last_used) > inactive: del cache if all-cache.size > max-size: del *the most inactive cache* until size < max-size
What if proxy_cache_valid(12h) is higher than the specified inactive value (1h) in proxy_cache_path?
As long as the request and accessed the particular cache, that cache object will still valid until 12h after the object put in cache. After that, cache was considered invalid, so nginx will fetch from backend and reset the valid timer. But if object was inactive (not accessed) more than one hour – even in 12h valid-cache-period -, nginx will delete it because of
Disclaimer: This has been sourced from a third party syndicated feed through internet. We are not responsibility or liability for its dependability, trustworthiness, reliability and data of the text. We reserves the sole right to alter, delete or remove (without notice) the content in its absolute discretion for any reason whatsoever.