Nginx cache基本指令整理

  categories:nginx  tags:  author:

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的几个主要cache指令做一个简单的整理,另外部分指令做了实际测试,本文具体包括如下指令:

 

proxy_cache,proxy_cache_bypass,proxy_cache_key,proxy_cache_lock,proxy_cache_lock_timeout,proxy_cache_min_uses,proxy_cache_path,proxy_cache_use_stale,proxy_cache_valid,proxy_no_cache 

 

 

1. proxy_cache 

 

syntax:

 

proxy_cache  zone| off;

 

default:

 

proxy_cache off;

 

context:

 

http, server, location

 

定义一块共享内存区域用来进行缓存。相同名称的缓存区域可以在多个地方使用。Off参数关闭从前一个级别配置中继承下来的缓存设置

 

Nginx官方原文如下:

 

Defines a shared memory zone used for caching. The same zone can be used in several places. The off parameter disables caching inherited from the previous configuration level.

 

 

2. proxy_cache_bypass

 

yntax:

 

proxy_cache_bypass   string…;

 

default:

 

 

context:

 

http, server, location

 

 

定义一个条件,在这个条件成立后将不会从缓存中获取数据。至少有一个字符串参数不为空,并且是不等于“0”,则响应不会从缓存中获取。

 

Nginx官方原文如下:

 

Defines conditions under which the response will not be taken from a cache. If at least one value of the string parameters is not empty and is not equal to “0” then the response will not be taken from the cache:

 

proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;

 

proxy_cache_bypass $http_pragma    $http_authorization;

 

 

Can be used along with the proxy_no_cache directive.

 

 

3. proxy_cache_key

 

syntax:

 

proxy_cache_key   string;

 

default:

 

proxy_cache_key $scheme$proxy_host$request_uri;

 

context:

 

http, server, location

 

给缓存数据定义一个键,例如
proxy_cache_key “$host$request_uri $cookie_user”;
默认情况下,该指令的值的字符串
proxy_cache_key $scheme$proxy_host$uri$is_args$args;

 

 

Nginx官方原文如下:

 

Defines a key for caching, for example

 

proxy_cache_key “$host$request_uri $cookie_user”;

 

 

By default, the directive’s value is close to the string

 

proxy_cache_key $scheme$proxy_host$uri$is_args$args;

 

 

4. proxy_cache_lock

 

syntax:

 

proxy_cache_lock  on | off;

 

default:

 

proxy_cache_lock off;

 

context:

 

http, server, location

 

This directive appeared in version 1.1.12.

 

当指令被指定时,根据 proxy_cache_key指令确定的若干个或得出相同响应元素若干个请求仅仅有一个能被传递给后端的代理服务器(最终传递给应用服务器)去生成响应内容,生成后响应内容添加到cache中, 其他请求从cache中获取数据。其他请求将等待cache中有内容出现或者等待超时为止。

 

 

Nginx官方原文如下:

 

When enabled, only one request at a time will be allowed to populate a new cache element identified according to the proxy_cache_key directive by passing a request to a proxied server. Other requests of the same cache element will either wait for a response to appear in the cache, or the cache lock for this element to be released, up to the time set by the proxy_cache_lock_timeout directive.

 

 

5. proxy_cache_lock_timeout

 

syntax:

 

proxy_cache_lock_timeout    time;

 

default:

 

proxy_cache_lock_timeout    5s;

 

context:

 

http,  server,  location

 

This directive appeared in version 1.1.12.

 

proxy_cache_lock指令设置一个超时时间

 

Sets a timeout for proxy_cache_lock.

 

 

6. proxy_cache_min_uses

 

syntax:

 

proxy_cache_min_uses   number;

 

default:

 

proxy_cache_min_uses 1;

 

context:

 

http, server, location

 

 

设置请求发生多少次后,请求(响应内容)被缓存

 

Sets the number of requests after which the response will be cached.

 

 

7. proxy_cache_path

 

syntax:

 

proxy_cache_path  path[levels=levels] keys_zone=name:size [inactive=time]

 

[max_size=size] [loader_files=number] [loader_sleep=time]

 

 [loader_threshold=time];

 

