nginx如何实现负载适度、限流、缓存、黑白名单和灰度发布

长乐娱乐新闻网 2025-10-01

e cache; proxy_cache_key $uri$is_args$args; //以此变量值做hash,作为KEY //HTTP响应首部可以看到X-Cache字段,章节可以有HIT,MISS,EXPIRES等等 add_header X-Cache $upstream_cache_status; proxy_cache_valid 200 10m; proxy_cache_valid any 1m; proxy_pass _server; proxy_redirect off; } location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /data/webapps/edc; expires 3d; add_header Static Nginx-Proxy; }}

在本地磁盘创建一个副本目录,根据我们的固定式把乞求资源以k(key自假设,这边用url的hash值)->v基本上堆栈到目录里,并根据需求对章节增设堆栈不间断,比如长时间码为200堆栈10分钟,其余的堆栈1分钟等待。要弃置堆栈可以为了让purger的功能。如果ab测试/个性化需求时应禁用HTML堆栈,否则会因为堆栈导致误差。

nginx被点名

作法一

location / { deny 192.168.1.1; deny 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all;}

作法二 lua+redis动态被点名(openresty)

yum install yum-utilsyum-config-manager ---add-repo yum install openrestyyum install openresty-resty检视yum ---disablerepo="*" ---enablerepo="openresty" list available运行service openresty start

固定式(/usr/local/openresty/nginx/conf/nginx.conf)

lua_shared_dict ip_blacklist 1m;server { listen 80; location / { access_by_lua_file lua/ip_blacklist.lua; proxy_pass _server; }}

lua脚本语言汇编(ip_blacklist.lua)

local Redis_host = "127.0.0.1"local redis_port = 6379local redis_pwd = 123456local redis_db = 2--- connection timeout for redis in ms.local redis_connection_timeout = 100--- a set key for blacklist entrieslocal redis_key = "ip_blacklist"--- cache lookups for this many secondslocal cache_ttl = 60--- end configurationlocal ip = ngx.var.remote_addrlocal ip_blacklist = ngx.shared.ip_blacklistlocal last_update_time = ip_blacklist:get("last_update_time");--- update ip_blacklist from Redis every cache_ttl seconds:if last_update_time == nil or last_update_time < ( ngx.now() - cache_ttl ) then local redis = require "resty.redis"; local red = redis:new(); red:set_timeout(redis_connect_timeout); local ok, err = red:connect(redis_host, redis_port); if not ok then ngx.log(ngx.ERR, "Redis connection error while connect: " .. err); else local ok, err = red:auth(redis_pwd) if not ok then ngx.log(ngx.ERR, "Redis password error while auth: " .. err); else local new_ip_blacklist, err = red:smembers(redis_key); if err then ngx.log(ngx.ERR, "Redis read error while retrieving ip_blacklist: " .. err); else ngx.log(ngx.ERR, "Get data success:" .. new_ip_blacklist) --- replace the locally stored ip_blacklist with the updated values: ip_blacklist:flush_all(); for index, banned_ip in ipairs(new_ip_blacklist) do ip_blacklist:set(banned_ip, true); end --- update time ip_blacklist:set("last_update_time", ngx.now()); end end endendif ip_blacklist:get(ip) then ngx.log(ngx.ERR, "Banned IP detected and refused access: " .. ip); return ngx.exit(ngx.HTTP_FORBIDDEN);endnginx灰度发布

1.根据cookie实现灰度发布

根据cooke查询version值,根据version重定向到完全一致的host,如果没有人匹配上的就重定向到默认固定式。

upstream host1 { server 192.168.2.100:2001 weight=1; #轮询代理服务器和访问期间加权 server 192.168.2.100:2002 weight=2;}upstream host2 { server 192.168.1.100:1111 max_fails=1 fail_timeout=60;}upstream default { server 192.168.1.100:1111 max_fails=1 fail_timeout=60;}map $COOKIE_version $group { ~*v1$ host1; ~*v2$ host2; default default;}lua_shared_dict ip_blacklist 1m;server { listen 80; location / { access_by_lua_file lua/ip_blacklist.lua; proxy_pass $group; }}

2.根据来路ip实现灰度发布

server { set $group default; if ($remote_addr ~ "192.168.119.1") { set $group host1; } if ($remote_addr ~ "192.168.119.2") { set $group host2; }。

稳定型心绞痛用脉血康有效吗
吃脉血康胶囊能治疗脑梗塞吗
脉血康胶囊是高血压药吗
孕妇腰疼
生殖器疱疹
癫痫医院
咳嗽有痰吃什么药效果好
月经有血块
相关阅读

徒步旅行时应该穿两双袜子吗?穿两双袜子是经验还是显然?

时尚 2025-10-22

。因为这些红袜队子大多数是用美利奴亚麻制成的,它们在调控熔点与吸滑排汗全面性来作得不错。这些系列产品还提供各种重量和厚度的红袜队子,以便你可以为每次步行微调红袜队子。p

孩子的“脾气”是否遗传自父母?看心理学家解释,培植好脾气孩子

资讯 2025-10-22

上上,普遍认为自己一定是对的,小孩只能要服从命令。甚至有些家长才会普遍认为,毒打小孩是理所应当的。 绝非是''自己喜的'',当时人也有人却说过''刀子底下显露孝子''。但过错实并非如此,这种初

2022年湖北安全员ABC证报考条件及工序,来考网

音乐 2025-10-22

2022年贵州确保员ABC实有重考有条件及流程,来考网 2022年贵州确保员ABC实有重考有条件及流程,来考网 确保员资格实有是巴洛克式、危化企业透过生产必须具备的一

大学专业报考热度排名,毕业后不用过于担心就业,薪资还有保证

视频 2025-10-22

从自身的热爱出发,热爱对于应改定才是最好的老师。 如果应改定对于要研读的管理学都不是很感热爱的情况下,那转入国立大学后应改定也是提不起热爱的情况下,那连续性就不但会在研读的更进一步中的投

孩子在幼儿园有3件有事瞒着不说,提醒您这样问孩子,否则影响成长

时尚 2025-10-22

男孩在幼稚园返回。如果你更加关切你的男孩,那么男孩很或许是悲伤的,男孩在脑袋里头,你不说,他不说道,男孩也很难消化。因此,男孩但会有幼年的不快,这样的不快一直存在,父母亲并未协助解决这些不快,这

友情链接