nginx openresty组件介绍

本文详细介绍了Nginx中的多种缓存配置方法,包括fastcgi缓存、proxy_pass缓存、proxy_cache配置等,并提供了使用Lua进行缓存管理和避免缓存失效风暴的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

---------------------缓存:

 

1、php的fastcgi缓存 fastcgi_cache

见上面配置:php fastcgi的缓存:

http://www.360doc.com/content/15/0816/14/552866_492084033.shtml

https://zhangge.net/5042.html

https://github.com/FRiCKLE/ngx_cache_purge/

 

 

 

2、缓存代理页面 proxy_pass_cache

见上面配置:proxy代理缓存

实现:本地文件的形式

http://www.open-open.com/lib/view/open1431254539701.html

https://www.aliyun.com/zixun/content/3_12_518236.html

 

 

3、proxy_cache的配置,可以通过挂载一块内存作为缓存的存储空间。

参考http://nginx.org/cn/docs/http/ngx_http_proxy_module.html。 

 

4、nginx的ngx.var.dict+lock实现  减少对后段服务的访问。解决缓存失效风暴

实现:redis,mem

 

lua-resty-lock锁机制

for-cache-locks避免雪崩

https://github.com/openresty/lua-resty-lock#for-cache-locks

 

主动异步过期缓存

https://github.com/lloydzhou/lua-resty-cache

 

srcache-nginx-module

https://github.com/openresty/srcache-nginx-module

这个模块支持和另一个模块配合将页面缓存写入redis集群

https://segmentfault.com/a/1190000003874328

 

 

5、resty.lrucache  lua自身代码实现的cache

 

local lrucache = require "resty.lrucache"

 

-- we need to initialize the cache on the lua module level so that

-- it can be shared by all the requests served by each nginx worker process:

local c = lrucache.new(200)  -- allow up to 200 items in the cache

if not c then

return error("failed to create the cache: " .. (err or "unknown"))

end

 

function _M.go()

c:set("dog", 32)

c:set("cat", 56)

ngx.say("dog: ", c:get("dog"))

ngx.say("cat: ", c:get("cat"))

 

c:set("dog", { age = 10 }, 0.1)  -- expire in 0.1 sec

c:delete("dog")

end

 

 

文档:https://github.com/openresty/lua-resty-lrucache

 

 

 

注意:

修改系统内核的修改通过设置如下参数来设置内存阀值

sysctl -w vm.extra_free_kbytes=6436787

sysctl -w vm.vfs_cache_pressure=10000

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值