• Nginx content cache Nginx内容缓存


    原文地址:http://nginx.com/resources/admin-guide/caching/

    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.

    本章来讨论一下怎样开启和配置缓存从代理server接受的响应。缓存开启之后Nginx把在磁盘里的响应缓存直接返回给client,不须要再代理同样的请求。

    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使用请求字符串作为一个请求的身份标识键。不论什么时候当两个请求有同样的键被觉得是一样的而且返回同一个缓存的响应发送给client。 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 GETHEAD, 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.

    该例假定从backend1server来的响应非常少改变,所以第一次请求的响应能够缓存尽可能长的时间。

    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.

    相比之下,来自backend2server的响应则很易变,所以仅仅有在出现三次同样的请求之后才缓存而且仅仅保存一分钟。此外,假设一个请求满足 proxy_cache_bapass 指令的条件。Nginx 不会在缓存里查找它的响应而是直接把请求发送给后端。




  • 相关阅读:
    [Scala] akka actor编程(一)
    随便说说
    [Java] Java执行Shell命令
    [Scala] Scala基础知识
    [Linux] 账户管理命令(二)
    [Linux] 账户管理命令(一)
    [Kerberos] Java client访问kerberos-secured cluster
    沟通与影响技术培训
    Python
    ML 基础知识
  • 原文地址:https://www.cnblogs.com/llguanli/p/7204170.html
Copyright © 2020-2023  润新知