Interpretation of nginx proxy cache configuration parameters

  nginx

Order

This article mainly analyzes cache related configuration parameters in nginx ngx_http_proxy_module.

proxy_cache

Name Default configuration action scope Official statement Interpretation in Chinese module
proxy_cache proxy_cache off; 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. Set whether to open the cache for back-end response. If so, the parameter value is the name of the zone, such as proxy_cache mycache. ngx_http_proxy_module
proxy_cache_valid There is no default value, for example proxy _ cache _ valid 2003021M; http, server, location Sets caching time for different response codes. Set different cache times for different response code. if code is not set, the default is 200,301,302, and any can be used to specify all codes. ngx_http_proxy_module
proxy_cache_key proxy_cache_key $scheme$proxy_host$request_uri; http, server, location Defines a key for caching Set a key for the cache, the default value is equivalent to proxy _ cache _ key $ scheme $ proxy _ host $ uri $ is _ args $ args; ngx_http_proxy_module
proxy_cache_path No default value, instance proxy _ cache _ path/var/cachelevels = 1: 2keys _ zone = imgcache: 100minactive = 2hmax _ size = 1g; 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. Specify the path of cache storage, the file name is md5 value of cache key, and then generate according to level parameter in case of multi-level directory, for example, levels=1:2:3, the first directory name takes the penultimate value of md5 value, the second directory name takes the 2nd and 3rd values of md5 value, and the third directory name takes the 4th, 5th and 6th values of md5 value; The key_zone parameter is used to specify the name and memory size of the metadata cached in the shared memory, for example, keys_zone=imgcache:100m, all cache searches first pass through here to find metadata, and if hit, go to the file system to find the corresponding cache; Inactive is used to specify the time when the cache has not been removed by the access timeout. The default is 10 minutes, or you can specify for example Inactive = 2h. Max_size is used to specify the maximum value of the cache, beyond which the least recently used cache will be automatically removed ngx_http_proxy_module
proxy_cache_bypass No default http, server, location 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 equa l to “0” then the response will not be taken from the cache. Specifies which responses will not be cached if some values are not empty or 0, such as proxy _ cache _ bypass $ http \ _ pragma $ http _ authorization; ngx_http_proxy_module
proxy_cache_min_uses proxy_cache_min_uses 1; http, server, location Sets the number of requests after which the response will be cached. Specifies how many requests before caching response content ngx_http_proxy_module
proxy_cache_use_stale proxy_cache_use_stale off; http, server, location Determines in which cases a stale cached response can be used during communication with the proxied server. The directive’s parameters match the parameters of the proxy_next_upstream directive. Specifies what state code the backend server can use when returning an expired cache, such as proxy _ cache _ use _ stall error timeout invalid _ headerhttp _ 500http _ 502http _ 503http _ 504; ngx_http_proxy_module
proxy_cache_lock proxy_cache_lock off; http, server, location 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. By default, it is not turned on. If it is turned on, only one request can update the same cache at a time. Other requests either wait for the cache to have data or wait for the lock to be released in a limited time. Nginx 1.1.12 ngx_http_proxy_module
proxy_cache_lock_timeout proxy_cache_lock_timeout 5s; http, server, location Sets a timeout for proxy_cache_lock. When the time expires, the request will be passed to the proxied server, however, the response will not be cached. After waiting for the cache lock to time out, the backend will be directly requested, and the result will not be cached; Nginx 1.1.12 ngx_http_proxy_module

Example

http {
    # we set this to be on the same filesystem as proxy_cache_path
    proxy_temp_path /usr/local/nginx/proxy_temp;
    # good security practice dictates that this directory is owned by the
    # same user as the user directive (under which the workers run)
    proxy_cache_path /usr/local/nginx/proxy_temp keys_zone=CACHE:10m levels=1:2 inactive=6h max_size=1g;

    server {
        location / {
            # using include to bring in a file with commonly-used settings
            include proxy.conf;
            # referencing the shared memory zone defined above
            proxy_cache CACHE;
            proxy_cache_valid any 1d;
            proxy_cache_bypass $http_pragma $http_authorization;
            proxy_cache_min_uses 3;
            proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
            proxy_pass http://upstream;
        }
    }
}

doc