限流是什么意思(Nginx)
admin
2023-09-24 11:27:01

1.安装依赖环境

yum-yinstallwgetgcc-c ncursesncrussenscurses-develgcc * libxml 2 libxml 2-devel curl-develolibjpeg * libpng * freetype * autoconfautomakezlib * fiex * libxml * libmcrypt * libtool-ltdl-devel * libaiolibaio-develbzrlibtool

2.安装openssl

wget 3359 www .OpenSSL。org/source/open/configure-prefix=/usr/local/pcre-8.43 make make install

3.安装pcre

wget https://SourceForge。net/projects/libpng/files/zlib/1。2 .11/zlib-1。2 .11 .焦油。gztar-zxvfzlib-1。2 .11 .焦油。gzcd/usr/local/src/zlib-1。2 .11。/configure-prefix=/usr/local/zlib-1。2 .11使使

4.安装zlib

wget http://nginx。org/download/nginx-1。17 .2 .焦油。gztar-zxvfnginx-1。17 .2 .焦油。gzcd/usr/local/src/nginx-1。17 .2 ./configure-prefix=/usr/local/nginx-1。17 .2-with-OpenSSL=/usr/local/src/OpenSSL-1。0 .2s-with-pcre=/usr/local/src/pcre-8.43-with-zlib=/usr/local/src/zlib-1。2 .11-with-http _ SSL _ modulemake安装这里需要注意的是:安装Nginx时,指定的是openssl、pcre和zlib的源码解压目录,安装完成后Nginx配置文件的完整路径为:/usr/local/nginx-1.17.2/conf/nginx.conf。

5.安装Nginx

Nginx负载均衡配置

