zookeeper 动态管理nginx配置
假设我们有一个场景,所有服务器共享同一份配置文件,我们肯定不可能单独手动维护每台服务器,这时可以利用zookeeper的配置管理功能。
环境:python + nginx + zookeeper
目的:当zookeeper中的配置文件发生变化时nginx自动拉取最新配置文件并应用到本地,最终重启服务器:
一。搭建zookeeper集群:
步骤略,请参考:http://zookeeper.apache.org/doc/r3.4.13/zookeeperStarted.html
请确保zookeeper集群为健康状态:

二。安装nginx
yum install nginx -y
启动nginx: systemctl start nginx
三。编写python zookeeper 客户端:
目的:定期连接zookeeper集群,检测配置文件发生变化:
from kazoo.client import KazooClient
import time
zk=KazooClient(hosts='192.168.85.137:2181')
zk.start() Version=None
while True:
@zk.DataWatch("/nginx")
def watch_node(data, stat):
global Version
if Version == None:
Version=stat
if Version != stat:
Version = stat
nginx_file=str(data,encoding='utf-8')
print("配置已改变!!!!!")
f=open('nginx.conf','w',encoding='utf-8')
f.write(nginx_file)
f.flush()
f.close()
import os
Path=os.path.dirname(os.path.abspath(__file__))
os.system('cp -f %s/nginx.conf /etc/nginx/nginx.conf && systemctl restart nginx.service'%Path) #替换nginx配置文件,然后重启服务,注意,这这里只是写了一个大体框架,生产环境一定不要直接重启,可以写一些判断,至少应该先确保配置文件没有错误再重启nginx
time.sleep()
测试:修改zookeeper中的nginx配置,看客户端是否能拉去配置文件:
from kazoo.client import KazooClient
import time
zk=KazooClient(hosts='192.168.85.137:2181')
zk.start() nginx_config="""
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 5555 default_server;
index index.php index.html;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf; location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
} """
zk.set('/nginx',bytes(nginx_config,encoding='utf-8'))


成功!!!
zookeeper 动态管理nginx配置的更多相关文章
- 使用etcd+confd管理nginx配置
1.前言 最近在项目中用nginx做反向代理,需要动态生成nginx的配置.大概流程是用户在页面上新增域名.http或https协议以及端口信息,后台会根据域名自动生成一个nginx的server配置 ...
- SpringCloud的Archaius - 动态管理属性配置
参考链接:http://www.th7.cn/Program/java/201608/919853.shtml 一.Archaius是什么? Archaius用于动态管理属性配置文件. 参考自Gett ...
- 新手学分布式-动态修改Nginx配置的一些想法
本人是分布式的新手,在实际工作中遇到了需要动态修改nginx的需求,因此写下实现过程中的想法.Nginx功能强大且灵活,所以这些权当抛砖引玉,希望可以得到大家的讨论和指点.(具体代码在 https:/ ...
- 关于linux下部署JavaWeb项目,nginx负责静态资源访问,tomcat负责处理动态请求的nginx配置
1.项目的运行环境 linux版本 [root@localhost ~]# cat /proc/version Linux version -.el6.x86_64 (mockbuild@x86-.b ...
- nginx配置静态资源与动态访问分离【转】
在前面的博客中<说说 NGINX 的配置及优化>的 2.5 小节里面,提到 location 模块是 nginx 中用的最多的,也是最重要的模块,负载均衡.反向代理.虚拟域名等都与它相关. ...
- nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)
前提:安装好nginx,如果已经启动nginx,先停止,命令: ./usr/local/nginx/sbin/nginx -s stop 修改nginx配置 vi /usr/local/nginx/c ...
- Nginx配置详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...
- nginx配置入门
谢谢作者的分享精神,原文地址:http://www.nginx.cn/591.html nginx配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一 ...
- nginx配置学习文章
partOne 自我释义部分 我的是阿里云的ubuntu *******实际上感觉这里是基本配置,很用不到*********#定义其用户或用户组user www-data;#nginx的进程数,应当为 ...
随机推荐
- 言传菜单JSON数据
{ "button": [ { "name": "快速发布", " ...
- 前端使用 validate , 根据条件进行动态的验证添加
需求如下: 审核操作的时候,选择“通过” 就不需要验证审核意见,但是选择的是“不通过”,那么需要进行审核意见验证 <script> $(function () { InitValidate ...
- BOM 清除
[root@test3 11]# gcc 11.2.c11.2.c:1: 错误:程序中有游离的 ‘\357’ 8进制,相当于16进制 ef11.2.c:1: 错误:程序中有游离的 ‘\273’11.2 ...
- Paxos 算法 :消息传递一致性
网络上有很多关于优秀的关于Paxos 算法的文章,我下面进行整理搜集一下: 分布式理论之一:Paxos算法的通俗理解 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 L ...
- Linux (Fedora 28) 如何查看 CPU 温度
机器突然负载有点高,查看CPU 温度的方法: 1.yum install lm_sensors; 2.sensors-detect 3.sensors 这些需要机器具备相应的设备,如果在虚拟机运行 ...
- .NetCore Build Terminology
.NETCore Command: 1.dotnet build 2.dotnet run 3.dotnet new classlib 4.dotnet new xunit 5.dotne ...
- cesium相机绕点飞行
相机绕点飞行原理就是获取相机参数与点位置信息,添加时间监听,一直去改变相机的heading,关键代码如下: function rotateHeading() { // 相机的当前heading var ...
- 方程:方程(equation)是指含有未知数的等式
方程(equation)是指含有未知数的等式.是表示两个数学式(如两个数.函数.量.运算)之间相等关系的一种等式,使等式成立的未知数的值称为“解”或“根”.求方程的解的过程称为“解方程”. 方程中文一 ...
- 踏得网互联网新技术垂直搜索服务和分享 - HTML5动效/特效/动画搜索
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/iefreer/article/details/34917729 当前主流搜索引擎在解决互联网技术创意 ...
- 1346:【例4-7】亲戚(relation)
并查集的模板题: #include<iostream> #include<cstdio> using namespace std; ; int fa[maxn]; int fi ...