nginx / python / 未分类 · 2012年3月9日

python 生成nginx 配置-tag

[root@evan_vm tmp]# cat nginx_conf
#!/usr/bin/env python
import sys
import ConfigParser
import traceback
import shlex
import subprocess
import re
output = open('/usr/local/nginx/conf/nginx.conf','w')
cf = ConfigParser.ConfigParser()
try:
try:
cf.read('/root/tmp/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/dv_mon/script/nginx_conf ==================#
user $USER;
worker_processes 8;
error_log /dev/null crit;
pid logs/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 fcgi.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 nginx_ext*.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()

[root@evan_vm tmp]# cat web.ini
[nginx]
conffile=/usr/local/nginx/conf/nginx.conf
nginxuser=www
nginx_host=s2.evan

[s2.evan]
domain=s2.xx.com
port=80
path=/data/www/html_s2
填写好 web.ini 然后执行 nginx_conf
## 放到 /et/init.d/
#!/bin/bash
# chkconfig: 2345 93 11
# description:Nginx Server

NGINX_HOME=/usr/local/nginx
NGINX_SBIN=$NGINX_HOME/sbin/nginx
NGINX_CONF=$NGINX_HOME/conf/nginx.conf
NGINX_PID=$NGINX_HOME/logs/nginx.pid
NGINX_MAXFD=65535
NGINX_NAME=”Nginx”
. /etc/rc.d/init.d/functions
if [ ! -f $NGINX_SBIN ]
then
echo “$NGINX_NAME startup: $NGINX_SBIN not exists! ”
exit
fi
start() {
ulimit -HSn $NGINX_MAXFD
$NGINX_SBIN -c $NGINX_CONF
ret=$?
if [ $ret -eq 0 ]; then
action $”Starting $NGINX_NAME: ” /bin/true
else
action $”Starting $NGINX_NAME: ” /bin/false
fi
}
stop() {
kill $(cat $NGINX_PID)
ret=$?
if [ $ret -eq 0 ]; then
action $”Stopping $NGINX_NAME: ” /bin/true
else
action $”Stopping $NGINX_NAME: ” /bin/false
fi
}
restart() {
stop
sleep 2
start
}
check() {
$NGINX_SBIN -c $NGINX_CONF -t
}
reload() {
kill -HUP $(cat $NGINX_PID)
}
relog() {
kill -USR1 $(cat $NGINX_PID)
}
case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
check)
check
;;
reload)
reload
;;
relog)
relog
;;
*)
echo $”Usage: $0 {start|stop|restart|reload|check|relog}”
exit 1
esac