Nginx常见配置:负载均衡、限流、缓存、黑名单和灰度发布
一、Nginx安裝(基于CentOS 6.5)
1.yum命令安裝
yum install nginx –y
(若不能安裝,執(zhí)行命令yum install epel-release)
2. 啟動(dòng)、停止和重啟
service nginx start
service nginx stop
service nginx restart
瀏覽器中 輸入服務(wù)器的 ip 地址,即可看到相應(yīng)信息
3. 其他信息
rpm -ql nginx?來查看安裝路徑
yum remove nginx?來卸載?
nginx -s reload 配置熱更新?
二、Nginx負(fù)載均衡配置(/etc/nginx/nginx.conf)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | http { ???????…… ????upstream real_server { ???????server 192.168.103.100:2001 weight=1;??#輪詢服務(wù)器和訪問權(quán)重 ???????server 192.168.103.100:2002 weight=2; ????} ????server { ????????listen? 80; ????????location / { ????????????proxy_pass http://real_server; ????????} ????} } |
| 1 2 3 4 | upstream real_server { ???server 192.168.103.100:2001 weight=1 max_fails=2 fail_timeout=60s; ???server 192.168.103.100:2002 weight=2 max_fails=2 fail_timeout=60s; } |
意思是在fail_timeout時(shí)間內(nèi)失敗了max_fails次請(qǐng)求后,則認(rèn)為該上游服務(wù)器不可用,然后將該服務(wù)地址踢除掉。fail_timeout時(shí)間后會(huì)再次將該服務(wù)器加入存活列表,進(jìn)行重試。
三、Nginx限流配置
limit_req_zone指令設(shè)置參數(shù)?
| 1 | limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; |
1)limit_req_zone定義在http塊中,$binary_remote_addr表示保存客戶端IP地址的二進(jìn)制形式。
2)Zone定義IP狀態(tài)及URL訪問頻率的共享內(nèi)存區(qū)域。zone=keyword標(biāo)識(shí)區(qū)域的名字,以及冒號(hào)后面跟區(qū)域大小。16000個(gè)IP地址的狀態(tài)信息約1MB,所以示例中區(qū)域可以存儲(chǔ)160000個(gè)IP地址。
3)Rate定義最大請(qǐng)求速率。示例中速率不能超過每秒10個(gè)請(qǐng)求。
2.設(shè)置限流
| 1 2 3 4 | location / { ????????limit_req zone=mylimit burst=20 nodelay; ????????proxy_pass http://real_server; } |
burst排隊(duì)大小,nodelay不限制單個(gè)請(qǐng)求間的時(shí)間
3.不限流白名單
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | geo $limit { default????????????? 1; 192.168.2.0/24??0; } map $limit $limit_key { 1 $binary_remote_addr; 0?""; } limit_req_zone $limit_key zone=mylimit:10m rate=1r/s; location / { ????????limit_req zone=mylimit burst=1 nodelay; ????????proxy_pass http://real_server; } |
上述配置中,192.168.2.0/24網(wǎng)段的IP訪問是不限流的,其他限流。
IP后面的數(shù)字含義:
24表示子網(wǎng)掩碼:255.255.255.0
16表示子網(wǎng)掩碼:255.255.0.0
8表示子網(wǎng)掩碼:255.0.0.0
四、Nginx緩存配置
靜態(tài)資源緩存用expire
| 1 2 3 | location ~*? .(jpg|jpeg|png|gif|ico|css|js)$ { ???expires 2d; } |
Response Header中添加了Expires和Cache-Control
靜態(tài)資源包括(一般緩存)
1)普通不變的圖像,如logo,圖標(biāo)等
2)js、css靜態(tài)文件
3)可下載的內(nèi)容,媒體文件
協(xié)商緩存(add_header ETag/Last-Modified value)
1)HTML文件
2)經(jīng)常替換的圖片
3)經(jīng)常修改的js、css文件
4)基本不變的API接口
不需要緩存
1)用戶隱私等敏感數(shù)據(jù)
2)經(jīng)常改變的api數(shù)據(jù)接口
2.代理層緩存
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //緩存路徑,inactive表示緩存的時(shí)間,到期之后將會(huì)把緩存清理 proxy_cache_path?/data/cache/nginx/?levels=1:2 keys_zone=cache:512m inactive = 1d max_size=8g; location / { ????location ~ \.(htm|html)?$ { ????????proxy_cache cache; ????????proxy_cache_key??? $uri$is_args$args;?????//以此變量值做HASH,作為KEY ????????//HTTP響應(yīng)首部可以看到X-Cache字段,內(nèi)容可以有HIT,MISS,EXPIRES等等 ????????add_header X-Cache $upstream_cache_status; ????????proxy_cache_valid 200 10m; ????????proxy_cache_valid any 1m; ????????proxy_pass? http://real_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; ????} } |
? ? ? ? 在本地磁盤創(chuàng)建一個(gè)文件目錄,根據(jù)設(shè)置,將請(qǐng)求的資源以K-V形式緩存在此目錄當(dāng)中,KEY需要自己定義(這里用的是url的hash值),同時(shí)可以根據(jù)需要指定某內(nèi)容的緩存時(shí)長,比如狀態(tài)碼為200緩存10分鐘,狀態(tài)碼為301,302的緩存5分鐘,其他所有內(nèi)容緩存1分鐘等等。
? ? ? ? 可以通過purger的功能清理緩存。
? ? ? ? AB測(cè)試/個(gè)性化需求時(shí)應(yīng)禁用掉瀏覽器緩存。
五、Nginx黑名單
1.一般配置
| 1 2 3 4 5 6 7 | location / { ????deny? 192.168.1.1; ????deny 192.168.1.0/24; ????allow 10.1.1.0/16; ????allow 2001:0db8::/32; ????deny? all; } |
2. Lua+Redis動(dòng)態(tài)黑名單(OpenResty)
1)安裝運(yùn)行
| 1 2 3 4 5 6 7 8 | yum?install?yum-utils yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo yum?install?openresty yum?install?openresty-resty 查看 yum --disablerepo="*"?--enablerepo="openresty"?list available 運(yùn)行 service openresty start |
2) 配置(/usr/local/openresty/nginx/conf/nginx.conf)
| 1 2 3 4 5 6 7 8 9 10 | lua_shared_dict ip_blacklist 1m; server { ????listen? 80; ????location / { ????????access_by_lua_file lua/ip_blacklist.lua; ????????proxy_pass http://real_server; ????} } |
lua腳本(ip_blacklist.lua)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | local redis_host??? =?"192.168.1.132" local redis_port??? =?6379 local redis_pwd???? =?123456 local redis_db =?2 -- connection timeout?for?redis?in?ms. local redis_connection_timeout =?100 -- a?set?key?for?blacklist entries local redis_key???? =?"ip_blacklist" -- cache lookups?for?this?many seconds local cache_ttl???? =?60 -- end configuration local ip??????????????? = ngx.var.remote_addr local ip_blacklist????? = ngx.shared.ip_blacklist local 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 ??end end if?ip_blacklist:get(ip) then ??ngx.log(ngx.ERR,?"Banned IP detected and refused access: "?.. ip); ??return?ngx.exit(ngx.HTTP_FORBIDDEN); end |
六、Nginx灰度發(fā)布
根據(jù)Cookie查詢version值,如果該version值為v1轉(zhuǎn)發(fā)到host1,為v2轉(zhuǎn)發(fā)到host2,都不匹配的情況下轉(zhuǎn)發(fā)默認(rèn)。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | upstream host1 { ???server 192.168.2.46:2001 weight=1;??#輪詢服務(wù)器和訪問權(quán)重 ???server 192.168.2.46:2002 weight=2; } upstream host2 { ???server 192.168.1.155:1111? max_fails=1 fail_timeout=60; } upstream default { ???server 192.168.1.153: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; ????#set $group "default"; ????#if ($http_cookie ~* "version=v1"){ ????#??? set $group host1; ????#} ????#if ($http_cookie ~* "version=v2"){ ????#??? set $group host2; ????#} ????location / { ????????access_by_lua_file lua/ip_blacklist.lua; ????????proxy_pass http://$group; ????} } |
2. 根據(jù)來路IP實(shí)現(xiàn)灰度發(fā)布
| 1 2 3 4 5 6 7 8 9 | server { ??…………… ??set?$group default; ??if?($remote_addr ~?"192.168.119.1") { ??????set?$group host1; ??} ??if?($remote_addr ~?"192.168.119.2") { ??????set?$group host2; ??} |
3. 更細(xì)粒度灰度發(fā)布
可用lua腳本實(shí)現(xiàn),參考開源項(xiàng)目:https://github.com/dayulxl/ABTestingGateway
轉(zhuǎn)載于:https://www.cnblogs.com/ZenoLiang/p/10946126.html
總結(jié)
以上是生活随笔為你收集整理的Nginx常见配置:负载均衡、限流、缓存、黑名单和灰度发布的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓前端布局Android,Androi
- 下一篇: Nginx实用指南V1 (连载之六:ca