5.1 Caching Zones

Problem

You need to cache content and need to define where the cache is

stored.

问题

需要定义响应内容的缓存路径及缓存操作


Solution

Use the proxy_cache_path directive to define shared memory cache

zones and a location for the content:

proxy_cache_path /var/nginx/cache
            keys\_zone=CACHE:60m

            levels=1:2

            inactive=3h

            max\_size=20g;
proxy_cache CACHE;

The cache definition example creates a directory for cached respon‐

ses on the filesystem at /var/nginx/cache and creates a shared mem‐

ory space named CACHE with 60 megabytes of memory. This

example sets the directory structure levels, defines the release of

cached responses after they have not been requested in 3 hours, and

defines a maximum size of the cache of 20 gigabytes. The

proxy_cache directive informs a particular context to use the cache

zone. The proxy_cache_path is valid in the HTTP context, and the

proxy_cache directive is valid in the HTTP, server, and location

contexts.

解决方案

使用 proxy_cache_path 指令为待缓存定义内容缓存区域的共享内存及缓存路径:

proxy_cache_path /var/nginx/cache
            keys\_zone=CACHE:60m

            levels=1:2

            inactive=3h

            max\_size=20g;
proxy_cache CACHE;

上面的配置中在 proxy_cache_path 指令中为响应在文件系统中定义了缓存的存

储目录 /var/nginx/cache,并使用 keys_zone 参数创建名为 CACHE 的拥有

60 M 的缓存内存空间;同时通过 levels 参数定义目录解构级别,通过 inactive

参数指明如果相同请求的缓存在 3 小时内未被再次访问则被释放,并使用 max_size

定义了缓存最大可用存储空间为 20 G。


Discussion

To configure caching in NGINX, it’s necessary to declare a path and

zone to be used. A cache zone in NGINX is created with the direc‐

tive proxy_cache_path. The proxy_cache_path designates a loca‐

tion to store the cached information and a shared memory space to

store active keys and response metadata. Optional parameters to this

directive provide more control over how the cache is maintained

and accessed. The levels parameter defines how the file structure is

created. The value is a colon-separated value that declares the length

of subdirectory names, with a maximum of three levels. NGINX

caches based on the cache key, which is a hashed value. NGINX then

stores the result in the file structure provided, using the cache key as

a file path and breaking up directories based on the levels value.

The inactive parameter allows for control over the length of time a

cache item will be hosted after its last use. The size of the cache is

also configurable with use of the max_size parameter. Other param‐

eters are in relation to the cache loading process, which loads the

cache keys into the shared memory zone from the files cached on

disk.

结论

要使用 NGINX 内容缓存,需要在配置中定义缓存目录及缓存区域(zone)。

通过 proxy_cache_path 指令创建 NGINX 内容缓存,定义用于缓存信息的路

径和用于存储缓存的元数据(metadata)和运行时键名(active keys)的共享内存。

其它的可选参数,还提供缓存如何维护和访问的控制,levels 参数定义如何

创建文件结构,定义子目录的文件名长度,语法是以冒号分隔的值,支持最大

3 级。

NGINX 的所有缓存依赖于最终被计算成散列的 cache key,接着将结果以

cache key 作为文件名,依据缓存级别创建缓存目录。

inactive 参数用于控制最后一次使用缓存选项的时间,超过这个时间的缓存

会被释放。缓存的大小则可以通过 max_size 参数进行配置。还有部分参数

作用于缓存加载进程中,功能是将 cache keys 从磁盘文件加载仅共享内存里。

results matching ""

    No results matching ""