插件窝 干货文章 nginx+rsync+inotify怎么配置实现负载均衡

nginx+rsync+inotify怎么配置实现负载均衡

Rsync Inotify 同步 服务器 322    来源:    2025-04-27

Nginx + Rsync + Inotify 实现负载均衡配置方案

这个方案通过 Nginx 作为负载均衡器,Rsync 结合 Inotify 实现多服务器间的文件同步,确保后端服务器的内容一致性。

架构概述

  1. Nginx:作为前端负载均衡器,分发请求到后端服务器
  2. Rsync:用于服务器间的文件同步
  3. Inotify:监控文件系统变化,触发同步操作

配置步骤

1. Nginx 负载均衡配置

在负载均衡服务器上配置 Nginx:

http {
    upstream backend {
        # 配置后端服务器
        server backend1.example.com weight=5;
        server backend2.example.com;
        server backend3.example.com;

        # 负载均衡策略
        # least_conn;  # 最少连接
        # ip_hash;    # IP哈希
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

2. Rsync + Inotify 配置(主服务器)

在主服务器上配置文件同步:

安装必要软件

# CentOS/RHEL
yum install rsync inotify-tools -y

# Ubuntu/Debian
apt-get install rsync inotify-tools -y

配置 Rsync 服务端

编辑 /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[webdata]
    path = /path/to/webroot
    comment = Web Content
    read only = no
    hosts allow = 192.168.1.0/24
    hosts deny = *
    list = yes
    auth users = rsyncuser
    secrets file = /etc/rsyncd.secrets

创建认证文件 /etc/rsyncd.secrets

rsyncuser:password123

设置权限:

chmod 600 /etc/rsyncd.secrets

启动 Rsync 服务:

rsync --daemon

配置 Inotify 监控脚本

创建同步脚本 /usr/local/bin/inotify_rsync.sh

#!/bin/bash

# 定义变量
SRC="/path/to/webroot/"
DEST="rsyncuser@backend1.example.com::webdata"
# 可以添加多个目标服务器
DEST2="rsyncuser@backend2.example.com::webdata"
DEST3="rsyncuser@backend3.example.com::webdata"

# 设置密码文件
PASSFILE="/etc/rsyncd.secrets"

# 确保inotifywait可用
if ! which inotifywait &>/dev/null; then
    echo "inotifywait command not found!"
    echo "Please install inotify-tools first."
    exit 1
fi

# 监控事件
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' \
    -e modify,delete,create,attrib,move ${SRC} | while read DATE TIME FILEPATH EVENT
do
    # 同步到所有目标服务器
    rsync -avz --delete --password-file=${PASSFILE} ${SRC} ${DEST}
    rsync -avz --delete --password-file=${PASSFILE} ${SRC} ${DEST2}
    rsync -avz --delete --password-file=${PASSFILE} ${SRC} ${DEST3}

    # 记录日志
    echo "At ${TIME} on ${DATE}, file ${FILEPATH} was ${EVENT}" >> /var/log/inotify_rsync.log
done

设置脚本可执行:

chmod +x /usr/local/bin/inotify_rsync.sh

3. 后端服务器配置

在每个后端服务器上配置 Rsync 客户端:

安装 Rsync

# CentOS/RHEL
yum install rsync -y

# Ubuntu/Debian
apt-get install rsync -y

创建密码文件

echo "password123" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

设置定时同步(可选)

crontab -e

添加以下内容(每小时同步一次):

0 * * * * rsync -avz --delete --password-file=/etc/rsyncd.secrets rsyncuser@master.example.com::webdata /path/to/webroot/

4. 启动服务

在主服务器上启动监控脚本:

nohup /usr/local/bin/inotify_rsync.sh > /dev/null 2>&1 &

或者创建 systemd 服务:

# /etc/systemd/system/inotify_rsync.service
[Unit]
Description=Inotify Rsync Service
After=network.target

[Service]
ExecStart=/usr/local/bin/inotify_rsync.sh
Restart=always
User=root

[Install]
WantedBy=multi-user.target

然后启用服务:

systemctl daemon-reload
systemctl enable inotify_rsync
systemctl start inotify_rsync

验证配置

  1. 在主服务器上修改/创建文件,检查是否自动同步到后端服务器
  2. 通过 Nginx 访问服务,检查负载均衡是否正常工作
  3. 查看日志文件 /var/log/inotify_rsync.log/var/log/rsyncd.log 确认同步状态

优化建议

  1. 带宽限制:在 rsync 命令中添加 --bwlimit 参数限制同步带宽
  2. 排除文件:使用 --exclude 参数排除不需要同步的文件
  3. 增量同步:合理设置 inotify 的监控间隔,避免频繁同步
  4. 错误处理:在脚本中添加错误处理逻辑,确保同步失败时有重试机制
  5. 监控告警:设置监控脚本,当同步失败时发送告警

这个方案实现了基于文件内容同步的负载均衡系统,适用于需要多台服务器内容一致的应用场景。