python修改nginx配置文件_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(3)
测试:修改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'))
成功!!!
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
python修改nginx配置文件_zookeeper 动态管理nginx配置相关推荐
- SpringCloud的Archaius - 动态管理属性配置
参考链接:http://www.th7.cn/Program/java/201608/919853.shtml 一.Archaius是什么? Archaius用于动态管理属性配置文件. 参考自Gett ...
- Linux网络系列--网络设置(查看及测试网络,设置网络连接、修改网络配置文件、虚拟机的域名解析配置)
文章目录 一. 查看及测试网络 1.1 查看网络配置 1.1.1 ifconfig命令--查看网络接口地址 1.1.2 hostname命令--查看.修改主机名 1.1.3 netstat与ss命令- ...
- linux 修改docker配置文件,dockerfile动态修改服务配置文件(示例代码)
主要是利用命令envsubst能实现变量的替换并生成新得配置文件以及docker命令行的变量输入等. 本次实验主要是编写flume镜像,并在容器启动(docker run)时动态修改配置文件并启动,并 ...
- 重识Nginx - 15 使用信号管理Nginx的父子进程
文章目录 Nginx 进程管理 :信号 常用信号说明 Nginx 进程管理 :信号 标红的 USR2 .WINCH ,只能通过 kill 的方式 来发送,主要是nginx 热部署的时候使用. 常用信号 ...
- 从零学习Nginx配置文件,呕心沥血w字长文
1 Nginx介绍 1 Nginx基本概念 可以理解为 nginx 是服务器的软件 1.1 反向代理 **正向代理:**需要在客户端(浏览器)配置代理服务器,通过代理服务器进行互联网访问. 反向代理: ...
- nginx配置文件及工作原理详解
nginx配置文件及工作原理详解 1 nginx配置文件的结构 2 nginx工作原理 1 nginx配置文件的结构 1)以下是nginx配置文件默认的主要内容: #user nobody; #配置用 ...
- Nginx 配置文件详解(一)
文章目录 Nginx 配置文件内容 Nginx 配置文件基本结构 Nginx 配置文件详细信息 全局块配置 配置运行 Nginx 服务器用户(组) 配置 worker processes 相关 配置 ...
- nginx配置文件详解(三)
nginx配置文件详细解析 nginx安装目录: /usr/local/nginx 配置文件: /usr/local/nginx/conf 目录下的 nginx.conf文件 nginx优化方法 ...
- Nginx配置文件中location配置
[标题]:Nginx location "/" 配置 服务访问 服务请求如下(示例): nginx服务: http://127.0.0.1:80 后台服务:http://127.0 ...
最新文章
- SAP中供应商确认功能的应用分析
- Python脑电数据的Epoching处理
- fast_recovery_area无剩余空间(ORA-19815)
- 搞定系统设计 02:估算的一些方法
- java如何分页_java中分页的实现
- oschina下载工具
- HashMap和ConcurrentHashMap
- 【LeetCode笔记】148. 排序链表(Java、归并排序、快慢指针、双重递归)
- QT制作自定义进度条(圆环状)
- matlab gui设置参数,基于Matlab/GUI界面设计的参数在线整定与优化(三)
- Microsoft Visio 2010 - 直线连接线
- 某城郊110kV降压变电站监控系统设计
- 电压电流测量模块在matlab,MATLAB仿真时用simulink的RMS模块测量电压有效值,总提示如下警告,怎么破,跪求?...
- 如何dismiss多个viewController
- 敏捷实践 | 分不清Kanban和看板的人只剩你了
- sdutacm-小雷的冰茶几
- JSF标签详解(全)
- 【Android】多渠道打包与签名机制
- 宝塔面板隐藏网站服务器真实IP
- Vue单文件中引入背景图片时,四周有空白的问题