> 有时候我们会有一类需求,有一个模板页面,模板的内容需要根据域名的不同,去文本里面或者Excel读取对应域名的信息,填充HTML模板文件,从而使访问不同的域名,得到的信息都不尽相同 #### 分析 - 一台服务器上nginx配置了多个server_name, 访问不同的域名时, 我们将域名传递给lua, lua再去操作系统命令或者python脚本,提取对应的信息 - lua使用set_by_lua去设置一个nginx变量,用来接收lua段脚本提取命令的输出信息 - 本次我们需要替换html页面的备案号等信息,我们使用nginx的subsub_filter模块去替换html中的字符串 - 通过此lua操作脚本的方法我们也可以做ip访问控制,将要屏蔽的ip写入文本, 每次请求进入nginx时,将改ip和文本中的ip做比对,由此可以做控制 #### 下载安装openresty 因为要使用到lua功能,简便一点就是直接安装openresty,会自带nginx和lua功能,也可以自己在nginx上编译lua模块, [中文站点](http://openresty.org/cn/linux-packages.html) 添加仓库 ``` sudo yum install yum-utils sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo ``` 安装 ``` sudo yum install openresty ``` 安装默认openresty路径在/usr/local/openresty #### nginx的server配置 ``` server { listen 80; server_name _ default_server; #charset koi8-r; access_log logs/access.log main; location / { root /nginx/copyright; index index.html index.htm; } location /index.html { root /nginx/copyright; index index.html index.htm; set_by_lua $info ' local opt = ngx.var.host local file = io.popen("python /nginx/shell/getSiteInfo.py " ..opt) content1 = file:read("*l") return content1 '; if ($info ~ (.*)\s(.*)\s(.*)\s(.*)) { set $registering $1; set $site $2; set $copyright $3; set $police $4; } sub_filter '沪ICP备1xxxx号-4' $registering; sub_filter 'http://idinfo.zjaic.gov.cn/bscx.do?spm=5176.8142029.631162.107.a7236d3erXRDEL&method=hddoc&id=33010800001867' $site; sub_filter '上海xxx科技有限公司' $copyright; sub_filter '沪公网安备 31011502005049号' $police; sub_filter_once on; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } ``` #### 配置文件中有三点需要注意 1. server_name为default_server,接收全部域名 2. 使用set_by_lua $info设置一个nginx变量info, 而这个变量的值就是lua块return的结果, io.popen()方法是使用系统命令去运行命令或脚本 3. 使用sub_filter去替换响应的html页面中对应的字符串 #### 读取Excel的脚本 ``` #/usr/bin/python #coding=utf8 import xlrd import sys import os import re reload(sys) sys.setdefaultencoding('utf8') host = sys.argv[1] domain_temp = host.split(".")[-2:] domain = "{}.{}".format(domain_temp[0], domain_temp[1]) path = "/nginx/execl/" files_old = os.listdir(path.decode('utf-8')) files = [] for i in files_old: if re.findall(".xlsx$",i): files.append(path+i) for k in files: xlsfile = k #xlsfile = "/root/域名.xlsx" book = xlrd.open_workbook(xlsfile) sheet0 = book.sheet_by_index(0) sheet_name = book.sheet_names()[0]# 获得指定索引的sheet表名字 sheet1 = book.sheet_by_name(sheet_name)# 通过sheet名字来获取,当然如果知道sheet名字就可以直接指定 nrows = sheet0.nrows for i in range(nrows): if i == 0: continue if domain == sheet1.row_values(i)[4]: print sheet1.row_values(i)[10].strip(),str(sheet1.row_values(i)[11]).strip(),sheet1.row_values(i)[12].strip(),sheet1.row_values(i)[13].strip().replace(' ', ' ') ``` ## lua扩展 在nginx中使用lua会很方便我们做一些自定义的需求,比如我们希望被拒绝某些ip地址访问nginx,ip地址会动态增改,希望写在文本中. #### 拒绝某些IP访问本站,将其重定向至百度 ``` location / { root html; index index.html index.htm; } set_by_lua $info ' local opt = ngx.var.remote_addr # 检查ip, 若ip在/tmp/ip中则返回该ip local file = io.popen("cat /tmp/ip | grep -o " ..opt) content1 = file:read("*l") return content1 '; if ( $info = $remote_addr) { return 301 https://www.baidu.com; } ``` **/tmp/ip** ``` ~]# cat /tmp/ip 101.26.35.26 102.43.89.65 ``` Last modification:July 1st, 2019 at 10:36 am © 允许规范转载 Support 如果觉得我的文章对你有用 ×Close Appreciate the author Sweeping payments