nginx集群同步方案

之前公司同事写过rsync加触发nginx reload脚本,适合nginx配置内容完全一致的情况。

今天写一个同步指定文件的脚本,修改完主服务器。使用scp传输到其他nginx服务器上重启NGINX的脚本。适用于nginx集群部分配置相同的情况下。

 

一、ssh免密步骤

ssh-kengen命令生成私钥,复制~/.ssh/id_rsa.pub添加到目标机器~/.ssh/authorized_keys文件内。注意~/.ssh/authorized_keys文件权限600,~/.ssh/文件权限700

二、脚本内容

#!/bin/bash
file=$1

if [ -z "$file" ];then
    echo "请使用$0 文件名方式,例如: $? el.conf"
    exit 1
fi


function trans(){
    server=("10.0.2.5" "10.0.2.6" "10.0.2.7")
    cd /etc/nginx/
    for s in ${server[@]}
    do
        if [ $file == "nginx.conf" ];then
            scp $file root@${s}:/etc/nginx/
        else
            scp conf.d/$file root@${s}:/etc/nginx/conf.d/
        fi
        ssh root@${s} "nginx -t && nginx -s reload"
    done
}

nginx -t && nginx -s reload && trans

 

posted @ 2022-03-01 10:28  一起走过的路  阅读(654)  评论(0编辑  收藏  举报