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

该例假定从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 不会在缓存里查找它的响应而是直接把请求发送给后端。

Nginx content cache Nginx内容缓存的更多相关文章

  1. 6、nginx的反向代理及缓存功能

    nginx模块的应用 ngx_http_proxy_module  nginx 反向代理模块: http://nginx.org/en/docs/http/ngx_http_proxy_module. ...

  2. Nginx内容缓存

    本节介绍如何启用和配置从代理服务器接收的响应的缓存.主要涉及以下内容 - 缓存介绍 启用响应缓存 涉及缓存的NGINX进程 指定要缓存的请求 限制或绕过缓存 从缓存中清除内容 配置缓存清除 发送清除命 ...

  3. Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能

    一.负载均衡: 通过反向代理客户端的请求到一个服务器群组,通过某种算法,将客户端的请求按照自定义的有规律的一种调度调度给后端服务器. Nginx的负载均衡使用upstream定义服务器组,后面跟着组名 ...

  4. 死磕nginx系列--使用nginx做cache服务

    配置文件 nginx.conf 主配置文件 worker_processes 1; events { worker_connections 1024; } http { include mime.ty ...

  5. nginx Proxy Cache 配置

    总结一下 proxy cache 设置的常用指令及使用方法: proxy_cache proxy_cache zone | off 配置一块公用的内存区域的名称,该区域可以存放缓存的索引数据.注意:z ...

  6. nginx实现负载均衡、缓存功能实战

    nginx实现负载均衡.缓存功能实战 什么是正向代理?应用场景:翻墙 什么是反向代理?例如:haproxy和nginx   Nginx实现反向代理 nginx代理基于是ngx_http_proxy_m ...

  7. nginx添加proxy_cache模块做缓存服务器

    业务需求nginx对后端tomcat(静态文件)做缓存 减轻后端服务器的压力 # nginx-1.6.2.tar.gz  ngx_cache_purge-2.3.tar.gz #编译安装 ./conf ...

  8. Nginx使用Expires增加浏览器缓存加速(转)

    转载自:Nginx使用Expires增加浏览器缓存加速 Nginx可以更改HTTP头部,这个是Web服务器必须的,当然Nginx更可以支持在HTTP头部中添加Expires等相关信息,增强浏览器缓存, ...

  9. nginx warn an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/ while reading upstream

    最近管理的nginx发现大量的error log,log内容如下: an upstream response is buffered to a temporary file /var/cache/ng ...

随机推荐

  1. pgrep---以名称为依据从运行进程队列中查找进程

    pgrep命令以名称为依据从运行进程队列中查找进程,并显示查找到的进程id.每一个进程ID以一个十进制数表示,通过一个分割字符串和下一个ID分开,默认的分割字符串是一个新行.对于每个属性选项,用户可以 ...

  2. 小米开源文件管理器MiCodeFileExplorer-源码研究(1)-2个模型Model

    上篇说到,把小米的Java代码整理成了5个包,其中1个是net.micode.fileexplorer.model.这个包就2个模型类,最基本了,FileInfo和FavoriteItem. pack ...

  3. 03007_JDBC概述

    1.JDBC概述 (1)JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...

  4. hdu 1863 畅通project kruskal || prim

    简单最小生成树,畅通project.这三道题目都是练习最小生成树的. 注意一下推断是否有通路时,kruskal能够推断每一个点的祖先是否同样.prim能够推断每一个点是否都加进集合里面了,也就是说是否 ...

  5. struts2中action手动获取參数

    struts2中action手动获取Session,jsp页面參数 1. ActionContext 在Struts2开发中,除了将请求參数自己主动设置到Action的字段中,我们往往也须要在Acti ...

  6. malloc,colloc,realloc内存分配,动态库,静态库的生成与调用

     1.在main方法里面直接定义一个很大的数组的时候.可能会出现栈溢出:错误代码演示: #include<stdio.h> #include<stdlib.h> void ...

  7. linux 批量设置文件夹755 文件644权限

    linux 批量设置文件夹755 文件644权限 文件来源 http://www.111cn.net/sys/linux/109724.htm 本文章来为各位介绍一篇关于linux 批量设置文件夹75 ...

  8. UDP深入骨髓【转】

    从UDP的”连接性”说起–告知你不为人知的UDP 原文地址:http://bbs.utest.qq.com/?p=631 很早就计划写篇关于UDP的文章,尽管UDP协议远没TCP协议那么庞大.复杂,但 ...

  9. Maven中央仓库信息速查

    http://maven.outofmemory.cn/

  10. Vue简介以及基本使用

    Vue 是一套构建用户界面的渐进式 框架 框架和库? 框架(基于自身的特点向用户提供一套完整的解决方案,控制权在框架本身,需要使用者按照框架所规定的某种规范进行开发) Vue Angular Reac ...