openresty 根据参数 重定向请求,openresty重定向


1、nginx配置文件增加配置节

    location /a {
        default_type 'text/html';
        lua_code_cache on;
        rewrite_by_lua_file /data/lua/a.lua;
    }
    location @b {
        default_type 'text/html';
        lua_code_cache on;
        content_by_lua_file /data/lua/b.lua;
    }
    location @c {
        default_type 'text/html';
        lua_code_cache on;
        content_by_lua_file /data/lua/c.lua;
    }


vi a.lua

ngx.req.read_body()
local args, err = ngx.req.get_post_args()
if args["t1"]==nil then
  ngx.req.set_uri_args("t1=ccc")
  ngx.exec("@b")
else
  ngx.req.set_uri_args("t1=ccc")
  ngx.exec("@c")
end


vi b.lua

ngx.req.read_body()
local args, err = ngx.req.get_post_args()
ngx.say("b")
ngx.say(ngx.var.arg_t1)
ngx.say(args["t1"])
ngx.say(ngx.req.get_headers()["AAA"])   

                                  

vi c.lua

ngx.req.read_body()
local args, err = ngx.req.get_post_args()
ngx.say("c")
ngx.say(ngx.var.arg_t1)
ngx.say(args["t1"])
ngx.say(ngx.req.get_headers()["AAA"])                                

--[[

分别显示post,get参数和header信息

]]--

模拟发送请求,携带和不携带POST参数,显示不同的结果。

相关内容

    暂无相关文章