Shell script: Nginx Reverse proxy node status check

Source: Internet
Author: User
Tags nginx reverse proxy egrep

LVS can use IPVSAM-LN to view the situation of the RS node, when the RS after the shutdown, when the RS automatically added after the restoration, Nginx can not view, need to install plug-ins or write their own script implementation;


The reverse proxy is configured as follows: (the server removes the preceding space, in order to facilitate later scripts to use SED to do text replacement;)

[[email protected] vhosts]# Cat Upstream01.confupstream Backend {server 192.168.20.10:80 weight=5;server 192.168.20.11:80 weight=5;}    server {Listen 80;    server_name blog.yong.com;        Location/{Proxy_pass http://backend;        Proxy_set_header Host $host;    Proxy_set_header x-forwarded-for $remote _addr; }}


Check the Nginx backend real server script, realize the detection of downtime, restore the service auto-join function;

Script Explanation:

Using the daemon, every 5 seconds to execute a script to determine whether the back-end RS service is normal, found that there is an outage of RS, the SED modified configuration file # comment out the corresponding RS,IP address, and restart the Nginx service, when the Discovery service recovery, remove the # number, and restart the Nginx service, Added to Rs;

[[Email protected] vhosts]# cat check_nginx_rs.sh  #!/bin/bash#written by  [email protected]ip_array= ($ (grep  "192.168.20"  upstream01.conf |awk  ' {print  $2} ' |awk -f ': '   ' {print $1} ') While truedo    for ((i=0;i<${#ip _array[*]};i++))     do         egrep  "^# . *${ip_array[$i]}.* " upstream01.conf &>/dev/null          [ $? -eq 0 ] && continue          status= ' curl -s -w  "%{http_code}"  -o /dev/null ${ip_array[$i]} '          if [ ${status} -ne 200 ]              then               sed -i  "s/server ${ip_array[$i]}/#server  ${ip_array[$i]}/g"  upstream01.conf              /etc/ init.d/nginx reload         fi     Donesleep 5for ((i=0;i<${#ip_array [*]};i++)) do a= ' curl -s -w  "%{http_code}"  -o  /dev/null ${ip_array[$i]} '      if [ ${a} -eq 200  ];then      egrep  "^#.*${ip_array[$i]}.*"  upstream01.conf & >/dev/null      if [ $? -eq 0 ];then            sed -i -r  "s/# (. *${ip_array[$i]}.*)/\1/g"  upstream01.conf           /etc/init.d/nginx  Reload  &nbsP;   fi     fidonedone 


Script execution results such as:

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/76/A0/wKiom1ZX9kezEslWAAAsQwGWDkA131.jpg "title=" Check_ Nginx_rs.jpg "alt=" Wkiom1zx9kezeslwaaasqwgwdka131.jpg "/>

This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1717464

Shell script: Nginx Reverse proxy node status check

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.