nginx proxy cache配置參數(shù)解讀

本文主要解析一下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;
        }
    }
}

doc

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末替裆,一起剝皮案震驚了整個(gè)濱河市校辩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌辆童,老刑警劉巖宜咒,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異把鉴,居然都是意外死亡故黑,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)庭砍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)场晶,“玉大人,你說(shuō)我怎么就攤上這事怠缸∈幔” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵凯旭,是天一觀的道長(zhǎng)概耻。 經(jīng)常有香客問(wèn)我使套,道長(zhǎng),這世上最難降的妖魔是什么鞠柄? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任侦高,我火速辦了婚禮,結(jié)果婚禮上厌杜,老公的妹妹穿的比我還像新娘奉呛。我一直安慰自己,他們只是感情好夯尽,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布瞧壮。 她就那樣靜靜地躺著,像睡著了一般匙握。 火紅的嫁衣襯著肌膚如雪咆槽。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天圈纺,我揣著相機(jī)與錄音秦忿,去河邊找鬼。 笑死蛾娶,一個(gè)胖子當(dāng)著我的面吹牛灯谣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛔琅,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼胎许,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了罗售?” 一聲冷哼從身側(cè)響起辜窑,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎莽囤,沒(méi)想到半個(gè)月后谬擦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡朽缎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年惨远,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片话肖。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡北秽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出最筒,到底是詐尸還是另有隱情贺氓,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布床蜘,位于F島的核電站辙培,受9級(jí)特大地震影響蔑水,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜扬蕊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一搀别、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧尾抑,春花似錦歇父、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至翎冲,卻和暖如春垂睬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背府适。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工羔飞, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人檐春。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像么伯,于是被迫代替她去往敵國(guó)和親疟暖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 第一章 Nginx簡(jiǎn)介 Nginx是什么 沒(méi)有聽(tīng)過(guò)Nginx田柔?那么一定聽(tīng)過(guò)它的“同行”Apache吧俐巴!Ngi...
    JokerW閱讀 32,676評(píng)論 24 1,002
  • 1.簡(jiǎn)介: ? Nginx:engine X ,2002年硬爆,開(kāi)源欣舵,商業(yè)版? http協(xié)議:web服務(wù)器(類(lèi)似于ht...
    尛尛大尹閱讀 1,867評(píng)論 0 3
  • nginx做負(fù)載均衡器以及proxy緩存配置 關(guān)于nginx的安裝和基本配置請(qǐng)參考nginx,本文在原基礎(chǔ)上完成以...
    meng_philip123閱讀 1,598評(píng)論 1 16
  • Nginx簡(jiǎn)介 解決基于進(jìn)程模型產(chǎn)生的C10K問(wèn)題,請(qǐng)求時(shí)即使無(wú)狀態(tài)連接如web服務(wù)都無(wú)法達(dá)到并發(fā)響應(yīng)量級(jí)一萬(wàn)的現(xiàn)...
    魏鎮(zhèn)坪閱讀 2,003評(píng)論 0 9
  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,536評(píng)論 1 41