序
本文主要解析一下nginx ngx_http_proxy_module中的cache相關(guān)配置參數(shù)犬缨。
proxy_cache
名稱(chēng) | 默認(rèn)配置 | 作用域 | 官方說(shuō)明 | 中文解讀 | 模塊 |
---|---|---|---|---|---|
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. | 設(shè)置是否開(kāi)啟對(duì)后端響應(yīng)的緩存赔硫,如果開(kāi)啟的話(huà),參數(shù)值就是zone的名稱(chēng)沙热,比如proxy_cache mycache | ngx_http_proxy_module |
proxy_cache_valid | 沒(méi)有默認(rèn)值狠角,實(shí)例如proxy_cache_valid 200 302 10m; | http, server, location | Sets caching time for different response codes. | 針對(duì)不同的response code設(shè)定不同的緩存時(shí)間驱还,如果不設(shè)置code膊毁,默認(rèn)為200,301,302,也可以用any指定所有code | ngx_http_proxy_module |
proxy_cache_key | proxy_cache_key $scheme$proxy_host$request_uri; | http, server, location | Defines a key for caching | 給緩存設(shè)定key,默認(rèn)值相當(dāng)于proxy_cache_key $scheme$proxy_host$uri$is_args$args; | ngx_http_proxy_module |
proxy_cache_path | 沒(méi)有默認(rèn)值绰更,實(shí)例proxy_cache_path /var/cache levels=1:2 keys_zone=imgcache:100m inactive=2h max_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. | 指定緩存存儲(chǔ)的路徑瞧挤,文件名為cache key的md5值锡宋,然后多級(jí)目錄的話(huà),根據(jù)level參數(shù)來(lái)生成特恬,比如levels=1:2:3执俩,第一個(gè)目錄名取md5值的倒數(shù)第一個(gè)值,第二個(gè)目錄名取md5值的第2和3個(gè)值癌刽,第三個(gè)目錄名取md5值的第4役首,5,6個(gè)值妒穴;key_zone參數(shù)用來(lái)指定在共享內(nèi)存中緩存的元數(shù)據(jù)的名稱(chēng)和內(nèi)存大小宋税,比如keys_zone=imgcache:100m摊崭,所有的緩存查找首先經(jīng)過(guò)這里查找元數(shù)據(jù)讼油,如果命中再去文件系統(tǒng)查找相應(yīng)的緩存 ;inactive用來(lái)指定緩存沒(méi)有被訪問(wèn)超時(shí)移除的時(shí)間呢簸,默認(rèn)是10分鐘矮台,也可以自己指定比如inactive=2h ;max_size 用來(lái)指定緩存的最大值根时,超過(guò)這個(gè)值則會(huì)自動(dòng)移除最近最少使用的緩存 | ngx_http_proxy_module |
proxy_cache_bypass | 沒(méi)有默認(rèn)值 | 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 equal to “0” then the response will not be taken from the cache. | 指定哪些響應(yīng)在某些值不為空或不為0的情況下不走緩存瘦赫,比如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. | 指定在多少次請(qǐng)求之后才緩存響應(yīng)內(nèi)容 | 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. | 指定在后端服務(wù)器在返回什么狀態(tài)碼的情況下可以使用過(guò)期的緩存,比如proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_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. | 默認(rèn)不開(kāi)啟蛤迎,開(kāi)啟的話(huà)則每次只能有一個(gè)請(qǐng)求更新相同的緩存确虱,其他請(qǐng)求要么等待緩存有數(shù)據(jù)要么限時(shí)等待鎖釋放;nginx 1.1.12才開(kāi)始有 | 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. | 等待緩存鎖超時(shí)之后將直接請(qǐng)求后端,結(jié)果不會(huì)被緩存 ; nginx 1.1.12才開(kāi)始有 | ngx_http_proxy_module |
實(shí)例
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;
}
}
}