nginx限流健康檢查

Nginx原生限流模塊:
ngx_http_limit_conn_module模塊
根據前端請求域名或ip生成一個key,對于每個key對應的網絡連接數進行限制。
配置如下:
http模塊
server模塊
#http模塊內
http {include       mime.types;default_type  application/octet-stream;log_format main '[$time_local][$msec]$status';sendfile        on;keepalive_timeout  65;proxy_cache_path  /var/nginx/cache  keys_zone=one:10m  levels=1:2  inactive=6h max_size=1g;###限流配置limit_conn_zone $binary_remote_addr zone=perip:10m;limit_conn_log_level info;limit_conn_status 503;    include conf.d/*.conf;
}

  

#server模塊內
server {listen        80;server_name  _;root         /opt/openresty/nginx/html;charset utf-8;proxy_send_timeout 60;proxy_read_timeout 1800s;client_max_body_size 300M ;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;	#---限流配置--#location /limit {limit_conn perip 2;proxy_pass http://backend/cache;}	#-----------#error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}
}

  

驗證:
采用ab測試:ab -n 10 -c 10 120.78.206.183/limit //并發數10個 總請求數10個
nginx:access.log日志
ab測試輸出:
ngx_http_limit_req_module模塊
利用漏桶算法實現。對于指定key進行限流,指定速率處理
配置
驗證:
#http模塊內
http {include       mime.types;default_type  application/octet-stream;log_format main '[$time_local][$msec]$status';sendfile        on;keepalive_timeout  65;proxy_cache_path  /var/nginx/cache  keys_zone=one:10m  levels=1:2  inactive=6h max_size=1g;###限流配置:每s處理一個請求limit_req_zone $binary_remote_addr zone=req:10m rate=1r/s;limit_conn_log_level info;limit_conn_status 503;    include conf.d/*.conf;
}

  

server {listen        80;server_name  _;root         /opt/openresty/nginx/html;charset utf-8;proxy_send_timeout 60;proxy_read_timeout 1800s;client_max_body_size 300M ;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#zone=one :設置使用哪個配置區域來做限制,與上面limit_req_zone 里的name對應#burst=5:設置一個大小為5的緩沖區當有大量請求(爆發)過來時,超過了訪問頻次限制的請求可以先放到這個緩沖區內等待,但是這個等待區里的位置只有5個,超過的請求會直接報503的錯誤然后返回。#nodelay:#   如果設置,會在瞬時提供處理(burst + rate)個請求的能力,請求超過(burst + rate)的時候就會直接返回503,永遠不存在請求需要等待的情況。(這里的rate的單位是:r/s)#   如果沒有設置,則所有請求會依次等待排隊location /limit_req {limit_req zone=req burst=3 nodelay;proxy_pass http://backend/cache;}	error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}
}

  

采用ab測試:ab -n 10 -c 10 120.78.206.183/limit_req //并發數10個 總請求數10個
ab測試工具展示:
OpenResty限流模塊:
lua-resty-limit-traffic:
github: https://github.com/openresty/lua-resty-limit-traffic/tree/master/lib/resty/limit
包含四個模塊:
  • conn:限制并發數
  • count:給定時間窗口內通過固定數量的請求限制請求率
  • req:請求速率限制
  • traffic:可以自由組合多種限流策略
