您的位置:首页 > 编程语言 > Lua

ngx_lua_API 指令详解(四)ngx.exec指令

2017-06-13 14:41 399 查看
https://github.com/openresty/lua-nginx-module#ngxexec

参照:http://blog.csdn.net/weiyuefei/article/details/38434797

在Nginx中实现重定向可以通过rewrite指令,具体可参考《Nginx学习——http_rewrite_module的rewrite指令

通过Lua模块也可以实现同样的功能,Lua模块提供了相关的API来实现重定向的功能,

语法:

syntax: ngx.exec(uri, args?)

context: rewrite_by_lua*, access_by_lua*, content_by_lua*

1、主要实现的是内部的重定向,等价于下面的rewrite指令 rewrite regrex replacement last;

location /foo {
content_by_lua_block {
ngx.exec("/bar", "a=goodbye");
}
}

location /bar {
content_by_lua_block {
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
if key == "a" then
ngx.say(val)
end
end
}
}


curl http://192.168.18.180:8088/foo 输出为 goodbye

2、 args参数可以以string的形式给出,也可以以lua table的形式给出,如下所示:

location /foo {
content_by_lua_block {
ngx.exec("/bar", { a= 4, b="hello world"});
}
}

location /bar {
content_by_lua_block {
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
ngx.say(key.." = "..val)
end
}
}




3. 该方法不会主动返回,因此,强烈建议在调用该方法时,最好显示加上return,如下所示:

return ngx.exec(...)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: