Nginx负载均衡后端健康检查
参考文档:https://www.cnblogs.com/kevingrace/p/6685698.html
本次使用第三方模块nginx_upstream_check_module的,要使用这个第三方模块首先您需要进行下载,然后通过patch命令将补丁打入您原有的Nginx源码中,并且重新进行编译安装。
下载nginx_upstream_check_module模块
1 | wget https: / / codeload.github.com / yaoweibin / nginx_upstream_check_module / zip / master |
解压
1 | unzip master |
解压后的Nginx和nginx_upstream_check_module-master在同一目录下
将补丁打入源码(没有patch命令使用yum -y install patch安装)
1 2 | cd nginx - 1.6 . 3 patch - p1 < .. / nginx_upstream_check_module - master / check_1. 5.12 + .patch |
出现以下代表成功(根据nginx版本选择不同的check)
编译安装nginx
1 2 | . / configure - - prefix = / usr / local / nginx - - add - module = .. / nginx_upstream_check_module - master - - with - http_stub_status_module make && make install |
通过以上的步骤,第三方的nginx_upstream_check_module模块就在Nginx中准备好了。接下来我们讲解一下如何使用这个模块。首先看一下upstream的配置信息
interval:必要参数,检查请求的间隔时间。
fall:当检查失败次数超过了fall,这个服务节点就变成down状态。
rise:当检查成功的次数超过了rise,这个服务节点又会变成up状态。
timeout:请求超时时间,超过等待时间后,这次检查就算失败。
default_down:后端服务器的初始状态。默认情况下,检查功能在Nginx启动的时候将会把所有后端节点的状态置为down,检查成功后,在置为up。
type:这是检查通信的协议类型,默认为http。以上类型是检查功能所支持的所有协议类型。
一个完整的nginx配置信息如下nginx.conf
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 | worker_processes 4 ; error_log logs / error.log; events { worker_connections 10240 ; } http { include mime.types; default_type application / octet - stream; sendfile on; keepalive_timeout 65 ; upstream ekp { server 10.1 . 1.131 : 8080 ; server 10.1 . 1.132 : 8080 ; server 10.1 . 1.135 : 8080 ; check interval = 3000 rise = 2 fall = 5 ; check_keepalive_requests 100 ; check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n" ; check_http_expect_alive http_2xx http_3xx; } server { listen 80 ; server_name www.test.com; access_log / usr / local / nginx / logs / access.log; location / { root ekp; index index.html index.htm index.jsp; proxy_pass http: / / ekp; proxy_set_header Host $host; proxy_set_header X - Real - IP $remote_addr; proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for; client_max_body_size 200m ; proxy_connect_timeout 10 ; proxy_read_timeout 300 ; proxy_send_timeout 300 ; } location / nstatus { check_status; access_log on; } } } |
PS:这里配置按照参考文档加timeout=1000
type
=http 否则无法负载均衡
访问测试 http://ip/nstatus
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!