Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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:8m mean? 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.

  • What if proxy_cache_valid(12h) is higher than the specified inactive value (1h) in proxy_cache_path? Which rule will it follow? e.g.

    proxy_cache_valid 200 302 12h;
    proxy_cache_valid 404 302 1h;
    

Thanks!

share|improve this question

1 Answer 1

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 name and size are configured by the keys_zone parameter. As a matter of completeness, lets break down per part

  • /var/cache/nginx is 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:2 is levels parameter sets the number of subdirectory levels in cache.
  • keys_zone=myCache:8m was 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/nginx if cache exist.
  • max_size was maximum size of cache e.g. files size on /var/cache/nginx.
  • inactive=1h specify maximum inactive time cache can be stored. Cached data that are not accessed during the time specified by the inactive parameter 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 inactive parameter.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.