ngx_lua常用變數引數
Ngx指令
lua_code_cache on | off;
作用:開啟或關閉 Lua 程式碼快取,影響以下指令: set_by_lua_file , content_by_lua_file, rewrite_by_lua_file, access_by_lua_file 及強制載入或者reload Lua 模組等.快取開啟時修改LUA程式碼需要重啟nginx,不開啟時則不用。開發階段一般關閉快取。
作用域:main, server, location, location if
lua_regex_cache_max_entries 1024;
作用:未知(貌似是限定快取正則表示式處理結果的最大數量)
lua_package_path …/path… ;
作用:設定用lua程式碼寫的擴充套件庫路徑。
例:lua_package_path ‘/foo/bar/?.lua;/blah/?.lua;;’;
lua_package_cpath ‘/bar/baz/?.so;/blah/blah/?.so;;’;
作用:設定C擴充套件的lua庫路徑。
set_by_lua
作用:設定一個Nginx變數,變數值從lua腳本里運算由return返回,可以實現複雜的賦值邏輯;此處是阻塞的,Lua程式碼要做到非常快.
另外可以將已有的ngx變數當作引數傳進Lua腳本里去,由ngx.arg[1],ngx.arg[2]等方式訪問。
作用域:main, server, location, server if, location if
處理階段:rewrite
content_by_lua ‘’;
content_by_lua_file luafile;
作用域:location, location if
說明:內容處理器,接收請求處理並輸出響應,content_by_lua直接在nginx配置檔案裡編寫較短Lua程式碼後者使用lua檔案。
rewrite_by_lua ‘’
rewrite_by_lua_file lua_file;
作用域:http, server, location, location if
執行內部URL重寫或者外部重定向,典型的如偽靜態化的URL重寫。其預設執行在rewrite處理階段的最後.
注意,在使用rewrite_by_lua時,開啟rewrite_log on;後也看不到相應的rewrite log。
access_by_lua ‘lua code’;
access_by_lua_file lua_file.lua;
作用:用於訪問控制,比如我們只允許內網ip訪問,可以使用如下形式。
access_by_lua ’
if ngx.req.get_uri_args()[“token”] ~= “123” then
return ngx.exit(403)
end ‘;
作用域:http, server, location, location if
header_filter_by_lua ‘lua code’;
header_filter_by_lua_file path_file.lua;
作用:設定header 和 cookie;
lua_need_request_body on|off;
作用:是否讀請求體,跟ngx.req.read_body()函式作用類似,但官方不推薦使用此方法。
lua_shared_dict shared_data 10m;
作用:設定一個共享全域性變量表,在所有worker程序間共享。在lua指令碼中可以如下訪問它:
例:local shared_data = ngx.shared.shared_data
10m 不知是什麼意思。
init_by_lua ‘lua code’;
init_by_lua_file lua_file.lua;
作用域:http
說明:ginx Master程序載入配置時執行;通常用於初始化全域性配置/預載入Lua模組
init_worker_by_lua ‘lua code’;
init_worker_by_lua_file luafile.lua;
作用域:http
說明:每個Nginx Worker程序啟動時呼叫的計時器,如果Master程序不允許則只會在init_by_lua之後呼叫;通常用於定時拉取配置/資料,或者後端服務的健康檢查。
ngx.arg[index] #ngx指令引數,當這個變數在set_by_lua或者set_by_lua_file內使用的時候是隻讀的,指的是在配置指令輸入的引數.
ngx.var.varname #讀寫NGINX變數的值,最好在lua腳本里快取變數值,避免在當前請求的生命週期內記憶體的洩漏
ngx.config.ngx_lua_version #當前ngx_lua模組版本號
ngx.config.nginx_version #nginx版本
ngx.worker.exiting #當前worker程序是否正在關閉
ngx.worker.pid #當前worker程序的PID
ngx.config.nginx_configure #編譯時的./configure命令選項
ngx.config.prefix #編譯時的prefix選項
core constans: #ngx_lua 核心常量
ngx.OK (0)
ngx.ERROR (-1)
ngx.AGAIN (-2)
ngx.DONE (-4)
ngx.DECLINED (-5)
ngx.nil
http method constans: #經常在ngx.location.catpure和ngx.location.capture_multi方法中被呼叫.
ngx.HTTP_GET
ngx.HTTP_HEAD
ngx.HTTP_PUT
ngx.HTTP_POST
ngx.HTTP_DELETE
ngx.HTTP_OPTIONS
ngx.HTTP_MKCOL
ngx.HTTP_COPY
ngx.HTTP_MOVE
ngx.HTTP_PROPFIND
ngx.HTTP_PROPPATCH
ngx.HTTP_LOCK
ngx.HTTP_UNLOCK
ngx.HTTP_PATCH
ngx.HTTP_TRACE
http status constans: #http請求狀態常量
ngx.HTTP_OK (200)
ngx.HTTP_CREATED (201)
ngx.HTTP_SPECIAL_RESPONSE (300)
ngx.HTTP_MOVED_PERMANENTLY (301)
ngx.HTTP_MOVED_TEMPORARILY (302)
ngx.HTTP_SEE_OTHER (303)
ngx.HTTP_NOT_MODIFIED (304)
ngx.HTTP_BAD_REQUEST (400)
ngx.HTTP_UNAUTHORIZED (401)
ngx.HTTP_FORBIDDEN (403)
ngx.HTTP_NOT_FOUND (404)
ngx.HTTP_NOT_ALLOWED (405)
ngx.HTTP_GONE (410)
ngx.HTTP_INTERNAL_SERVER_ERROR (500)
ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)
ngx.HTTP_SERVICE_UNAVAILABLE (503)
ngx.HTTP_GATEWAY_TIMEOUT (504)
Nginx log level constants: #錯誤日誌級別常量 ,這些引數經常在ngx.log方法中被使用.
ngx.STDERR
ngx.EMERG
ngx.ALERT
ngx.CRIT
ngx.ERR
ngx.WARN
ngx.NOTICE
ngx.INFO
ngx.DEBUG
############
API中的方法:
############
print() #與 ngx.print()方法有區別,print() 相當於ngx.log()
ngx.ctx #這是一個lua的table,用於儲存ngx上下文的變數,在整個請求的生命週期內都有效,詳細參考官方
ngx.location.capture() #發出一個子請求,詳細用法參考官方文件。
ngx.location.capture_multi() #發出多個子請求,詳細用法參考官方文件。
ngx.status #讀或者寫當前請求的相應狀態. 必須在輸出相應頭之前被呼叫.
ngx.header.HEADER #訪問或設定http header頭資訊,詳細參考官方文件。
ngx.req.set_uri() #設定當前請求的URI,詳細參考官方文件
ngx.set_uri_args(args) #根據args引數重新定義當前請求的URI引數.
ngx.req.get_uri_args() #返回一個LUA TABLE,包含當前請求的全部的URL引數
ngx.req.get_post_args() #返回一個LUA TABLE,包括所有當前請求的POST引數
ngx.req.get_headers() #返回一個包含當前請求頭資訊的lua table.
ngx.req.set_header() #設定當前請求頭header某欄位值.當前請求的子請求不會受到影響.
ngx.req.read_body() #在不阻塞ngnix其他事件的情況下同步讀取客戶端的body資訊.[詳細]
ngx.req.discard_body() #明確丟棄客戶端請求的body
ngx.req.get_body_data() #以字串的形式獲得客戶端的請求body內容
ngx.req.get_body_file() #當傳送檔案請求的時候,獲得檔案的名字
ngx.req.set_body_data() #設定客戶端請求的BODY
ngx.req.set_body_file() #通過filename來指定當前請求的file data。
ngx.req.clear_header() #清求某個請求頭
ngx.exec(uri,args) #執行內部跳轉,根據uri和請求引數
ngx.redirect(uri, status) #執行301或者302的重定向。
ngx.send_headers() #傳送指定的響應頭
ngx.headers_sent #判斷頭部是否傳送給客戶端ngx.headers_sent=true
ngx.print(str) #傳送給客戶端的響應頁面
ngx.say() #作用類似ngx.print,不過say方法輸出後會換行
ngx.log(log.level,...) #寫入nginx日誌
ngx.flush() #將緩衝區內容輸出到頁面(重新整理響應)
ngx.exit(http-status) #結束請求並輸出狀態碼
ngx.eof() #明確指定關閉結束輸出流
ngx.escape_uri() #URI編碼(本函式對逗號,不編碼,而php的urlencode會編碼)
ngx.unescape_uri() #uri解碼
ngx.encode_args(table) #將tabel解析成url引數
ngx.decode_args(uri) #將引數字串編碼為一個table
ngx.encode_base64(str) #BASE64編碼
ngx.decode_base64(str) #BASE64解碼
ngx.crc32_short(str) #字串的crs32_short雜湊
ngx.crc32_long(str) #字串的crs32_long雜湊
ngx.hmac_sha1(str) #字串的hmac_sha1雜湊
ngx.md5(str) #返回16進位制MD5
ngx.md5_bin(str) #返回2進位制MD5
ngx.today() #返回當前日期yyyy-mm-dd
ngx.time() #返回當前時間戳
ngx.now() #返回當前時間
ngx.update_time() #重新整理後返回
ngx.localtime() #返回 yyyy-mm-dd hh:ii:ss
ngx.utctime() #返回yyyy-mm-dd hh:ii:ss格式的utc時間
ngx.cookie_time(sec) #返回用於COOKIE使用的時間
ngx.http_time(sec) #返回可用於http header使用的時間
ngx.parse_http_time(str) #解析HTTP頭的時間
ngx.is_subrequest #是否子請求(值為 true or false)
ngx.re.match(subject,regex,options,ctx) #ngx正則表示式匹配,詳細參考官網
ngx.re.gmatch(subject,regex,opt) #全域性正則匹配
ngx.re.sub(sub,reg,opt) #匹配和替換(未知)
ngx.re.gsub() #未知
ngx.shared.DICT #ngx.shared.DICT是一個table 裡面儲存了所有的全域性記憶體共享變數
ngx.shared.DICT.get
ngx.shared.DICT.get_stale
ngx.shared.DICT.set
ngx.shared.DICT.safe_set
ngx.shared.DICT.add
ngx.shared.DICT.safe_add
ngx.shared.DICT.replace
ngx.shared.DICT.delete
ngx.shared.DICT.incr
ngx.shared.DICT.flush_all
ngx.shared.DICT.flush_expired
ngx.shared.DICT.get_keys
ndk.set_var.DIRECTIVE #不懂