您的位置 首页 php

抽象nginx做cache时缓存判断万能公式——存不存、存多久、用不用

nginx 是综合型的大拿选手,他集 web服务器 、负载均衡、cache三种能力于一身,可以说是无所不能。比如说一个中型网站的场景选型,前端是负载,后端托着一堆 apache 服务器,现在该到前端负载选型的了,虽然lvs和ha单纯从负载的性能要比nginx好一些,但我还是会选nginx,因为nginx在做负载的同时,可以将热点的静态内容cache一遍,做一次加速,无形间减轻了后端web服务器的压力,提高了用户体验,一箭双雕。Nginx做cache配置是很灵活的,里面有各种缓存指令,起初接触会摸不到北,不知道缓存怎么生效、怎么过期、怎么更新,用了一段时间后我终于抽象出了一套 缓存判断的万能公式 —— 存不存、存多久、用不用?

Nginx的资源是否缓存是由客户端、源站与nginx的缓存配置共同决定,nginx如果没有缓存策略配置,默认按照 request 请求头、header响应头信息走标准的http缓存判断机制(看 cache-control 、expires、cookie这些属性),仅当一个资源没有被设为不能缓存的黑名单,且有大于0的存放时间的生命周期时,资源才被缓存,对于nginx做cache时缓存判断的万能公式集成到一张图上如下, 一个资源只有同时具备可缓存和有缓存时间大于0的缓存生命周期双重属性,才能真正被缓存下来,至于存下来之后用不用还得再进行下一步的判断

e2507a14b8e24c1788f4d9bbae3f9e3f

Nginx缓存判断的万能公式(运维网咖社—矢量bit)

存不存: 如果没有设置,会根据源站响应头信息走标准的http协议缓存判断机制,查看cache-control、expires以及cookie等属性,nginx主要设置参数有:

proxy_ignore_headers (X-Accel-Expires |Expires | Cache-Control |Set-Cookie)忽略这些header响应头属性的判断,是缓存的必要不充分条件。

proxy_no_cache ($cookie_nocache $arg_nocache $arg_comment)如果任何一个变量参数值不为空,或者不等于0,nginx就不会对资源进行缓存,直接进行代理转发,是缓存的必要不充分条件。

存多久: 如果一个资源没有被加入不能存的黑名单,也就是说这个资源是可以存的,然后就会到这个判断环节“存多久”,如果存多久的时间参数为0,那么这个资源也是不会存的,存多久有其优先级的判断顺序:

1级 、proxy_cache_path中inactive参数,意思是一个已存资源多久内一次访问也没有就将其删除,这个缓存时间优先于所有策略;

2级 、响应头Expires属性,就是资源存到什么时候过期,这个是第二优先级策略,有这个属性后将停止后续的时间判断;

3级 、响应头cache-control里的max-age,这个是第三优先级存多久的参数,如果有max-age将不再看后续的时间判断;

4级 、 proxy_cache_valid 200 1d;默认的存储时间缓存策略,比如当前参数,如果一个200ok的资源是可以存的,但是没有其它策略说可以存多久,默认走的是这个参数的缓存时间,此参数设置是很灵活的,可以对各种状态码进行配置。

用不用: 用不用有两种情况,一个是已经过期的资源用不用,另一个是没过期的资源用不用,日志打印有BYPASS、EXPIERD、STALE、UPDATING这些,具体如下:

proxy_cache_bypass ($cookie_nocache $arg_nocache$arg_comment)任何一个变量参数不为空,都直接回源,不考虑缓存中有没有,request头里面有nocache直接回源;

proxy_cache_use_stale (error timeout invalid_header updating http_500 http_502 http_503 http_504)发现已经缓存的内容有这些问题,直接用过期的缓存资源回复;

proxy_cache_revalidate off ;如果一个走默认缓存时间的资源过期了,且当前参数是off,直接忽略缓存资源直接回源,日志EXPIERD。

测试举例论证 ,默认配置,一个200ok资源() 只有cookie信息没有max-age。

第一次测试配置参数:

 proxy_cache_valid 200 10m;#proxy_ignore_headers Set-Cookie; 注释掉  

头信息以及测试如下:

b77297fdfb9d41e8b05b79c0501fe588

多次访问操作如下:

118a4c329d114cfd9098a50be373281f

多次访问日志如下(全部MISS):

1e20988f52c54e1aa5b59dd96513a964

小结: 虽然有对于200ok的信息设置缓存时间为10分钟,但是cookie信息的首先判断是不能存,所以根本不会看你对200ok资源的缓存时间,最终结论是不能存。

第二次测试配置参数:

 #proxy_cache_valid 200 10m;注释掉proxy_ignore_headers Set-Cookie;  

多次访问日志如下(全部MISS):

8a116d08b0554b3ca902f71937958766

小结: 虽然忽略了对cookie信息的判断,告诉nginx有cookie的信息是可以存的,但是对于200ok的信息设置缓存时间为0,所以最终资源还是不能存。

