環境: 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., balance. 這個 Lua 表可以用來儲存基於請求的 Lua 環境數據,其生存週期與目前請求相同 (類似 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 (內容) 各處理階段保持一致。
每個請求,包括子請求,都有一份自己的 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。
內部重定向將摧毀原始請求中的 ngx.ctx 資料 (如果有),新請求將會有一個空白的 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" 值。
任意資料值,包括 Lua 閉包與巢狀表,都可以插入這個「魔法」表,也允許註冊自訂元方法。
也可以將 ngx.ctx 覆寫為一個新 Lua 表,例如,
ngx.ctx = { foo = 32, bar = 54 }
當用在 init_worker_by_lua* 環境中,這個表與目前 Lua 句柄生命週期相同。
ngx.ctx 表查詢需要相對昂貴的元方法調用,這比透過使用者自己的函數參數直接傳遞基於請求的資料要慢得多。所以不要為了節約用戶函數參數而濫用此 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
應使用下面方式替代:
-- mymodule.lua
local _M = {}
# function _M.main(ctx)
ctx.foo = "bar"
# end
return _M
就是說,呼叫者對 ctx 表呼叫應透過函數傳參方式完成。
以上是nginx怎麼使用ctx實現資料共享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

NGINX適合處理高並發請求,Apache適合需要復雜配置和功能擴展的場景。 1.NGINX採用事件驅動、非阻塞架構,適用於高並發環境。 2.Apache採用進程或線程模型,提供豐富的模塊生態系統,適合複雜配置需求。

NGINX可用於提升網站性能、安全性和可擴展性。 1)作為反向代理和負載均衡器,NGINX可優化後端服務和分擔流量。 2)通過事件驅動和異步架構,NGINX高效處理高並發連接。 3)配置文件允許靈活定義規則,如靜態文件服務和負載均衡。 4)優化建議包括啟用Gzip壓縮、使用緩存和調整worker進程。

NGINXUnit支持多種編程語言,通過模塊化設計實現。 1.加載語言模塊:根據配置文件加載相應模塊。 2.應用啟動:調用語言運行時執行應用代碼。 3.請求處理:將請求轉發給應用實例。 4.響應返回:將處理後的響應返回給客戶端。

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具