Nginx content cache 

Nginx内容缓存

This chapter describes how to enable and configure caching responses received from proxied servers. When caching is enabled NGINX saves responses in the cache on the disk and uses them to respond to clients without proxying the requests.

本章来讨论一下如何开启和配置缓存从代理服务器接受的响应。缓存开启之后Nginx把在磁盘里的响应缓存直接返回给客户端,不需要再代理相同的请求。

Enabling the Cache of Responses 
开启响应缓存

To enable caching configure the path to the cache and other parameters using the  proxy_cache_path directive. Then place the  proxy_cache directive in the context where you want caching to be enabled:

通过  proxy_cache_path 指令配置缓存路径及其他参数以开启缓存。然后把   proxy_cache 指令放在你需要开启缓存的上下文中:


http {
    ...
    proxy_cache_path /data/nginx/cache keys_zone=one:10m;

    server {
        proxy_cache one;
        location / {
            proxy_pass http://localhost:8000;
        }
    }
}


 

Note that the  proxy_cache_path directive can be specified only on the http level. It has two mandatory parameters: the path on the file system where cached responses will be stored, and the name and size of the shared memory zone defined by the  keys_zone parameter. The same name is specified in the  proxy_cache directive.

注意 proxy_cache_path 这个指令只能在 http 层指定。它有两个必须的参数:缓存响应存储在文件系统中的路径,以及由参数 keys_zone 指定的共享内存空间的名字和大小。在 proxy_cache 指令中指定的名字要与 proxy_cache_path指定的内存空间名字相同。

The shared memory zone is used to store meta information on cached items. However, its size does not limit the total size of the cached responses. Cached responses themselves are stored with the copy of the meta information in specific files on the file system. You can limit the size of this file storage with the  max_size parameter. However, the actual size of the file storage can temporarily exceed this until a process called  cache manager checks the cache size and removes the least recently used cached responses and their metadata.

共享内存区用于存储缓存项里的元数据。然而,它的大小并不能限制缓存的响应的总大小。缓存的响应本身以元信息的拷贝存储在文件系统中指定的文件里。你可以通过 max_size 参数限制文件存储器的大小。然而,文件存储器的实际大小也会临时性的超过这个设置,直到缓存管理进程来检查缓存大小并且把最近最少用到的响应缓存及其元数据删除。

Caching Processes 
缓存过程

There are two additional NGINX processes involved in caching, the  cache loader and the cache manager.

还有另外两个Nginx 进程参与缓存,cache loader 和 cache manager。

The  cache manager is activated periodically to check the state of the cache file storage. In particular, it removes the least recently used data when the size of the file storage exceeds the  max_size parameter.

缓存管理器定期激活检查缓存文件存在器的状态。特别地,当文件存储器的大小超过 max_size 参数的值时,把最近最少使用到的数据删除。

The  cache loader is activated only once, right after NGINX starts. It loads the meta information about the previously cached data into the shared memory zone. Loading the whole cache at once may consume a considerable amount of resources and slow nginx’s performance during the first minutes. This is why the  cache loader works in iterations configured with parameters of the  proxy_cache_path directive.

缓存加载器只激活一次,在Nginx启动之后。它加载先前缓存的数据的元信息到共享内存区。一次性在头一分钟内加载所有的缓存可能会消耗大量的资源和减慢 Nginx 的性能。这就是为何缓存加载器要依据 proxy_cache_path 指令的配置参数迭代的工作。

Each iteration lasts no longer than a  loader_threshold value specified in milliseconds (by default, 200). During one iteration the cache loader can load no more than the  loader_files items s pecified (by default, 100). A pause between iterations is set with  loader_sleeps in milliseconds (by default, 50). For example, these parameters can be modified to speed up loading of the cache meta data:

每一次迭代用时不超为参数 loader_threshold 的值,该值以毫秒为单位(默认200)。每次迭代缓存加载器能加载的不超过 loader_files 项所设定的(默认100)。两个迭代的时间间隔以 loader_sleeps 指定,单位毫秒(默认50)。例如,这些参数可以修改以加速缓存元数据的加载:


proxy_cache_path /data/nginx/cache keys_zone=one:10m
                 loader_threshold=300 loader_files=200;


Specifying Which Requests to Cache 
设定需要缓存的请求

By default, NGINX caches all responses that have the GET and HEAD methods the first time such responses are received from a proxied server. As a key identifier of a request NGINX uses the request string. Whenever two requests have the same key they are considered equal and the same cached response is sent to the client. The  proxy_cache_key directive defines the way the key is calculated for a request and can be changed on the location, server, or http level:

