背景
服务是采用nginx+lua实现的,在nginx的配置中存在多处(共20多处)正则判断,期望进行优化
location ~* /test.gif {
rewrite_by_lua_file 'lua/edit.lua';
if ($query_string ~* "pd=8(&|$)") {
set $req_host inner.a.com;
proxy_pass http://127.0.0.1:8481;
}
if ($query_string ~* "pd=3(&|$)") {
set $req_host inner.b.com;
proxy_pass http://127.0.0.1:8481;
}
if ($query_string ~* "ffs=.*(\{|%7B).*(%22|\")flag(%22|\")(:|%3A)(%22|\")?1(%22|\")+.*(\}|%7D)") {
set $req_host inner.c.com;
proxy_pass http://127.0.0.1:8481;
}
}
改进方法
1、在nginx中定义pd、flag字段用来存储pd、flag的值
set $pd 0;
set $flag 0;
2、在set_by_lua_file中使用正则进行提取pd、flag的值
set_by_lua_file:
local args = ngx.req.get_uri_args()
if args['action'] == 'test&#