nginx利用ctx實現(xiàn)數(shù)據(jù)共享、修改上下文功能
環(huán)境: init_worker_by_lua, set_by_lua, rewrite_by_lua, access_by_lua, content_by_lua, header_filter_by_lua, body_filter_by_lua, log_by_lua, ngx.timer., balancer_by_lua
這個 Lua 表可以用來存儲基于請求的 Lua 環(huán)境數(shù)據(jù),其生存周期與當(dāng)前請求相同 (類似 Nginx 變量)。
參考下面例子,
location /test { rewrite_by_lua_block { ngx.ctx.foo = 76 } access_by_lua_block { ngx.ctx.foo = ngx.ctx.foo + 3 } content_by_lua_block { ngx.say(ngx.ctx.foo) } }
訪問 GET /test 輸出
79
也就是說,ngx.ctx.foo 條目跨越一個請求的 rewrite (重寫),access (訪問),和 content (內(nèi)容) 各處理階段保持一致。
每個請求,包括子請求,都有一份自己的 ngx.ctx 表。例如:
location /sub { content_by_lua_block { ngx.say("sub pre: ", ngx.ctx.blah) ngx.ctx.blah = 32 ngx.say("sub post: ", ngx.ctx.blah) } } location /main { content_by_lua_block { ngx.ctx.blah = 73 ngx.say("main pre: ", ngx.ctx.blah) local res = ngx.location.capture("/sub") ngx.print(res.body) ngx.say("main post: ", ngx.ctx.blah) } }
訪問 GET /main 輸出
main pre: 73
sub pre: nil
sub post: 32
main post: 73
這里,在子請求中修改 ngx.ctx.blah 條目并不影響父請求中的同名條目,因為它們各自維護不同版本的 ngx.ctx.blah。
內(nèi)部重定向?qū)⒋輾г颊埱笾械?ngx.ctx 數(shù)據(jù) (如果有),新請求將會有一個空白的 ngx.ctx 表。例如,
location /new { content_by_lua_block { ngx.say(ngx.ctx.foo) } } location /orig { content_by_lua_block { ngx.ctx.foo = "hello" ngx.exec("/new") } }
訪問 GET /orig 將輸出
nil
而不是原始的 "hello" 值。
任意數(shù)據(jù)值,包括 Lua 閉包與嵌套表,都可以被插入這個“魔法”表,也允許注冊自定義元方法。
也可以將 ngx.ctx 覆蓋為一個新 Lua 表,例如,
ngx.ctx = { foo = 32, bar = 54 }
當(dāng)用在 init_worker_by_lua* 環(huán)境中,這個表與當(dāng)前 Lua 句柄生命周期相同。
ngx.ctx 表查詢需要相對昂貴的元方法調(diào)用,這比通過用戶自己的函數(shù)參數(shù)直接傳遞基于請求的數(shù)據(jù)要慢得多。所以不要為了節(jié)約用戶函數(shù)參數(shù)而濫用此 API,因為它可能對性能有明顯影響。
而且由于元方法“魔法”,不要在 lua 模塊級別試圖使用 "local" 級別的 ngx.ctx ,例如 worker-level data sharing。下面示例是糟糕的:
-- mymodule.lua
local _M = {}
-- 下面一行的 ngx.ctx 是屬于單個請求的,但 ctx 變量是在 Lua 模塊級別
-- 并且屬于單個 worker 的。
local ctx = ngx.ctx function _M.main() ctx.foo = "bar" end return _M
應(yīng)使用下面方式替代:
-- mymodule.lua local _M = {} function _M.main(ctx) ctx.foo = "bar" end return _M
就是說,調(diào)用者對 ctx 表調(diào)用應(yīng)通過函數(shù)傳參方式完成。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
通過Nginx反向代理實現(xiàn)IP訪問分流的示例代碼
本篇文章主要介紹了通過Nginx反向代理實現(xiàn)IP訪問分流的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11503 service unavailable錯誤解決方案講解
這篇文章主要介紹了503 service unavailable錯誤解決方案講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08詳解Nginx服務(wù)器中配置全站HTTPS安全連接的方法
這篇文章主要介紹了詳解Nginx服務(wù)器中配置全站HTTPS安全連接的方法,其中要點還是在于SSL證書的申請,需要的朋友可以參考下2016-01-01使用Nginx進行URL轉(zhuǎn)發(fā)的配置案例
Nginx是一個高性能的Web服務(wù)器和反向代理服務(wù)器,它還可以用于URL轉(zhuǎn)發(fā),在本教學(xué)文章中,我們將從安裝Nginx開始,逐步介紹配置步驟,并展示一個URL轉(zhuǎn)發(fā)的案例,感興趣的朋友可以參考下2023-09-09Nginx geoip模塊實現(xiàn)地區(qū)性負載均衡
相信做過awstats的都用過開源的geoip.dat ip數(shù)據(jù)庫,剛好nginx wiki上有g(shù)eoip 模塊,這樣就可以實現(xiàn)地區(qū)性的負載均衡,但是maxmind 的ip數(shù)據(jù)庫對中國的支持不算太好,不過現(xiàn)在也不錯了~2010-12-12Nginx如何實現(xiàn)pathinfo模式的方法詳解
pathinfo是偽靜態(tài)的一種,對于用過thinkphp的朋友們來說應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Nginx如何實現(xiàn)pathinfo模式的方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。2017-09-09