今天阅读老男孩教育博客http://oldboy.blog.51cto.com/ 中一篇关于shell实现nginx反向代理后端realserver健康检查的文章,根据其中一个学员朋友的思路自己写了一个脚本。
一、nginx.conf部分内容如下:
upstream rs_pools { server 10.0.0.8:80 weight=5; server 10.0.0.9:80 weight=5; server 10.0.0.10:80 weight=5; } server { location / { proxy_pass http://rs_pools; } }
二、脚本如下:
#!/bin/bash NginxConfigPath=/application/nginx/conf NginxConfigFile=nginx.conf NginxCheckPath=/application/nginx/html NginxCheckFile=nginx_check.html #通过sed和awk获取realserver的IP地址,以数组保存 rs_list=(`sed -n '/upstream/{:a;N;/}/!ba;p}' ${NginxConfigPath}/${NginxConfigFile} | awk -F "[ :]+" '/server/{print $2}'`) #生成html主体部分的上半部分,并生成表格的第一行 function html_static(){ cat >> ${NginxCheckPath}/${NginxCheckFile}<
Nginx Proxy Healthy Check
EOF } #生成html主体的后半部分 function html_tail(){ cat >> ${NginxCheckPath}/${NginxCheckFile}< Server_ID Server_Type Server_IP Server_status