本文主要解析一下nginx ngx_http_proxy_module中的cache相關設定參數,希望對大家有幫助。
名稱 | 預設組態 | 作用域 | 官方說明 | 中文解讀 | 模組 |
---|---|---|---|---|---|
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. ##設定是否開啟對後端回應的緩存,如果開啟的話,參數值就是zone的名稱,例如proxy_cache mycache | ngx_http_proxy_module | |
#沒有預設值,實例如proxy_cache_valid 200 302 10m; | http, server, location | Sets caching time for different response codes. | #Sets caching time for different response codes. | #對不同的response code定不同的緩存時間,如果不設置code,默認為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 | 為快取設定key,預設值相當於proxy_cache_key $scheme$proxy_host$ uri$is_args$args; | ngx_http_proxy_module |
proxy_cache_path | 沒有預設值,實例proxy_cache_path /var/cache levels=1:2 keys_achezone=imgcachezone=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. | 指定快取儲存的路徑,檔案名稱為快取儲存的md值,然後多級目錄的話,根據level參數來生成,例如levels=1:2:3,第一個目錄名取md5值的倒數第一個值,第二個目錄名取md5值的第2和3個值,第三個目錄名取md5值的第4,5,6個值;key_zone參數用來指定在共享記憶體中快取的元資料的名稱和記憶體大小,例如keys_zone=imgcache:100m,所有的快取查找首先經過這裡查找元數據,如果命中再去檔案系統查找相應的快取;inactive用來指定快取沒有被存取逾時移除的時間,預設是10分鐘,也可以自己指定例如inactive=2h ;max_size 用來指定快取的最大值,超過這個值則會自動移除最近最少使用的快取 | ngx_http_proxy_module |
proxy_cache_bypass | #沒有預設值 | 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. | 指定哪些回應在某些值不為空或不為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#http, server, location | Sets the number of requests after which the response be cached. | 指定在多少次請求之後才快取回應內容 |
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_up directive's parameters match the parameters of the proxy_next_updtive#next#.指定在後端服務器在返回什麼狀態碼的情況下可以使用過期的緩存,比如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 cache or the cache lock for this 問題 to be rel the uple the cache elect the cache lock for this ? by the proxy_cache_lock_timeout directive. | 預設不開啟,開啟的話則每次只能有一個請求更新相同的緩存,其他請求要么等待緩存有資料要么限時等待鎖釋放;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. | 等待緩存鎖定超時之後將直接請求後端,結果不會被緩存; nginx 1.1.12才開始有 | 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; } } }
#ngx_http_proxy_module
nginx反向代理程式快取設定
Understanding the nginx proxy_cache_path directive
#相關推薦:
#以上是關於nginx proxy cache設定參數解讀的詳細內容。更多資訊請關注PHP中文網其他相關文章!