其他分享
首页 > 其他分享> > openresty动态转发

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()
            }
        }

 

标签:ngx,adcode,say,version,openresty,转发,动态,local,response
来源: https://www.cnblogs.com/fengzi7314/p/15823469.html