linux编写脚本实现Nginx集群管理代码
时间: 2024-10-23 17:09:50 浏览: 23
Linux shell实现HTTP服务示例代码
在Linux环境下,使用脚本管理和配置Nginx集群通常涉及到监控、负载均衡以及健康检查等功能。以下是一个简单的shell脚本示例,用于基本的Nginx集群状态监控和启动停止操作:
```bash
#!/bin/bash
# 定义Nginx的安装路径和配置文件路径
NGINX_DIR="/usr/local/nginx"
NGINX_CONF="$NGINX_DIR/conf"
# 集群节点列表,这里假设是两个节点
NODES=("node1" "node2")
# 启动Nginx
start_nginx() {
for node in "${NODES[@]}"; do
echo "Starting Nginx on $node..."
systemctl start nginx@$node || { echo "Failed to start on $node"; exit 1; }
done
}
# 停止Nginx
stop_nginx() {
for node in "${NODES[@]}"; do
echo "Stopping Nginx on $node..."
systemctl stop nginx@$node || { echo "Failed to stop on $node"; exit 1; }
done
}
# 检查Nginx状态
check_nginx_status() {
for node in "${NODES[@]}"; do
if systemctl is-active nginx@$node | grep -q "active"; then
echo "$node is running."
else
echo "$node is not running."
fi
done
}
case $1 in
start)
start_nginx
;;
stop)
stop_nginx
;;
status)
check_nginx_status
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
#
阅读全文