配置并發限流如下:
http {include       mime.types;default_type  application/octet-stream;log_format main '[$time_local][$msec]$status';sendfile        on;keepalive_timeout  65;lua_shared_dict my_limit_conn_store 100m;limit_conn_log_level info;limit_conn_status 503;    include conf.d/*.conf;
}

  

server {listen        80;server_name  _;root         /opt/openresty/nginx/html;charset utf-8;proxy_send_timeout 60;proxy_read_timeout 1800s;client_max_body_size 300M ;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#限制接口總并發數location /limit_lua_conn {access_by_lua_block {local limit_conn = require "resty.limit.conn"-- 限制一個 ip 客戶端最大 1 個并發請求-- burst 設置為 0,如果超過最大的并發請求數,則直接返回503,-- 如果此處要允許突增的并發數,可以修改 burst 的值(漏桶的桶容量)-- 最后一個參數其實是你要預估這些并發(或者說單個請求)要處理多久,以便于對桶里面的請求應用漏桶算法local lim, err = limit_conn.new("my_limit_conn_store",2,1,0.5)if not lim thenngx.log(ngx.ERR,"限流:",err)return ngx.exit(503)endlocal key = ngx.var.binary_remote_addrlocal delay, err = lim:incoming(key, true)if not delay thenif err == "rejected" thenreturn ngx.exit(503)endngx.log(ngx.ERR, "failed to limit req:", err)return ngx.exit(500)end}proxy_pass http://backend/cache;}#error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}
}

 

驗證結果:
ab -n 10 -c 10 120.78.206.183/limit_lua_conn
nginx日志:
ab結果:
?
Nginx健康檢查機制
nginx默認檢查機制
測試:后端兩臺服務器:
max_fails:定義定義可以發生錯誤的最大次數
fail_timeout:nginx在fail_timeout設定的時間內與后端服務器通信失敗的次數超過max_fails設定的次數,則認為這個服務器不在起作用;在接下來的 fail_timeout時間內,nginx不再將請求分發給失效的server。
后端默認配置
前端請求:
請求多次,后端服務均有日志產生
120.78.206.183機器
14.116.196.138機器
停掉一臺?14.116.196.138,請求正常返回:
nginx日志:
結論:
1.nginx健康檢查機制為被動檢查。
2.在fail_timeout時間內,如果服務器節點在請求max_fails次數都不返回,在這fail_timeout內,請求不會向這臺服務器轉發,fail_timeout指定的超時時間到了,再次發起請求,就按照輪轉規則,該到這臺服務器還是會過去,這時候再經歷fail_timeout指定時間,請求不會到這臺服務器
Nginx第三方模塊健康檢查模塊:
主動檢查:
第三方模塊:
1.nginx_upstream_check_module
主要配置:
 
upstream name{server 192.168.0.21:80;server 192.168.0.22:80;check interval=3000 rise=2 fall=5 timeout=1000;
}
#對所有節點,每個3秒檢測一次,請求2次正常則標記 realserver狀態為up,如果檢測 5 次都失敗,則標記 realserver的狀態為down,超時時間為1秒

 

?


2.openresty模塊:lua-resty-upstream-healthcheck
http {upstream backend {server 120.78.206.183:8080;server 14.116.196.138:8002;}lua_shared_dict healthcheck 1m;lua_socket_log_errors off;init_worker_by_lua_block {local hc = require "resty.upstream.healthcheck"local ok, err = hc.spawn_checker {shm = "healthcheck",upstream = "tomcat",type = "http",#指定后端健康檢查http請求接口    http_req = "GET /nginx HTTP/1.0\r\nHost: tomcat\r\n\r\n",interval = 2000,timeout = 5000,fall = 3,rise = 2,#http請求接口返回200,302表示服務端正常valid_statuses = {200, 302},concurrency = 1,}if not ok thenngx.log(ngx.ERR, "=======> failed to spawn health checker: ", err)returnend}server {listen      80;server_name localhost;location ^~ /cache {proxy_cache one;proxy_no_cache $http_soapaction;proxy_cache_key $request_body;proxy_cache_valid 200 302 10m;proxy_cache_methods GET POST;proxy_ignore_headers Cache-Control Set-Cookie;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://backend/cache;}location /server/status {access_log off;default_type text/plain;content_by_lua_block {local hc = require "resty.upstream.healthcheck"ngx.say("Nginx Worker PID: ", ngx.worker.pid())ngx.print(hc.status_page())}}}
}

 

配置2s時間間隔探測:
access.log:
在nginx訪問日志中每隔2s健康檢查請求一次
kill掉任意一臺后端服務:
nginx error.log日志
會持續檢查指定3次:上面fall參數指定
請求nginx后端健康檢查探測接口
多次請求后端接口:error.log日志無變化,說明請求不會路由到down機器上
重啟啟動down機器,再次請求nginx探測接口

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">



來自為知筆記(Wiz)



轉載于:https://www.cnblogs.com/HushAsy/p/10302291.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278432.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278432.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278432.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何在Ubuntu上創建桌面快捷方式

Desktop icons should be simple, but they’re not on Ubuntu 18.04 LTS and newer releases like Ubuntu 19.10. Follow these easy steps to get desktop shortcuts for your favorite applications, just like on other operating systems and other Linux desktops. 桌面圖…

阿里再破記錄!代表中國企業首次在這項國際比賽中摘得銀牌!

2月9日在洛杉磯舉行的第11屆網絡搜索與數據挖掘國際會議&#xff08;WSDM 2018&#xff09;上&#xff0c;公布了今年的WSDM Cup競賽成績&#xff0c;來自阿里巴巴的AliOS團隊憑借優秀的算法能力&#xff0c;摘得榜眼。這是該賽事舉辦11屆以來&#xff0c;中國企業在該賽事上首…

關于IE 對 $.get 緩存的記錄

最近在IE9中碰到一個問題是&#xff0c; 當我對某個角色進行修改的時候&#xff0c;再點擊查詢還是修改之前的內容&#xff0c;但是實際數據庫已經修改成功&#xff0c;糾結了好一會兒之后&#xff0c;才發現是 $.get請求的問題。 因為 IE對get請求&#xff0c;如果請求url 相…

閃存驅動器_將閃存驅動器變成便攜式Web服務器

閃存驅動器Portable applications are very useful for getting work done on the go, but how about portable servers? Here’s how you can turn your flash drive into a portable web server. 便攜式應用程序對于在旅途中完成工作非常有用&#xff0c;但是便攜式服務器呢…

Android中文API-ViewStub

ViewStub控件是一個不可見&#xff0c;0尺寸得惰性控件。當ViewStub控件設置可見&#xff0c;或者調用inflate()&#xff0c;并運行完畢之后&#xff0c;ViewStub所指定的layout資源就會被載入。這個ViewStub就會被新載入的layout文件取代。ViewStub也會從其父控件中移除。因此…

如何播放梅西百貨的感恩節大游行2019

Macy’s梅西百貨As we draw ever closer to the Thanksgiving holiday, multiple things come to mind: turkey, Black Friday, and the Macy’s Thanksgiving Day Parade. With that in mind, you might want to find a way to stream it for your family. 隨著我們越來越接近…

thinkpaidE480office安裝文件夾

C:\Program Files (x86)\Microsoft Office\root\Office16需要嵌入office的小伙伴自行百度吧教程吧&#xff0c;網上有很多的轉載于:https://www.cnblogs.com/hgyzm/p/10303449.html

AJAX入門這一篇就夠了

什么是Ajax Ajax(Asynchronous JavaScript and XML) 異步JavaScript和XML Ajax實際上是下面這幾種技術的融合&#xff1a; (1)XHTML和CSS的基于標準的表示技術(2)DOM進行動態顯示和交互(3)XML和XSLT進行數據交換和處理(4)XMLHttpRequest進行異步數據檢索(5)Javascript將以上技術…

如何在iPhone和iPad上允許“不受信任的快捷方式”

Khamosh PathakKhamosh PathakShortcuts is now a stock app in iOS 13, iPadOS 13, and beyond. Thanks to Apple’s stricter rules, any shortcut you download from the internet is blocked. Here’s how you can allow untrusted shortcuts on your iPhone or iPad. 現在…

程序員技術進階手冊(一)

AI正在迅速改變世界&#xff0c;對于程序員來說&#xff0c;這絕對是一個千載難逢的轉型機會。機器學習是計算機科學的一個子領域&#xff0c;在人工智能領域&#xff0c;機器學習逐漸發展成模式識別和計算科學理論的研究。從2016年起&#xff0c;機器學習到達了不合理的火熱巔…

SpringBoot入門 (一) HelloWorld

一 什么是springboot springboot是一個全新的框架&#xff0c;它設計的目的簡化spring項目的初始環境的搭建和開發&#xff0c;主要有以下幾個特點&#xff1a; 1、簡化初始配置 &#xff0c;可與主流框架集成&#xff1b; 2、內置Servlet容器&#xff0c;無需在打War包&#x…

gmail附件調用_如何將Gmail附件保存到Google云端硬盤

gmail附件調用While you can access Gmail attachments by opening the related message deep within Google’s client, it’s not very convenient. You need a central location to access saved documents and images. This guide shows you how to save Gmail attachments…

spring boot攔截器中獲取request post請求中的參數(轉)

文章轉自 https://www.jianshu.com/p/69c6fba08c92 轉載于:https://www.cnblogs.com/shuaiandjun/p/10306242.html

絕地求生大逃殺,改配置

提取效果設置配置文件 通過Procmon工具分析&#xff0c;絕地求生大逃殺效果設置的配置文件為 “C:\Users\Administrator\AppData\Local\TslGame\Saved\Config\WindowsNoEditor\GameUserSettings.ini”&#xff0c;設置好網吧需要的游戲效果后將“TslGame”文件夾提取出來即可&a…

如何使用VLOOKUP在Google表格中查找數據

VLOOKUP is one of the most misunderstood functions in Google Sheets. It allows you to search through and link together two sets of data in your spreadsheet with a single search value. Here’s how to use it. VLOOKUP是Google表格中最容易被誤解的功能之一。 它使…

共享內存

https://blog.csdn.net/tojohnonly/article/details/70246965 轉載于:https://www.cnblogs.com/132818Creator/p/10307072.html

WPF項目學習.一

WPF項目搭建 版權聲明&#xff1a;本文為博主初學經驗&#xff0c;未經博主允許不得轉載。 一、前言 記錄在學習與制作WPF過程中遇到的解決方案。 使用MVVM的優點是 數據和視圖分離&#xff0c;雙向綁定&#xff0c;低耦合&#xff0c;可重用行&#xff0c;相對獨立的設計和邏輯…

airpods_如何通過AirPods與其他人共享音樂

airpodsKhamosh PathakKhamosh PathakUsing the new Audio Sharing feature introduced in iOS 13.1 and iPadOS 13.1, you can share audio from one iPhone with two AirPods. You can watch a video or listen to a song along with your friend in just a tap! 使用iOS 13.…

Laravel 5 多個視圖共享數據的方法

我們都知道模板一般會用到繼承&#xff0c;導航欄就是一個很好的例子&#xff0c;但是導航欄的數據如何共享&#xff0c;比如有個導航的文件叫在view/navigation.blade.php為了簡單一點&#xff0c;文件里只有設置了一個變量1{{ $cqh }}現在的要求是每個頁面都會用到這個變量&a…

HR面 - 十大經典提問

1、HR&#xff1a;你希望通過這份工作獲得什么&#xff1f; 1&#xff09;、自殺式回答&#xff1a;我希望自己為之工作的企業能夠重視質量&#xff0c;而且會給做得好的員工予以獎勵。我希望通過這份工作鍛煉自己&#xff0c;提升自己的能力&#xff0c;能讓公司更加重視我。 …