default:

 

 

context:

 

http

 

 

设置高速缓存的路径和其他参数。缓存数据存储在文件中。在高速缓存中缓存内容文件名以及对应的key都用MD5函数处理。Levels参数确定缓存文件的层级关系。例如下面的配置文件

 

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

 

缓存后的文件名将如下:

 

/ data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c

 

一个缓存的响应,首先被写入到一个临时文件,然后重命名一个文件。从版本0.8.9临时文件和缓存可以放在不同的文件系统,但在这种情况下,一个文件复制在两个文件系统中发生,而不是廉价的重命名操作。因此建议,对于任何给定的位置,缓存目录proxy_temp_path指令所设定的临时文件都放在同一个文件系统

 

此外,所有活跃的数据信息包括其key都存储在一个共享内存区域这个区域的名称及大小等keys_zone指令的参数进行配置被缓存在内存中的数据在一定时间没有被访问后将变成不活跃,这些数据将被从内存中移除无论数据是否还是有效,不活跃时间由inative 参数设置。默认情况下inactive10分钟。

 

高速缓存管理器进程将监控max_size参数设置最大缓存大小,超过此大小时,它消除了最近最少使用的数据。

 

一分钟后开始的指定“cache loader”进程被激活,他将加载以前缓存在文件系统中的缓存数据到内存中。一个加载包括在若干个迭代过程。在一个迭代过程中加载文件数量小于loader_files 参数指定的数量(默认值100.除此以外迭代时间小于loader_threshold限制的值,默认值200毫秒。在两个迭代中间有个短暂的暂停(加载的暂停,nginx其他工作没有暂停),这个时间被loader_sleep参数所控制,默认值50毫秒。

 

 

Nginx官方原文如下:

 

Sets path and other parameters of a cache. Cache data are stored in files. Both the key and file name in a cache are a result of applying the MD5 function to the proxied URL. The levels parameter defines hierarchy levels of a cache. For example, in the following configuration

 

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

 

file names in a cache will look like this:

 

/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c

 

A cached response is first written to a temporary file, then a file is renamed. Starting from version 0.8.9 temporary files and the cache can be put on different file systems but be aware that in this case a file is copied across two file systems instead of the cheap rename operation. It is thus recommended that for any given location both cache and a directory holding temporary files set by the proxy_temp_path directive are put on the same file system.

 

In addition, all active keys and information about data are stored in a shared memory zone, whose name and size are configured by the keys_zone parameter. Cached data that are not accessed during the time specified by the inactive parameter get removed from the cache regardless of their freshness. By default, inactive is set to 10 minutes.

 

The special process “cache manager” monitors the maximum cache size set by the max_size parameter; when this size is exceeded it removes the least recently used data.

 

A minute after the start the special process “cache loader” is activated that loads information about previously cached data stored on file system into a cache zone. A load is done in iterations. During one iteration no more than loader_files items are loaded (by default, 100). Besides, the duration of one iteration is limited by the loader_threshold parameter (by default, 200 milliseconds). A pause is made between iterations, configured by the loader_sleep parameter (by default, 50 milliseconds).

 

 

8. proxy_cache_use_stale 

 

syntax:

 

proxy_cache_use_stale error | timeout | invalid_header | updating | http_500

 

| http_502 | http_503 | http_504 | http_404 | off …;

 

default:

 

proxy_cache_use_stale off;

 

context:

 

http, server, location

 

 

如果nginx同代理服务器工作过程中发生一个错误,nginx可能使用一个陈旧的被缓存的数据。这个指令决定在那种情况下使用这个功能。这个指令的参数会同proxy_next_upstream指令的参数相匹配。

 

 此外,如果目前正在更新,更新参数允许使用过时的缓存的响应。这可以最大限度地减少更新缓存数据时,代理服务器的访问次数。
     为了尽量减少当填充一个新缓存元素时访问代理服务器次数过多的问题,可以使用proxy_cache_lock指令,来缓解。

 

 

Nginx官方原文如下:

 

If an error occurs while working with the proxied server it is possible to use a stale cached response. This directives determines in which cases it is permitted. The directive’s parameters match those of the proxy_next_upstream directive.

 

Additionally, the updating parameter permits to use a stale cached response if it is currently being updated. This allows to minimize the number of accesses to proxied servers when updating cached data.

 

To minimize the number of accesses to proxied servers when populating a new cache element, the proxy_cache_lock directive can be used.

 

 

9. proxy_cache_valid 

 

syntax:

 

proxy_cache_valid [code…] time;

 

default:

 

 

context:

 

http, server, location

 

 

设置不同响应代码的缓存时间,例如如下指令:

 

proxy_cache_valid 200 302   10m;

 

proxy_cache_valid 404       1m;

 

设置缓存的响应代码为200302时间为10分钟,代码为404的响应缓存时间为1分钟。

 

如果只指定缓存时间
proxy_cache_valid 5m;
    然后只有200301302响应缓存。

 

此外,它可以被指定缓存任何响应通过使用any参数:
    proxy_cache_valid 200 302 10m;

 

proxy_cache_valid 301      1h;

 

proxy_cache_valid any      1m;

 

缓存的参数也能通过响应头直接设置。这个设置具有更高优先级比通过缓存指令设置的缓存时间。“X-ACCEL-Expires”头字段以秒为单位设置响应的缓存时间。0禁止缓存响应。

 

如果一个值前缀@开始,将针对响应设置一个绝对过期时间(自1970110时到达过期时间的绝对秒数)的缓存若是响应头中没有 “X-Accel-Expires” 字段,则缓存参数可以通过“Expires” or “Cache-Control”字段进行设置。若是一个响应头包括“Set-Cookie” 字段,则这样的响应将不被缓存。这样的一个或者多个响应头字段的处理可以通过proxy_ignore_headers 指令进行禁止。

 

http://www.ehow.com/how_7520132_convert-seconds-epoch-date.html

 

 

Nginx官方原文如下:

 

Sets caching time for different response codes. For example, the following directives

 

proxy_cache_valid 200 302 10m;

 

proxy_cache_valid 404      1m;

 

set 10 minutes of caching for responses with codes 200 and 302, and 1 minute for responses with code 404.

 

If only caching time is specified

 

proxy_cache_valid 5m;

 

then only 200, 301, and 302 responses are cached.

 

In addition, it can be specified to cache any responses using the any parameter:

 

proxy_cache_valid 200 302 10m;

 

proxy_cache_valid 301      1h;

 

proxy_cache_valid any      1m;

 

Parameters of caching can also be set directly in the response header. This has a higher precedence than setting of caching time using the directive. The “X-Accel-Expires” header field sets caching time of a response in seconds. The value 0 disables to cache a response. If a value starts with the prefix @, it sets an absolute time in seconds since Epoch, up to which the response may be cached. If header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or “Cache-Control”. If a header includes the “Set-Cookie” field, such a response will not be cached. Processing of one or more of these response header fields can be disabled using the proxy_ignore_headers directive.

 

 

 

10. proxy_no_cache 

 

syntax:

 

proxy_no_cache  string…;

 

default:

 

 

context:

 

http, server, location

 

 

定义条件下的反应将不会被保存到高速缓存。如果至少有一个值的字符串参数不为空,不等于“0”,那么响应将不会被保存:
proxy_no_cache$ cookie_nocache$ arg_nocache$ arg_comment;
proxy_no_cache$ http_pragma HTTP_AUTHORIZATION;

可用于沿的proxy_cache_bypass指令。

 

定义一些条件,当响应满足这些条件时将不被缓存起来。在条件字符串中至少有一个条件不为空或者0,符合这样条件的响应才不会被缓存。

 

proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;

 

proxy_no_cache $http_pragma    $http_authorization;

 

 

Nginx官方原文如下:

 

Defines conditions under which the response will not be saved to a cache. If at least one value of the string parameters is not empty and is not equal to “0” then the response will not be saved:

 

proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;

 

proxy_no_cache $http_pragma    $http_authorization;

 

Can be used along with the proxy_cache_bypass directive.

 

 



快乐成长 每天进步一点点