linux中用shell脚本自动化汇总nginx负载均衡节点健康状态,需依赖nginx_upstream_check_module等扩展模块暴露/status接口,通过curl获取html状态页,用grep、awk提取ip:port、status、rise、fall、type字段,结构化输出并高亮显示up/down状态,支持定时检查与邮件告警。

在 Linux 中用 Shell 脚本自动化汇总 Nginx 负载均衡节点的健康状态,核心思路是:通过解析 Nginx 的 stub_status 或第三方模块(如 nginx_upstream_check_module)暴露的健康检查接口,结合 curl、grep、awk 等工具提取关键字段,再统一格式输出或写入日志/监控系统。
确认 Nginx 是否启用健康检查接口
原生 Nginx 不提供节点级健康状态 API,需依赖扩展模块:
- 若已编译安装
nginx_upstream_check_module(常见于 OpenResty 或定制版),确保配置中启用了check指令,例如:
upstream backend {
check interval=3 rise=2 fall=5 timeout=1;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}并配置一个 location 暴露检查状态页(如 /status):
location /status {
check_status;
access_log off;
allow 127.0.0.1;
allow 10.0.0.0/8;
deny all;
}验证是否可用:curl http://localhost/status 应返回类似 HTML 表格的节点状态(UP/DOWN、rise/fall 计数等)。
Shell 脚本提取并结构化健康状态
以下是一个轻量、可直接运行的脚本示例(保存为 nginx_health_check.sh):
#!/bin/bash
NGINX_STATUS_URL="http://127.0.0.1/status"
TMP_FILE=$(mktemp)
<h1>获取状态页内容,过滤出节点行(跳过表头和空行)</h1><p>curl -s "$NGINX_STATUS_URL" 2>/dev/null | \
grep -E '</p><tr>
<td>[0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+</td>' | \
sed -e 's/]*>/ /g' -e 's/[[:space:]]+/ /g' -e 's/^ //;s/ $//' > "$TMP_FILE"<p>echo "=== Nginx Upstream Health Status ($(date)) ==="
printf "%-15s %-8s %-6s %-6s %-6s\n" "IP:PORT" "STATUS" "RISE" "FALL" "TYPE"
printf "%-15s %-8s %-6s %-6s %-6s\n" "------" "------" "----" "----" "----"</p>
<p>while IFS= read -r line; do
[[ -z "$line" ]] && continue
ip_port=$(echo "$line" | awk '{print $1}')
status=$(echo "$line" | awk '{print $2}')
rise=$(echo "$line" | awk '{print $3}')
fall=$(echo "$line" | awk '{print $4}')
type=$(echo "$line" | awk '{print $5}')</p>
<h1>状态高亮:UP 为绿色,DOWN 为红色(仅终端显示)</h1>
<p>if [[ "$status" == "UP" ]]; then
printf "\033[32m%-15s %-8s %-6s %-6s %-6s\033[0m\n" "$ip_port" "$status" "$rise" "$fall" "$type"
else
printf "\033[31m%-15s %-8s %-6s %-6s %-6s\033[0m\n" "$ip_port" "$status" "$rise" "$fall" "$type"
fi
done </p>
<p>rm -f "$TMP_FILE"</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill8828" title="Obsidian (Linux)"><img
src="https://img.php.cn/upload/skill/000/000/081/179228531040284.jpg" alt="Obsidian (Linux)" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill8828" title="Obsidian (Linux)" class="overflowclass">Obsidian (Linux)</a>
<p class="overflowclass">操作 Obsidian 库(纯 Markdown 笔记),并通过 notesmd-cli 实现自动化。</p>
</div>
<a rel="nofollow" href="/xiazai/skill8828" title="Obsidian (Linux)" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>赋予执行权限:<code>chmod +x nginx_health_check.sh</code>,运行即可看到带颜色的状态汇总。</p>
<h3>集成到定时任务与简单告警</h3>
<p>将结果定期执行并检测异常,例如每 2 分钟检查一次,发现 DOWN 节点时发邮件或写日志:</p>
<ul><li>添加 cron 任务(<code>crontab -e</code>):</li></ul>
<font size="2"><pre class="brush:php;toolbar:false;">*/2 * * * * /path/to/nginx_health_check.sh >> /var/log/nginx_health.log 2>&1- 增强脚本加入告警逻辑(片段):
if grep -q "DOWN" "$TMP_FILE"; then
echo "$(date): CRITICAL - Some upstream servers are DOWN!" | mail -s "Nginx Health Alert" admin@example.com
logger "Nginx upstream health check detected DOWN node"
fi注意:邮件需提前配置本地 sendmail 或 msmtp;生产环境建议对接 Prometheus + Alertmanager 或企业微信/钉钉机器人。
替代方案:无扩展模块时的简易探测
若无法安装第三方模块,可用 HTTP 探测模拟健康检查:
- 对每个 upstream server 发起 HEAD 请求,判断响应码是否为 2xx/3xx:
for server in 192.168.1.10:8080 192.168.1.11:8080; do
code=$(curl -s -o /dev/null -w "%{http_code}" --head "http://$server/health" --connect-timeout 2)
status=$( [[ $code =~ ^2|^3 ]] && echo "UP" || echo "DOWN" )
echo "$server: $status (HTTP $code)"
done此方式需后端服务提供 /health 接口,不依赖 Nginx 模块,但无法反映 Nginx 内置探活的真实状态(如连接池、超时重试等)。










