openresty结合lua对同一条域名判断参数做负载 1,配置一条域名 ,当https://hudiecaifus.net/?channelCode=gg_34
这条域名后缀参数等于gg_34的时候转发http://meigui_34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 server { listen 80; server_name hudiecaifus.net www.hudiecaifus.net; root /software/mangguo站点/gg代理/hudiecaifus.net-no-cnzz; #默认不统计 location / { set_by_lua_block $gg_34 { local channel = { "gg_34" }; for k, v in pairs( channel ) do if ngx.var.arg_channelCode == v then return 1; end end return 0; } if ($gg_34) { proxy_pass http://meigui-gg_34; } } }
2, 再新建一个文件touliang_upstream.conf
每个落地页分别建2个server段。一个统计。一个不统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 upstream meigui-gg_34 { # 不扣 server 127.0.0.1:8804 weight=7; # 扣量 server 127.0.0.1:8805 weight=3; } server { listen 8804; server_name 127.0.0.1; root /software/mangguo站点/gg代理/hudiecaifus.net; location = /index.html { set_by_lua_block $date { return ngx.today(); } set_by_lua_block $c { if ngx.var.arg_channelCode ~= nil then return ngx.var.arg_channelCode; end return "default"; } set_by_lua_block $log_name { local name = ngx.var.date .. "-" .. ngx.var.c .. "-cnzz"; #访问生成对应的日志 return name; } access_log logs/$log_name.log; } } server { listen 8805; server_name 127.0.0.1; root /software/mangguo站点/gg代理/hudiecaifus.net-no-cnzz; #不统计落地页 location = /index.html { set_by_lua_block $date { return ngx.today(); } set_by_lua_block $c { if ngx.var.arg_channelCode ~= nil then return ngx.var.arg_channelCode; end return "default"; } set_by_lua_block $log_name { local name = ngx.var.date .. "-" .. ngx.var.c .. "-no-cnzz"; return name; } access_log logs/$log_name.log; } }
2.1, 优化后的配置文件,放在同一个目录下面,方便工作维护 新建touliang_xin.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 erver { listen 80; server_name aiktv.com www.aiktv.com ; root /software/nsvwang站点/nsvwang落地页1; location ~* (\.html|/)$ { if ($arg_channelCode = 'nsw_1') { proxy_pass http://nsvwang-touliang-nsw_1; } if ($arg_channelCode = 'nsw_c1') { proxy_pass http://nsvwang-touliang-nsw_c1; } } }
再建一个touliang_upstram.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 upstream nsvwang-touliang-nsw_1 { # 不扣 server 127.0.0.1:12001 weight=5; # 扣量 server 127.0.0.1:12002 weight=5; } server { listen 12001; server_name 127.0.0.1; root /software/nsvwang站点/nsvwang落地页1; location ~* (\.html|/)$ { set_by_lua_block $date { return ngx.today(); } set_by_lua_block $c { if ngx.var.arg_channelCode ~= nil then return ngx.var.arg_channelCode; end return "default"; } set_by_lua_block $log_name { local name = ngx.var.date .. "-" .. ngx.var.c .. "-op"; return name; } access_log logs/$log_name.log; } } server { listen 12002; server_name 127.0.0.1; root /software/nsvwang站点/nsvwang落地页1; index no-op.html; location ~* (\.html|/)$ { set_by_lua_block $date { return ngx.today(); } set_by_lua_block $c { if ngx.var.arg_channelCode ~= nil then return ngx.var.arg_channelCode; end return "default"; } set_by_lua_block $log_name { local name = ngx.var.date .. "-" .. ngx.var.c .. "-touliang"; return name; } access_log logs/$log_name.log; } }
3, 查看当天代理线的访问次数
1 2 3 4 5 6 7 8 9 10 11 12 进入源服务器 cd /etc/nginx/logs/ cat 2022-03-12-gg_34-cnzz.log |wc -l 83746 #表示gg_34代理线当天总访问83736次,走统计页面 cat 2022-03-12-gg_34-no-cnzz.log |wc -l 36845 #表示gg_34代理线当天总访问36845次,不走统计页面
4, 查看对应域名有多少代理线,或者查看代理线有哪些域名访问
1 2 3 4 5 6 7 8 9 cat check.sh #!/bin/sh if [ ! -z $1 ]; then grep $1 /etc/nginx/logs/access.log | awk '{ printf ("%s --- %s\n",$3,$5) }' | grep $1 | grep channelCode | sort | uniq -c | sort -n #grep $1 access.log-20220413 | awk '{ printf ("%s --- %s\n",$3,$5) }' | grep $1 | grep channelCode | sort | uniq -c | sort -n else echo 参数为空 fi
使用脚本
1 2 3 4 5 ./check.sh woaktv.cc #参数1输入域名,就会统计出对应的代理线以及访问量 ./check.sh gg_34 #参数1输入代理线参数,就会统计出对应的域名以及访问量