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.mzph.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;中国企业在该赛事上首…

闪存驱动器_将闪存驱动器变成便携式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. 随着我们越来越接近…

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. 现在…

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…

如何使用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表格中最容易被误解的功能之一。 它使…

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.…

谷歌云使用账号密码_如何使用Google密码检查

谷歌云使用账号密码Google has a tool designed to securely analyze your passwords against a database of ones that are known to be compromised and breached. Password Checkup is available as an extension or a web service. Here’s how to use it. Google提供了一种…

Jolicloud是一款适合上网本的漂亮新操作系统

Want to breathe new life into your netbook? Here’s a quick look at Jolicloud, a unique new Linux based OS that lets you use your netbook in a whole new way. 想为您的上网本注入新的活力吗&#xff1f; 快速浏览一下Jolicloud&#xff0c;这是一个独特的基于Linu…

Repeater片段

1.字段过长截取字符串 1.1 截取字符串类 可以直接substring 也可以<%# Utility.Common.GetShow( Eval("NewTitle").ToString(),20,true) %><td><%#fcwms.Common.GetContent.GetShow(Eval("com_address").ToString(), 19, true)%> </t…

谷歌浏览器的翻译功能在哪_如何在Google表格中使用AND和OR功能

谷歌浏览器的翻译功能在哪If you’ve ever wanted to check whether data from your Google Sheets spreadsheet meets certain criteria, you can use AND and OR. These logical functions give you TRUE and FALSE responses, which you can use to sort through your data.…

保存网络文章以供以后使用Instapaper阅读

Have you ever come across a bunch of great articles that you want to read online, but just don’t have the time? Today we take a look at an online service that allows you to read your articles later, either online, or on an iPhone, or eReader. 您是否曾经遇…

谷歌chrome xp_将非Google任务列表添加到Chrome

谷歌chrome xpMost people rely on a task list to help them remember what they need to do but not everyone wants one that is tied to a Google account. If you have been wanting an independent tasks list then join us as we look at the Tasks extension for Googl…

我们生活在最好的时代

2019独角兽企业重金招聘Python工程师标准>>> 没规划的人生叫拼图&#xff0c;有规划的人生叫蓝图&#xff1b; 没目标的人生叫流浪&#xff0c;有目标的人生叫航行&#xff01; 我们生活在最好的时代&#xff1a;在认知和学习机会上&#xff0c;人人平等&#xff0c…

MapReduce详解和WordCount模拟

最早接触大数据&#xff0c;常萦绕耳边的一个词「MapReduce」。它到底是什么&#xff0c;能做什么&#xff0c;原理又是什么&#xff1f;且听下文讲解。 是什么 MapReduce 即是一个编程模型&#xff0c;又是一个计算框架&#xff0c;它充分采用了分治的思想&#xff0c;将数据处…