大网数据广告活动
服务器运维问答

Understanding the nginx proxy_cache_path directive

· 52CE 我爱测

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…

问题原文

原帖链接 · Jürgen Paul · CC BY-SA 3.0

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!

社区高分解答

原帖链接 · masegaloeh · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-03-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。