使用python脚本批量设置nginx站点的rewrite规则
一般情况下,配置rewrite重写规则使用shell脚本即可:
把url拼凑成1,2文件中中的格式,运行 chongxie.sh 即可生成我们需要的rewrite规则
[[email protected]:/opt/rewrite]# more 1 ^/facebook/recover-facebook-messages-on-android.html [[email protected]:/opt/rewrite]# more 2 https://www.chinasoft.com/facebook/recover-facebook-messages-on-android.html [[email protected]:/opt/rewrite]# more chongxie.sh #!/bin/bash > ./temp; cat -n 1 >3;cat -n 2 >4;join -o 1.2,2.2 3 4 >301temp; # 按行读取拼凑的内容,并且拼凑成我们需要的规则写入到temp中 while read line do a=`echo $line|awk '{print "if (\$request_uri ~ " $1 ") { rewrite ^ " $2 " permanent; }"}'` echo $a >> temp; done < 301temp sed -i 's/.html\$/.html/' temp;sed -i 's/.php\$/.php/' temp; [[email protected]:/opt/rewrite]# more temp if ($request_uri ~ ^/facebook/recover-facebook-messages-on-android.html) { rewrite ^ https://www.chinasoft.com/facebook/recover-facebook-messages-on-android.html permanent; }
如下配置:
rewrite规则写在 rewrite_web.d 目录中
# cat /usr/local/nginx/conf/web.d/no.chinasoft.com.conf server { listen 80; server_name no.chinasoft.com ; access_log /data/www/logs/nginx_log/access/no.chinasoft.com_access.log main ; error_log /data/www/logs/nginx_log/error/no.chinasoft.com_error.log ; root /data/www/web/no.chinasoft.com/httpdocs ; index index.html index.shtml index.php ; include rewrite_web.d/no.chinasoft.com.conf ; error_page 404 /404.html; location ~ \.php$ { proxy_pass http://php_pool; include proxy_params; } location / { include proxy_params; if (!-d $request_filename){ set $flag 1$flag; } if (!-f $request_filename){ set $flag 2$flag; } if ($flag = "21"){ proxy_pass http://php_pool; } } }
python脚本:
excel的插件下载地址:
https://files.pythonhosted.org/packages/b0/16/63576a1a001752e34bf8ea62e367997530dc553b689356b9879339cf45a4/xlrd-1.2.0-py2.py3-none-any.whl
pip install xlrd-1.2.0-py2.py3-none-any.whl 安装
# coding:utf-8 # 读取excel的每一行,获取host信息列表 import xlrd import os import sys import datetime workbook = xlrd.open_workbook('web01.xlsx') for row in xrange(workbook.sheets()[0].nrows): src_url=workbook.sheets()[0].cell(row,0).value dst_url=workbook.sheets()[0].cell(row,1).value # 获取原始域名和path from urlparse import urlparse parsed_uri = urlparse(src_url) domain = '{src_url.netloc}'.format(src_url=parsed_uri) src_path = parsed_uri.path rewrite_url = "if ($request_uri ~ ^" + src_path +") { rewrite ^ " + dst_url + " permanent; }" time = datetime.datetime.now().strftime('%Y%m%d') today = "# "+time # 判断rewrite.d文件是否存在,存在就写入 dst_file = "/usr/local/nginx/conf/rewrite_web.d/"+domain+".conf" if os.path.isfile(dst_file): # cmd_addtime = 'echo ' + today + '>>' + dst_file # print cmd_addtime # res1 = os.system(cmd_addtime) # if res1 != 0: # print 'write today fail' # cmd_addurl = 'echo ' + rewrite_url + '>>' + dst_file # print cmd_addurl # res2 = os.system(cmd_addurl) # if res2 != 0: # print 'write url %s failed' % rewrite_url with open(dst_file, mode='a+') as f: f.write('\n') f.write(today) f.write('\n') f.write(rewrite_url) f.write('\n') else: print "file %s is not exists" % dst_file
excel的规则,要编辑太多文件内容了,如果手动会浪费大量时间,于是写成了python脚本
注意,源url需要带 http://