Nginx作为代理服务_缓存服务配置语法
proxy_cache配置语法
(1)proxy_cache_path配置语法
Syntax: | proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; |
Default: | — |
Contex: | http |
语法解释:
Sets the path and other parameters of a cache. Cache data are stored in files. The file name in a cache is a result of applying the MD5 function to the cache key. The levels
parameter defines hierarchy levels of a cache: from 1 to 3, each level accepts values 1 or 2. 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, and then the file is renamed. Starting from version 0.8.9, temporary files and the cache can be put on different file systems. However, be aware that in this case a file is copied across two file systems instead of the cheap renaming operation. It is thus recommended that for any given location both cache and a directory holding temporary files are put on the same file system. The directory for temporary files is set based on the use_temp_path
parameter (1.7.10). If this parameter is omitted or set to the value on
, the directory set by the proxy_temp_path directive for the given location will be used. If the value is set to off
, temporary files will be put directly in the cache directory.
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. One megabyte zone can store about 8 thousand keys.
As part of commercial subscription, the shared memory zone also stores extended cache information, thus, it is required to specify a larger zone size for the same number of keys. For example, one megabyte zone can store about 4 thousand keys.
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 “cache manager” process monitors the maximum cache size set by the max_size
parameter. When this size is exceeded, it removes the least recently used data. The data is removed in iterations configured by manager_files
, manager_threshold
, and manager_sleep
parameters (1.11.5). During one iteration no more than manager_files
items are deleted (by default, 100). The duration of one iteration is limited by the manager_threshold
parameter (by default, 200 milliseconds). Between iterations, a pause configured by the manager_sleep
parameter (by default, 50 milliseconds) is made.
A minute after the start the special “cache loader” process is activated. It loads information about previously cached data stored on file system into a cache zone. The loading is also 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). Between iterations, a pause configured by the loader_sleep
parameter (by default, 50 milliseconds) is made.
Additionally, the following parameters are available as part of our commercial subscription:
purger
=on
|off
Instructs whether cache entries that match a wildcard key will be removed from the disk by the cache purger (1.7.12). Setting the parameter to on
(default is off
) will activate the “cache purger” process that permanently iterates through all cache entries and deletes the entries that match the wildcard key.
purger_files
=number
Sets the number of items that will be scanned during one iteration (1.7.12). By default, purger_files
is set to 10.
purger_threshold
=number
Sets the duration of one iteration (1.7.12). By default, purger_threshold
is set to 50 milliseconds.
purger_sleep
=number
Sets a pause between iterations (1.7.12). By default, purger_sleep
is set to 50 milliseconds.
(2)proxy_cache配置语法
Syntax: | proxy_cache zone | off; |
Default: | proxy_cache off; |
Context: | http, server, location |
语法解释:
Defines a shared memory zone used for caching. The same zone can be used in several places. Parameter value can contain variables (1.7.9). The off parameter disables caching inherited from the previous configuration level.
(3)proxy_cache_valid配置语法
Syntax: | proxy_cache_valid [code ...] time; |
Default: | — |
Context: | http, server, location |
语法解释:
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, the any parameter can be specified to cache any responses:
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 higher priority than setting of caching time using the directive.
- The “X-Accel-Expires” header field sets caching time of a response in seconds. The zero value disables caching for a response. If the value starts with the @ prefix, it sets an absolute time in seconds since Epoch, up to which the response may be cached.
- If the header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or “Cache-Control”.
- If the header includes the “Set-Cookie” field, such a response will not be cached.
- If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes the “Vary” field with another value, such a response will be cached taking into account the corresponding request header fields (1.7.7).
(4)proxy_cache_key配置语法
Syntax: | proxy_cache_key string; |
Default: | proxy_cache_key $scheme$proxy_host$request_uri; |
Context: | http, server, location |
语法解释:
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;