http {……上游real _ server { server 192。168 .103 .10033602001权重=1;#轮询服务器和访问权重192号服务器。168 .103 .33602002权重=2;}服务器{ listen 80 location/{ proxy _ pass http://real _ server;} } }

1.负载均衡配置

上游real _ server { server 192。168 .103 .10033602001 weight=1 max _ fails=2 fail _ time out=60s;192号服务器。168 .103 .100:2002 weight=2 max _ fails=2 fail _ time out=60s;}意思是在失败_超时时间内失败了最大失败次数次请求后,则认为该上游服务器不可用,然后将该服务地址踢除掉失败_超时时间后会再次将该服务器加入存活列表,进行重试。

2.失败重试配置

Nginx限流配置

限制请求区域指令设置参数

limit _ req _ zone $ binary _ remote _ addr zone=my limit :10 mrate=10r/s;限制请求区域定义在超文本传送协议(超文本传输协议的缩写)块中,$binary_remote_addr表示保存客户端互联网协议(互联网协议)地址的二进制形式100区。定义互联网协议(互联网协议)状态及统一资源定位器访问频率的共享内存区域区域=关键字标识区域的名字,以及冒号后面跟区域大小。16000个互联网协议(互联网协议)地址的状态信息约1MB,所以示例中区域可以存储160000个互联网协议(互联网协议)地址100%的比率定义最大请求速率。示例中速率不能超过每秒10个请求。

1.配置参数

ode>location/{limit_reqzone=mylimitburst=20nodelay;proxy_passhttp://real_server;}

burst排队大小,nodelay不限制单个请求间的时间。

3.不限流白名单

geo$limit{default1;192.168.2.0/240;}map$limit$limit_key{1$binary_remote_addr;0"";}limit_req_zone$limit_keyzone=mylimit:10mrate=1r/s;location/{limit_reqzone=mylimitburst=1nodelay;proxy_passhttp://real_server;}

上述配置中,192.168.2.0/24网段的IP访问是不限流的,其他限流。

IP后面的数字含义:

24表示子网掩码:255.255.255.016表示子网掩码:255.255.0.08表示子网掩码:255.0.0.0

Nginx缓存配置

1.浏览器缓存

静态资源缓存用expire

location~*.(jpg|jpeg|png|gif|ico|css|js)${expires2d;}

ResponseHeader中添加了Expires和Cache-Control,

静态资源包括(一般缓存)

普通不变的图像,如logo,图标等js、css静态文件可下载的内容,媒体文件

协商缓存(add_headerETag/Last-Modifiedvalue)

HTML文件经常替换的图片经常修改的js、css文件基本不变的API接口

不需要缓存

用户隐私等敏感数据经常改变的api数据接口

2.代理层缓存

//缓存路径,inactive表示缓存的时间,到期之后将会把缓存清理proxy_cache_path/data/cache/nginx/levels=1:2keys_zone=cache:512minactive=1dmax_size=8g;location/{location~\.(htm|html)?${proxy_cachecache;proxy_cache_key$uri$is_args$args;//以此变量值做HASH,作为KEY//HTTP响应首部可以看到X-Cache字段,内容可以有HIT,MISS,EXPIRES等等add_headerX-Cache$upstream_cache_status;proxy_cache_valid20010m;proxy_cache_validany1m;proxy_passhttp://real_server;proxy_redirectoff;}location~.*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)${root/data/webapps/edc;expires3d;add_headerStaticNginx-Proxy;}}

在本地磁盘创建一个文件目录,根据设置,将请求的资源以K-V形式缓存在此目录当中,KEY需要自己定义(这里用的是url的hash值),同时可以根据需要指定某内容的缓存时长,比如状态码为200缓存10分钟,状态码为301,302的缓存5分钟,其他所有内容缓存1分钟等等。
可以通过purger的功能清理缓存。

AB测试/个性化需求时应禁用掉浏览器缓存。

Nginx黑名单

1.一般配置

location/{deny192.168.1.1;deny192.168.1.0/24;allow10.1.1.0/16;allow2001:0db8::/32;denyall;}

2.Lua+Redis动态黑名单(OpenResty)

安装运行

yuminstallyum-utilsyum-config-manager--add-repohttps://openresty.org/package/centos/openresty.repoyuminstallopenrestyyuminstallopenresty-resty查看yum--disablerepo="*"--enablerepo="openresty"listavailable运行serviceopenrestystart

配置(/usr/local/openresty/nginx/conf/nginx.conf)

lua_shared_dictip_blacklist1m;server{listen80;location/{access_by_lua_filelua/ip_blacklist.lua;proxy_passhttp://real_server;}}

lua脚本(ip_blacklist.lua)

localredis_host="192.168.1.132"localredis_port=6379localredis_pwd=123456localredis_db=2--connectiontimeoutforredisinms.localredis_connection_timeout=100--asetkeyforblacklistentrieslocalredis_key="ip_blacklist"--cachelookupsforthismanysecondslocalcache_ttl=60--endconfigurationlocalip=ngx.var.remote_addrlocalip_blacklist=ngx.shared.ip_blacklistlocallast_update_time=ip_blacklist:get("last_update_time");--updateip_blacklistfromRediseverycache_ttlseconds:iflast_update_time==nilorlast_update_time<(ngx.now()-cache_ttl)thenlocalredis=require"resty.redis";localred=redis:new();red:set_timeout(redis_connect_timeout);localok,err=red:connect(redis_host,redis_port);ifnotokthenngx.log(ngx.ERR,"Redisconnectionerrorwhileconnect:"..err);elselocalok,err=red:auth(redis_pwd)ifnotokthenngx.log(ngx.ERR,"Redispassworderrorwhileauth:"..err);elselocalnew_ip_blacklist,err=red:smembers(redis_key);iferrthenngx.log(ngx.ERR,"Redisreaderrorwhileretrievingip_blacklist:"..err);elsengx.log(ngx.ERR,"Getdatasuccess:"..new_ip_blacklist)--replacethelocallystoredip_blacklistwiththeupdatedvalues:ip_blacklist:flush_all();forindex,banned_ipinipairs(new_ip_blacklist)doip_blacklist:set(banned_ip,true);end--updatetimeip_blacklist:set("last_update_time",ngx.now());endendendendifip_blacklist:get(ip)thenngx.log(ngx.ERR,"BannedIPdetectedandrefusedaccess:"..ip);returnngx.exit(ngx.HTTP_FORBIDDEN);end


Nginx灰度发布

1.根据Cookie实现灰度发布

根据Cookie查询version值,如果该version值为v1转发到host1,为v2转发到host2,都不匹配的情况下转发到默认配置。

upstreamhost1{server192.168.2.46:2001weight=1;#轮询服务器和访问权重server192.168.2.46:2002weight=2;}upstreamhost2{server192.168.1.155:1111max_fails=1fail_timeout=60;}upstreamdefault{server192.168.1.153:1111max_fails=1fail_timeout=60;}map$COOKIE_version$group{~*v1$host1;~*v2$host2;defaultdefault;}lua_shared_dictip_blacklist1m;server{listen80;#set$group"default";#if($http_cookie~*"version=v1"){#set$grouphost1;#}#if($http_cookie~*"version=v2"){#set$grouphost2;#}location/{access_by_lua_filelua/ip_blacklist.lua;proxy_passhttp://$group;}}

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

server{……………set$groupdefault;if($remote_addr~"192.168.119.1"){set$grouphost1;}if($remote_addr~"192.168.119.2"){set$grouphost2;}

3.更细粒度灰度发布

参考:

https://github.com/sunshinelyz/ABTestingGateway



相关内容

热门资讯

金花创建房间/微信金花房卡怎么... 1.微信渠道:(荣耀联盟)大厅介绍:咨询房/卡添加微信:88355042 2.微信游戏中心:打开微...
金花房间卡/金花房卡如何购买/... 金花房间卡/金花房卡如何购买/新超圣金花房卡正版如何购买新超圣是一款非常受欢迎的游戏,咨询房/卡添加...
牛牛创建房间/金花房卡批发/神... 微信游戏中心:神牛大厅房卡在哪里买打开微信,添加客服【88355042】,进入游戏中心或相关小程序,...
链接牛牛/牛牛房卡游戏代理/鸿... 鸿运大厅房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
科技实测!牛牛房卡怎么获得/乐... 微信游戏中心:乐酷大厅房卡在哪里买打开微信,添加客服【88355042】,进入游戏中心或相关小程序,...