nginx proxy cache配置参数解读

本文主要解析一下nginx ngx_http_proxy_module中的cache相关配置参数。

proxy_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 configuration level. 设置是否开启对后端响应的缓存,如果开启的话,参数值就是zone的名称,比如proxy_cache mycache ngx_http_proxy_module
proxy_cache_valid 没有默认值,实例如proxy_cache_valid 200 302 10m; http, server, location 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_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. 指定缓存存储的路径,文件名为cache key的md5值,然后多级目录的话,根据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 which the response will be cached. 指定在多少次请求之后才缓存响应内容 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. 指定在后端服务器在返回什么状态码的情况下可以使用过期的缓存,比如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. 默认不开启,开启的话则每次只能有一个请求更新相同的缓存,其他请求要么等待缓存有数据要么限时等待锁释放;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;
        }
    }
}

doc

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容

  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,670评论 24 1,002
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,867评论 0 3
  • nginx做负载均衡器以及proxy缓存配置 关于nginx的安装和基本配置请参考nginx,本文在原基础上完成以...
    meng_philip123阅读 1,598评论 1 16
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,003评论 0 9
  • Page 1:nginx 服务器安装及配置文件详解 CentOS 6.2 x86_64 安装 nginx 1.1 ...
    xiaojianxu阅读 8,535评论 1 41