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

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= , 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
}
}

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

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

return ngx.exec(...)
上一篇:【DB2数据库在windows平台上的安装】


下一篇:请在微信客户端打开链接 html