第三次测试配置参数:

 proxy_cache_valid 200 10m;proxy_ignore_headers Set-Cookie;  

多次访问日志如下(1次访问MISS后,之后均为HIT):

ebc8a83587f047f6bfb23637bb90a923

小结: 首先忽略了cookie信息的判断,告诉nginx说cookie信息是可以存的,后查询没有expires和max-age就去找默认缓存时间,发现对于200ok的默认缓存时间是10m,所以最终判定可以缓存,有效缓存时间为10分钟。

综上论证, 一个资源只有同时具备可缓存和有缓存时间大于0的生命周期的双重属性,才能真正被缓存下来,至于存下来之后用不用还得再进行下一步的判断 。所以nginx对于资源是否缓存要经过两步判断,第一步存不存,第二步存多久,对于是否用缓存了的资源为用户进行服务还得进行下一层用不用的判断,详细的走的判断参数可以看我画的那张图。

优化建议: 为了做到cache加速的同时,又不影响业务,在缓存策略配置上最好遵循头部信息的要求,不要忽略nocache等字样强制存储,也就是说proxy_ignore_headers指令慎用,比如一些图片验证码和一些php、jsp、asp等动态内容在存储了后,用户多次访问会返回同样的信息,导致用户报障。还有一类资源是没有明确生命周期缓存头的(无cache-control或expires),也就是没有任何缓存要求,建议采用保守方式不要存储,主要是proxy_cache_valid指令的配置,有cookie的信息nginx默认就是不存的。对于故障信息的存储根据实际业务处理,有些故障信息是有必要存储的,还有任何资源如果源站出问题,要设置吐过期资源给用户,做到起码用户可以访问,保护一下源站。个别资源的缓存处理根据业务需要个别设置。

文章来源:智云一二三科技

文章标题:抽象nginx做cache时缓存判断万能公式——存不存、存多久、用不用

文章地址:https://www.zhihuclub.com/51018.shtml

关于作者: 智云科技

热门文章

评论已关闭

29条评论

  1. 8mL Cloth Topical 40 mL 100mL Cloth Topical 90 mL 601 Liquid, extended release Topical 54 Informal Gathering 11 00 a

  2. It appears that his caregivers have been in those kids lives from day one so it is not like he found them on Craigslist; he also has the financial means to give his children whatever is required 16 Aredia 12 people, 63

  3. Notation of adoption, nonpaternity the biologic father should be included in the pedigree, consanguinity, and use of assisted reproductive technology e

  4. They like it but they aren t jumping up and down about it as they did when the iPad first appeared

  5. Intas is trusted by healthcare professionals across more than 85 countries across the globe, it is my pleasure to contact you in order Please contact us for more details and best price Enquire Now to know about

  6. Angiogenesis arrays were used to identify factors secreted by breast tumor cells after exposure to APR

  7. When the malignancy is a malignancy characterized by overexpressed topoisomerase II, the additional therapeutic agent can be selected from the group consisting of etoposide, teniposide, doxorubicin, daunorubicin, mitoxantrone, amsacrine, ellipticine, aurintricarboxylic acid, and HU 331 3 hydroxy 2 1R 6 isopropenyl 3 methyl cyclohex 2 en 1 yl 5 pentyl 1, 4 benzoquinone, but is not limited to those agents

  8. The serum levels of motilin, total ghrelin and leptin were assayed by Human Motilin Elisa kit Cusabio Biotech Co, Wuhan, P Alternatively, they may be formulated in a cream with an oil in water cream base

  9. Some authors conclude that VEGFR2 was not detected in MCF 7 cells neither in the absence nor in the presence of hormones 27, while others note that VEGFR2 was only detected in tumor endothelial cells, but not in the carcinoma cells of the MCF 7 tumors 26

  10. Ashwagandha has been the subject of several extensive studies, including a crossover study that showed a 15 increase in testosterone levels after several weeks of administration

  11. The proportions of patients who had adverse events, serious adverse events, and adverse events leading to the discontinuation of a study drug were similar in the empagliflozin group and the placebo group Table 2 Patients undergoing ovulation induction and superovulation are closely monitored by blood tests and ultrasounds

  12. High blood pressure is a possibility, especially if an underlying disease is present, although it is quite unlikely

  13. Minor 1 budesonide, indapamide If you become pregnant while taking nitrofurantoin, call your doctor

  14. Furthermore, Nasu Hakola patients having TREM2 mutations only in microglia show pain symptoms 52

  15. This suggests that escape of ER cells in the normal breast from an E mediated inhibitory effect on growth could be one step in the development of malignancy

  16. Ning Qidi continued I am here, I will not let you do anything, and I am tired of seeing all of this, this world should have made some changes long ago

  17. 53 per 1000 person years and 48, 991 among patients who had received the diagnosis of any cancer but CNS tumors incidence rate, 23

网站地图