openresty动态转发
location /lua { resolver 114.114.114.114; content_by_lua_block { local args,error = ngx.req.get_uri_args() local adcode = args['adcode'] local version = args['version'] ngx.say('adcode--->',adcode) ngx.say('version--->',version) local http = require("resty.http") local httpc = http.new() local url = nil if version and adcode then url = "http://127.0.0.1/?adcode="..adcode.."&".."version="..version ngx.say(url) return end local response, error = httpc:request_uri(url, { method = "GET", path = "/", headers = { ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36" } }) if not response then ngx.say(11111111111 .. error) return end ngx.status = response.status for k, v in pairs(response.headers) do if k ~= "Transfer-Encoding" and k ~= "Connection" then ngx.header[k] = v end end ngx.say(response.body) httpc:close() } }
第二种方法
location /lua { resolver 114.114.114.114; set $target "https://www.taobao.com"; access_by_lua_block { local args,error = ngx.req.get_uri_args() local adcode = args['adcode'] local version = args['version'] if version and adcode then ngx.var.target = "https://www.baidu.com/" end } echo "$target"; proxy_pass $target; }
根据post请求参数进行拦截
location / { try_files $uri $uri/ /index.php?$query_string; access_by_lua_block { if ngx.var.request_method == "POST" then ngx.req.read_body() local p_args = ngx.req.get_post_args() for k,v in pairs(p_args) do if string.match(k, "1111") then ngx.say("2222") end if string.match(k, "2222") then ngx.say("2222") end end end } }
测试命令
[root@VM-0-15-centos conf.d]# curl -XPOST -d '{"name":"2222","age":"12"}' http://127.0.0.1:8888 2222 [root@VM-0-15-centos conf.d]# curl -XPOST -d '{"name":"1111","age":"12"}' http://127.0.0.1:8888 1111

浙公网安备 33010602011771号