使用python脚本批量设置nginx站点的rewrite规则
2024-10-19 06:16:53
一般情况下,配置rewrite重写规则使用shell脚本即可:
把url拼凑成1,2文件中中的格式,运行 chongxie.sh 即可生成我们需要的rewrite规则
[root@web01:/opt/rewrite]# more
^/facebook/recover-facebook-messages-on-android.html [root@web01:/opt/rewrite]# more
https://www.chinasoft.com/facebook/recover-facebook-messages-on-android.html [root@web01:/opt/rewrite]# more chongxie.sh
#!/bin/bash
> ./temp;
cat -n >;cat -n >;join -o 1.2,2.2 >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;
[root@web01:/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 ;
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 /.html; location ~ \.php$ {
proxy_pass http://php_pool;
include proxy_params;
} location / {
include proxy_params;
if (!-d $request_filename){
set $flag $flag;
}
if (!-f $request_filename){
set $flag $flag;
}
if ($flag = ""){
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://
最新文章
- node.js基础 1之 HTTP流程实例
- Java 位运算(移位、位与、或、异或、非)
- MySQL批量SQL插入性能优化
- 【WEB】原理 之 线程池
- Linux系统编程(22)——响应信号
- Web 开发中 9 个有用的提示和技巧
- AlertDialog具体解释
- CodeM资格赛 Round A 最长树链
- python_12 模块
- GitHub下载提速
- JAVA中循环删除list中元素的方法总结(同上篇)
- 如何解决Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a. 问题
- GTP+SDI工程播出部分思路整理
- angularjs中阻止事件冒泡,以及指令的注意点
- 19.纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
- 更好使用jQuery的8个小技巧
- 解题:洛谷4721 [模板]分治FFT
- .net Basic
- hdu1848Fibonacci again and again(sg函数)
- Office文件的实质是什么