默认情下,Nginx 把所有有GET和HEAD方法的响应缓存起来当这些响应第一次从被代理的服务器接收的时候。Nginx使用请求字符串作为一个请求的身份标识键。任何时候当两个请求有相同的键被认为是一样的并且返回同一个缓存的响应发送给客户端。  proxy_cache_key 指令定义一个请求键的计算方法,这个指令能在location、server 和 http 修改:



proxy_cache_key "$host$request_uri$cookie_user";


It is possible to increase the minimum number of times a request with the same key should be cached by using the  proxy_cache_min_uses directive:

使用   proxy_cache_min_uses 指令设定一个键至少被请求多少次才能被缓存:

It is also possible to specify additional HTTP methods of the requests to cache:

同样可以设定请求的其他HTTP方法进行缓存:



proxy_cache_methods GET HEAD POST;



This setting enables caching of responses for requests that have the GET, HEAD, or POST method.

上述设置开启了对带有GET、HEAD或POST方法的请求的响应的缓存。

Limiting or Bypassing Caching 
限制或绕过缓存

By default, the time which a response is cached isn’t limited. When the cache file storage is exceeded it will be removed if it has been used less than other cached items. Otherwise, the response can be kept in the cache indefinitely.

默认情况下,一个响应缓存的时长没有限制。一个缓存的响应在缓存文件存储器超过设置大小且比其他响应缓存使用次数少的情况下被删除。否则,这个响应将会无期地保存在缓存中。

You can limit the time which responses with specific status codes are considered valid, by using the  proxy_cache_valid directive:

使用 proxy_cache_valid指令,能限制指定状态的响应的有效期:



proxy_cache_valid 200 302 10m;
proxy_cache_valid 404      1m;



In this example the responses with the 200 and 302 code will be valid in the cache for 10 minutes, and the responses with the 404 code will be valid for 1 minute. To set the storage time limit applied all status codes, specify  any in the first parameter:

这个例子中带着200和302状态码的响应在缓存中的有效期为10分钟,带着404的则为1分钟。如果要设置所有状态码的存储时间,第一个参数指定为any:



proxy_cache_valid any 5m;



To define conditions when the response is not taken from the cache (even if it may exist in the cache) use the  proxy_cache_bypass directive. Keep in mind that no conditions are specified by default. The directive may have one or several parameters, each may consist of a number of variables. If at least one parameter is not empty and does not equal “0”, NGINX will not look up the response in the cache. For example:

 proxy_cache_bypass 指令定义不从缓存中取响应(即使缓存中有该响应)的条件。记住默认是没有条件的。这个指令可以多个参数,每一个由一系列值组成。只要有一个参数不为空或者不为”0”,Nginx 将不会在缓存中查找响应,例如:



proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;



To define conditions where the response is not saved in the cache at all, use the  proxy_no_cache directives. The conditions are specified by the same rules as for proxy_cache_bypass:

指定不存储响应的条件使用  proxy_no_cache  指令,这个指令的条件规则跟 proxy_cache_bypass 一样:



proxy_no_cache $http_pragma $http_authorization;


Combined Configuration Example 
组合配置示例

The configuration sample below combines some of the different caching options described above.

下面的配置示例组合了上文描述过的一些不同的缓存选项:



http {
    ...
    proxy_cache_path /data/nginx/cache keys_zone=one:10m
                     loader_threshold=300 loader_files=200
                     max_size=200m;

    server {
        listen 8080;
        proxy_cache one;

        location / {
            proxy_pass http://backend1;
        }

        location /some/path {
            proxy_cache_valid any   1m;
            proxy_cache_min_uses 3;
            proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
            proxy_pass http://backend2;
        }
    }
}


 

This example defines a virtual server with two locations that use the same cache but with different settings.

这个例子定义了一个拥有两个使用相同缓存但是做了不同配置的location的虚拟服务。

It is assumed that responses from the  backend1 server rarely change and can be cached the first time a request is received and held for as long as possible.

该例假定从backend1服务器来的响应很少改变,所以第一次请求的响应可以缓存尽可能长的时间。

By contrast, responses from the  backend2 server are highly volatile, and therefore are cached only after three occurrences of the same request and held for one minute. Moreover, if a request satisfies the conditions of the  proxy_cache_bypass directive, the cache will not be searched for the response at all and NGINX will immediately pass the request to the backend.

相比之下,来自backend2服务器的响应则非常易变,所以只有在出现三次相同的请求之后才缓存并且只保存一分钟。此外,如果一个请求满足 proxy_cache_bapass 指令的条件,Nginx 不会在缓存里查找它的响应而是直接把请求发送给后端。