Python 生成nginx 配置

来自linux中国网wiki
Evan讨论 | 贡献2024年10月21日 (一) 15:47的版本 →‎生成配置的py脚本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

直接对nignx.conf操作

生成配置的py脚本

#!/usr/bin/env python
#data/oldcompany/dovo/dovo_shell_study/FRG/data/data/dv_mon/scripts
#[root@vm mon]# cat nginx_conf.py 
import sys
import ConfigParser
import traceback
import shlex
import subprocess
import re
output = open('/apps/nginx/conf/nginx.conf','w')
cf = ConfigParser.ConfigParser()
try:
	try:
		cf.read('/conf/web.ini')
		conffile = cf.get('nginx','conffile')
		user = cf.get('nginx','nginxuser')
		hosts = re.split('\ *',cf.get('nginx','nginx_host').strip())
		common = """#================== Don't edit this file by human , pls use /data/script/nginx_conf ==================#
user $USER;
worker_processes 8;
error_log /dev/null crit;
pid /apps/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events {
	use epoll;
	worker_connections 51200;
}
http {
	include mime.types;
	gzip on;
	gzip_min_length  1k;
	gzip_buffers     4 16k;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_types       text/plain application/x-javascript text/css application/xml;
	gzip_vary on;
	default_type application/octet-stream;
	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 off;
	charset utf-8;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	server_names_hash_bucket_size 512;
	sendfile on;
	tcp_nopush on;
	keepalive_timeout 60;
	tcp_nodelay on;
	client_max_body_size 30m; 
	client_body_buffer_size 128k;
	proxy_connect_timeout 800;
	proxy_read_timeout 800;
	proxy_send_timeout 800;
	proxy_buffer_size 8k;
	proxy_buffers 4 32k;
	proxy_busy_buffers_size 64k;
	proxy_temp_file_write_size 64k;
	proxy_temp_path /dev/shm/proxy_temp;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header REMOTE_ADDR $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	fastcgi_connect_timeout 800;
	fastcgi_send_timeout 800;
	fastcgi_read_timeout 800;
	fastcgi_buffer_size 256k;
	fastcgi_buffers 8 512k;
	fastcgi_busy_buffers_size 512k;
	fastcgi_temp_file_write_size 512k;
	fastcgi_temp_path /dev/shm/tmp;
	"""
		common = common.replace('$USER',user)
		for host in hosts:
			_common = """
	#==================  $DOMAIN ==================#
	server {
		listen $PORT;
		server_name $DOMAIN;
		charset utf-8;
		access_log off;
		root $DIR;
		index index.php index.htm index.html;
		location ~ .*\.(gif|jpg|jpeg|png|bmp|ico|swf|html|htm|mp3|wma|js|css)$ {
			expires      7d;
		}
		location ~ .*\.php?$ {
			include /data/apps/nginx/conf/fastcgi.conf;
			fastcgi_pass 127.0.0.1:10081;
			fastcgi_index index.php;
		}
		location /NginxStatus {
			stub_status on;
			access_log off;
			auth_basic "NginxStatus";
			auth_basic_user_file /usr/local/nginx/conf/htpasswd;
		}
		error_page 404 /error/404.php; 
	}
	"""
			_domain = cf.get(host,'domain')
			_port = cf.get(host,'port')
			_path = cf.get(host,'path')
			common = common + _common.replace('$PORT',_port).replace('$DOMAIN',_domain).replace('$DIR',_path)
		common = common + """
	include /data/apps/nginx/conf/hosts/*.conf;
}
"""
		output.write(common)
		output.close()
		retcode = subprocess.call(shlex.split("/etc/init.d/nginx check"))
		if 0 == retcode:
			retcode = subprocess.call(shlex.split("/etc/init.d/nginx reload"))
			print('SUCCESS!!')
		else:
			print('FAIL!!')
	except:
		print traceback.print_exc()
		print('FAIL!!')
finally:
	sys.exit()

配置文件

[nginx]
conffile=/apps/nginx/conf/nginx.conf
nginxuser=www
nginx_host=s1.com  s2.com

[s1.com]
domain=s1.com 
port=80
path=/www/html_s1

[s2.com]
domain=s2.com 
port=80
path=/www/html_s2

执行py 脚本生成的配置

#================== Don't edit this file by human , pls use /data/script/nginx_conf ==================#
user www;
worker_processes 8;
error_log /dev/null crit;
pid /apps/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events {
	use epoll;
	worker_connections 51200;
}
http {
	include mime.types;
	gzip on;
	gzip_min_length  1k;
	gzip_buffers     4 16k;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_types       text/plain application/x-javascript text/css application/xml;
	gzip_vary on;
	default_type application/octet-stream;
	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 off;
	charset utf-8;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	server_names_hash_bucket_size 512;
	sendfile on;
	tcp_nopush on;
	keepalive_timeout 60;
	tcp_nodelay on;
	client_max_body_size 30m; 
	client_body_buffer_size 128k;
	proxy_connect_timeout 800;
	proxy_read_timeout 800;
	proxy_send_timeout 800;
	proxy_buffer_size 8k;
	proxy_buffers 4 32k;
	proxy_busy_buffers_size 64k;
	proxy_temp_file_write_size 64k;
	proxy_temp_path /dev/shm/proxy_temp;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header REMOTE_ADDR $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	fastcgi_connect_timeout 800;
	fastcgi_send_timeout 800;
	fastcgi_read_timeout 800;
	fastcgi_buffer_size 256k;
	fastcgi_buffers 8 512k;
	fastcgi_busy_buffers_size 512k;
	fastcgi_temp_file_write_size 512k;
	fastcgi_temp_path /dev/shm/tmp;
	
	#==================  s1.com ==================#
	server {
		listen 80;
		server_name s1.com;
		charset utf-8;
		access_log off;
		root /www/html_s1;
		index index.php index.htm index.html;
		location ~ .*\.(gif|jpg|jpeg|png|bmp|ico|swf|html|htm|mp3|wma|js|css)$ {
			expires      7d;
		}
		location ~ .*\.php?$ {
			include /data/apps/nginx/conf/fastcgi.conf;
			fastcgi_pass 127.0.0.1:10081;
			fastcgi_index index.php;
		}
		location /NginxStatus {
			stub_status on;
			access_log off;
			auth_basic "NginxStatus";
			auth_basic_user_file /usr/local/nginx/conf/htpasswd;
		}
		error_page 404 /error/404.php; 
	}
	
	#==================  s2.com ==================#
	server {
		listen 80;
		server_name s2.com;
		charset utf-8;
		access_log off;
		root /www/html_s2;
		index index.php index.htm index.html;
		location ~ .*\.(gif|jpg|jpeg|png|bmp|ico|swf|html|htm|mp3|wma|js|css)$ {
			expires      7d;
		}
		location ~ .*\.php?$ {
			include /data/apps/nginx/conf/fastcgi.conf;
			fastcgi_pass 127.0.0.1:10081;
			fastcgi_index index.php;
		}
		location /NginxStatus {
			stub_status on;
			access_log off;
			auth_basic "NginxStatus";
			auth_basic_user_file /usr/local/nginx/conf/htpasswd;
		}
		error_page 404 /error/404.php; 
	}
	
	include /apps/nginx/conf/hosts/*.conf;
}

独立生成各域名配置

参考

Python ConfigParser模块