nginx高可用方案

3.3.6 Nginx 高可用

3.3.6.1 安装Keepalived

yum install -y keepalived

3.3.6.2 编辑keepalived配置文件

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL					#主备一样
}
vrrp_script chk_nginx {					#检测nginx脚本
    script "/home/eqics/script/chk_nginx.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP						#主MASTER  备BACKUP
    interface eth0						#网卡
    nopreempt							#备机BACKUP才用,不抢占的意思
    unicast_src_ip 192.168.0.96			#本机内网ip
    unicast_peer {
		192.168.0.102					#对机内网ip
	}
    virtual_router_id 51				#主备一样
    priority 101						#主比备高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111					#密码可以修改主备相同
    }
    track_script {						#脚本调用
	chk_nginx
    }

    virtual_ipaddress {
        192.168.0.100					#虚拟ip,自己设置,要跟内网一个网段
    }
}

3.3.6.3 keepalived命令

systemctl start keepalived.service

云服务器上操作:

虚拟ip绑定两台机器,再用公网ip绑定虚拟ip。

可以实现一台机器挂了,ip会跳到另外的机器上继续运行。

3.3.6.4 chk_nginx 脚本

在/home/eqics/script/下创建chk_nginx.sh

#!/bin/bash

n=`netstat -tlnp | grep 0.0.0.0:80`

if [ $? -eq "1" ]
then
        /home/eqics/nginx-https/sbin/nginx
	sleep 2 
        n2=`netstat -tlnp | grep 0.0.0.0:80`
        if [ $? -eq "1" ]
        then
                killall keepalived
        fi
fi

3.3.6.5 脚本赋权

chmod 755 chk_nginx

3.3.6.6 重启Keepalived

systemctl restart keepalived.service

nginx高可用方案

上一篇:cocos2dx windows phone平台下CCLabelTTF自动换行的实现


下一篇:HashMap中存储原理简单介绍