Nginx content cache Nginx内容缓存

标签: nginx content cache | 发表时间:2014-11-06 23:53 | 作者:agangdi
出处:http://blog.csdn.net

原文地址: 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.

本章来讨论一下如何开启和配置缓存从代理服务器接受的响应。缓存开启之后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 不会在缓存里查找它的响应而是直接把请求发送给后端。




作者:agangdi 发表于2014-11-6 15:53:03 原文链接
阅读:39 评论:0 查看评论

相关 [nginx content cache] 推荐:

Nginx content cache Nginx内容缓存

- - CSDN博客推荐文章
原文地址: http://nginx.com/resources/admin-guide/caching/. 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 设置 expires and Cache-Control

- - 互联网 - ITeye博客
源:http://nginx.org/en/docs/http/ngx_http_headers_module.html. Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307.

开启nginx cache后导致内存几乎100%问题

- - 开涛的博客
1、前些日子某服务被刷,每分钟达到上几百万请求;当时采用了nginx cache来解决的;但是因为某服务不能缓存太久,当时设置了5s,那么带来的问题就是产生大量小文件,而且很快就删除了. 会发现used是27G;但是通过top查看进程占的内存并没有那么多. SReclaimable: 16474128 kB (这些是内核保持的但是可以释放的inode和dentry的缓存).

nginx cache静态化+tmpfs 高性能cdn方案

- - 开源软件 - ITeye博客
本文档主要分为3部分内容:. (1)       解决不同URL访问不同后端的nginx配置方法. (2)       Nginx cache和内存文件系统的配置方法. (3)       Proxy cache的详细配置内容. 2       匹配不同URL访问不同后端. 如果想通过访问不同类别URL分配到不同的后端通过nginx实现,首先举个例子,将需求场景进行描述:.

GitHub - winshining/nginx-http-flv-module: Media streaming server based on nginx-rtmp-module. In addtion to the features nginx-rtmp-module supplies, HTTP-FLV, GOP cache and VHOST are supported now.

- -
MUSTbe enclosed by quotation marks, or arguments in url will be discarded (some shells not so smart will interpret "&" as "run in background").. ngx_rtmp_stat_modulemay not get statistics from a specified worker process in multi-processes mode, for HTTP requests are randomly distributed to worker processes.

nginx cache静态化+tmpfs 高性能cdn方案 原创-胡志广

- - 开源软件 - ITeye博客
nginx cache静态化+tmpfs 高性能cdn方案 原创-胡志广. cachenginxproxy静态化. 本文档主要分为3部分内容:. (1)       解决不同URL访问不同后端的nginx配置方法. (2)       Nginx cache和内存文件系统的配置方法. (3)       Proxy cache的详细配置内容.

Guava cache

- - 孟飞阳的博客
Guava Cache是一个全内存的本地缓存实现,它提供了线程安全的实现机制. 整体上来说Guava cache 是本地缓存的不二之选,简单易用,性能好.    Guava Cache有两种创建方式:.   通过这两种方法创建的cache,和通常用map来缓存的做法比,不同在于,这两种方法都实现了一种逻辑——从缓存中取key X的值,如果该值已经缓存过了,则返回缓存中的值,如果没有缓存过,可以通过某个方法来获取这个值.

Java Cache系列之Guava Cache

- - BlogJava-首页技术区
然而作为工具库中的一部分,我们自然不能期待Guava对Cache有比较完善的实现. 因而Guava中的Cache只能用于一些把Cache作为一种辅助设计的项目或者在项目的前期为了实现简单而引入. 在Guava CacheBuilder的注释中给定Guava Cache以下的需求:. 对于这样的需求,如果要我们自己来实现,我们应该怎么设计.

巧用query cache

- - OurMySQL
   收到一用户反馈其应用日志中狂报错误,获取连接超时:. 同时应用报错超出了数据库的最大连接数:max connections:. 这种情况很有可能是有慢sql占用了连接池中的连接没有释放,导致后续进来的请求迟迟获取不到连接池中的连接,导致请求报错,登录数据库排查发现如下sql出现执行非常的慢:.

Content-disposition中Attachment和inline的区别

- - 编程语言 - ITeye博客
java web中下载文件时,我们一般设置 Content-Disposition告诉浏览器下载文件的名称,是否在浏览器中内嵌显示.. 表示浏览器内嵌显示一个文件. 表示会下载文件,如火狐浏览器中.  如上代码中是内嵌显示图片呢?还是会弹框下载呢?. 为什么呢?设置为inline应该是内嵌显示啊!.