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 不会在缓存里查找它的响应而是直接把请求发送给后端。
相关推荐
这个小工具极大地简化了对Nginx缓存的管理,确保了网站内容更新时,用户能够快速获取到最新的信息,避免了旧版本的缓存数据影响用户体验。 首先,我们需要了解Nginx的缓存工作原理。Nginx通过其内置的...
ngx_cache_purge 是 nginx 模块,此模块可以清理 nginx 的 FastCGI、proxy、 SCGI 和 uWSGI 的缓存。配置指令(相同位置语法)fastcgi_cache_purgesyntax: fastcgi_cache_purge on|off|<method> [from all|<ip> [.....
### 强烈推荐打造高性能Nginx缓存服务器 #### Nginx缓存概述 Nginx是一款广泛使用的高性能HTTP服务器及反向代理服务器。从0.7.48版本开始,Nginx引入了缓存功能,允许将请求的结果缓存起来,从而提高响应速度并...
nginx-缓存控制没有缓存清除模块的 Nginx 缓存控制插件部分代码是基于其他 Nginx Cache Plugins 的代码,但这是设计为简单、轻量级的实现== 具体.... == Mark Jaquith 提出了使用强制动态页面加载的标头的基本设计,...
nginx缓存阅读器 解析 nginx 的缓存文件夹和文件 这是一个用于快速查看nginx缓存文件夹信息的CLI客户端。 它可以从缓存文件夹中读取密钥并提取其中的内容。 这也可以用作库。 见 先决条件 节点 >=12.xx 建议您...
fastdfs-5.05.tar.gz : FastDFS安装主文件包 libfastcommon-master.zip: FastDFS文件系统依赖包 nginx-1.8.1.tar.gz:nginx安装包 fastdfs-nginx-module_v1.16.tar.gz: nginx下...ngx_cache_purge-2.1.tar.gz:缓存
【Nginx 静态文件缓存解决方案】 Nginx 是一款高性能的 HTTP 和反向代理服务器,常用于处理静态资源,以减轻后端服务器的压力。为了进一步提高静态资源的访问效率,我们可以利用 Nginx 的缓存功能。下面详细介绍...
proxy_cache机制允许Nginx将从后端服务器获得的静态内容缓存到磁盘上,当相同请求再次发生时,可以直接从磁盘缓存中提供数据,而无需每次都向后端服务器获取。 proxy_cache的基本用法是在Nginx的配置文件中进行设置...
ngx_slowfs_cache 是 nginx 模块,允许缓存静态文件(使用 root 指令管理)。 这使得能够为存储在慢文件系统上的文件创建快速缓存。当缓存放置在与原点相同的速度磁盘上时,使用此模块没有意义。配置示例:http { ...
ngx_cache_purge模块则允许动态清除缓存,确保内容更新及时生效。 安装配置流程大致如下: 1. 安装依赖库:如openssl、pcre、zlib,它们是Nginx编译所需的库文件。 2. 编译安装FastDFS:解压FastDFS_v5.01.tar.gz...
例如,可以将Cache-Control指令修改为其他值,如public、max-age=秒数等,来允许nginx缓存服务器缓存资源。 方法二:修改nginx的配置文件 可以通过在nginx配置文件中添加特定的配置指令来强制nginx忽略某些响应头,...
**FastDFS 5.01 + Nginx + ...以上是对"FastDFS 5.01 + Nginx + Cache安装手册"的主要内容概述,实际操作中还需要详细阅读安装手册,理解每个步骤的具体细节,以便成功部署和使用这套高性能的分布式文件系统解决方案。
Nginx图片缓存服务器配置是一项关键的技术实践,旨在提高网站性能和用户体验,尤其是在处理大量静态图片资源的网站中。Nginx服务器以其高效的处理能力、低资源占用和高并发特性,成为部署静态内容的理想选择。它能...
根据实际负载调整配置参数,例如增加Tracker或Storage服务器,优化Nginx缓存策略,以达到最佳性能。 总结,FastDFS+Nginx+Cache的组合,为大型文件服务提供了稳定且高效的解决方案。正确配置与优化每个组件,能确保...
### FastDFS 5.01 + Nginx + Cache 集群安装配置知识点解析 #### 一、FastDFS 概述与环境准备 **FastDFS**是一款开源的分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、负载均衡等。FastDFS适合...
“nginx系列(十)nginx缓存代理proxy_cache和CDN实现的原理”这个标题揭示了本文将探讨的主题,主要集中在两个关键点上:一是Nginx的缓存代理机制proxy_cache,二是如何利用Nginx实现内容分发网络(CDN)的功能。...
总结起来,NGINX的反向代理和缓存功能可以帮助我们有效地管理流量,减轻后端服务器的负担,同时通过缓存静态内容来显著提高网站的加载速度。正确配置这些设置,即使在资源有限的情况下,也能实现高性能的网站服务。...
nginx.conf 部分内容: proxy_temp_path /nginx/cache/temp; proxy_cache_path /nginx/cache/path levels=1:2 keys_zone=cache_test:2048m inactive=7d max_size=10g; ...... location ~ .(gif|jpg|jgep|png)$ ...
srcache-nginx-module, 基于透明subrequest的任意 Nginx 位置缓存布局 电子邮件名称ngx_srcache - 基于透明subrequest的任意 Nginx 位置缓存布局:这里 MODULE 没有与 Nginx 源一起分发。 我们将看到安装说明( 参见...