Home  >  Article  >  Backend Development  >  Interpretation of nginx proxy cache configuration parameters

Interpretation of nginx proxy cache configuration parameters

小云云
小云云Original
2018-01-03 15:44:092041browse

This article mainly analyzes the cache-related configuration parameters in nginx ngx_http_proxy_module. I hope it will be helpful to everyone.

proxy_cache

##proxy_cacheproxy_cache off;http, server, locationDefines 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 enable caching of backend responses. If enabled, the parameter value is the name of the zone, such as proxy_cache mycachengx_http_proxy_moduleproxy_cache_validThere is no default value, for example proxy_cache_valid 200 302 10m;http, server, locationSets caching time for different response codes.Sets caching time for different response codes. Set different cache times. If you do not set the code, the default is 200, 301, 302. You can also use any to specify all codesngx_http_proxy_moduleproxy_cache_keyproxy_cache_key $ scheme$proxy_host$request_uri;http, server, locationDefines a key for cachingSet the key for caching, the default value is equivalent to proxy_cache_key $scheme$proxy_host$ uri$is_args$args;ngx_http_proxy_moduleproxy_cache_pathNo default value, instance proxy_cache_path /var/cache levels=1:2 keys_zone=imgcache :100m inactive=2h max_size=1g;httpSets 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.Specifies the path to cache storage, the file name is md5 of cache key value, and then if there are multi-level directories, they are generated according to the level parameter, for example, levels=1:2:3. The first directory name takes the last value of the md5 value, and the second directory name takes the second and third values ​​of the md5 value. value, the third directory name takes the 4th, 5th, and 6th values ​​of the md5 value; the key_zone parameter is used to specify the name and memory size of the metadata cached in the shared memory, such as keys_zone=imgcache:100m, all cache searches first Search metadata here, and if it hits, go to the file system to find the corresponding cache; inactive is used to specify the time when the cache is not removed by access timeout, the default is 10 minutes, you can also specify it yourself, such as inactive=2h; max_size is used to specify the cache The maximum value, exceeding this value will automatically remove the least recently used cachengx_http_proxy_moduleproxy_cache_bypassNo default valuehttp, server, locationDefines 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.Specify which responses will not be cached if some values ​​are not empty or not 0, such as proxy_cache_bypass $http\_pragma $http_authorization; ngx_http_proxy_module##proxy_cache_min_uses#http, server, locationDetermines 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. Specify what status code the backend server can use when returning an expired cache, such as proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;ngx_http_proxy_module
Name Default configuration Scope Official description Chinese interpretation Module
proxy_cache_min_uses 1; http, server, location Sets the number of requests after which the response will be cached. Specify how many requests to cache the response content after
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. It is not enabled by default. If it is enabled, only one request can update the same cache at a time. Other requests will either wait for the cache to have data or wait for the lock to be released within a time limit; nginx 1.1.12 only started to have it. 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 requested directly, and the result will not be cached; nginx ngx_http_proxy_module

#
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

  • ngx_http_proxy_module

  • nginx reverse proxy cache configuration

  • Understanding the nginx proxy_cache_path directive

Related recommendations:

nginx configuration React static page tutorial

How to choose Apache and Nginx

nginx and node in Alibaba Cloud deployment https method steps

#

The above is the detailed content of Interpretation of nginx proxy cache configuration parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn