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= 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(...)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM