openresty开发系列24--openresty中lua的引入及使用
openresty開發(fā)系列24--openresty中l(wèi)ua的引入及使用
openresty 引入 lua
一)openresty中nginx引入lua方式
? 1)xxx_by_lua?? --->字符串編寫方式
? 2) xxx_by_lua_block ---->代碼塊方式
? 3) xxx_by_lua_file? ---->直接引用一個(gè)lua腳本文件
我們案例中使用內(nèi)容處理階段,用content_by_lua演示
-----------------編輯nginx.conf-----------------------
第一種:content_by_lua
location /testlua {
? content_by_lua "ngx.say('hello world')";
}
輸出了hello world
content_by_lua 方式,參數(shù)為字符串,編寫不是太方便。
----------------------------------------
第二種:content_by_lua_block
location /testlua {
? content_by_lua_block {
?????? ngx.say("hello world");
? }
}
content_by_lua_block {}? 表示內(nèi)部為lua塊,里面可以應(yīng)用lua語句
----------------------------------------
第三種:content_by_lua_file
location /testlua {
? content_by_lua_file /usr/local/lua/test.lua;
}
content_by_lua_file 就是引用外部lua文件
# vi? test.lua
ngx.say("hello world");
二)openresty使用lua打印輸出案例
? location /testsay {
??? content_by_lua_block {
??????? --寫響應(yīng)頭 ?
??????? ngx.header.a = "1" ?
??????? ngx.header.b = "2"
??????? --輸出響應(yīng) ?
??????? ngx.say("a", "b", "<br/>") ?
??????? ngx.print("c", "d", "<br/>") ?
??????? --200狀態(tài)碼退出 ?
??????? return ngx.exit(200)
??? }
? }
? ngx.header:輸出響應(yīng)頭;
? ngx.print:輸出響應(yīng)內(nèi)容體;
? ngx.say:通ngx.print,但是會(huì)最后輸出一個(gè)換行符;
? ngx.exit:指定狀態(tài)碼退出。
三)介紹一下openresty使用lua常用的api
1)ngx.var : 獲取Nginx變量 和 內(nèi)置變量
nginx內(nèi)置的變量
$arg_name 請(qǐng)求中的name參數(shù)
$args 請(qǐng)求中的參數(shù)
$binary_remote_addr 遠(yuǎn)程地址的二進(jìn)制表示
$body_bytes_sent? 已發(fā)送的消息體字節(jié)數(shù)
$content_length HTTP請(qǐng)求信息里的"Content-Length"
$content_type 請(qǐng)求信息里的"Content-Type"
$document_root? 針對(duì)當(dāng)前請(qǐng)求的根路徑設(shè)置值
$document_uri 與$uri相同; 比如 /test2/test.php
$host 請(qǐng)求信息中的"Host",如果請(qǐng)求中沒有Host行,則等于設(shè)置的服務(wù)器名
$hostname 機(jī)器名使用 gethostname系統(tǒng)調(diào)用的值
$http_cookie? cookie 信息
$http_referer 引用地址
$http_user_agent? 客戶端代理信息
$http_via 最后一個(gè)訪問服務(wù)器的Ip地址。
$http_x_forwarded_for 相當(dāng)于網(wǎng)絡(luò)訪問路徑
$is_args? 如果請(qǐng)求行帶有參數(shù),返回"?",否則返回空字符串
$limit_rate 對(duì)連接速率的限制
$nginx_version? 當(dāng)前運(yùn)行的nginx版本號(hào)
$pid? worker進(jìn)程的PID
$query_string 與$args相同
$realpath_root? 按root指令或alias指令算出的當(dāng)前請(qǐng)求的絕對(duì)路徑。其中的符號(hào)鏈接都會(huì)解析成真是文件路徑
$remote_addr? 客戶端IP地址
$remote_port? 客戶端端口號(hào)
$remote_user? 客戶端用戶名,認(rèn)證用
$request? 用戶請(qǐng)求
$request_body 這個(gè)變量(0.7.58+)包含請(qǐng)求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比較有意義
$request_body_file? 客戶端請(qǐng)求主體信息的臨時(shí)文件名
$request_completion 如果請(qǐng)求成功,設(shè)為"OK";如果請(qǐng)求未完成或者不是一系列請(qǐng)求中最后一部分則設(shè)為空
$request_filename 當(dāng)前請(qǐng)求的文件路徑名,比如/opt/nginx/www/test.php
$request_method 請(qǐng)求的方法,比如"GET"、"POST"等
$request_uri? 請(qǐng)求的URI,帶參數(shù); 比如http://localhost:88/test1/
$scheme 所用的協(xié)議,比如http或者是https
$server_addr? 服務(wù)器地址,如果沒有用listen指明服務(wù)器地址,使用這個(gè)變量將發(fā)起一次系統(tǒng)調(diào)用以取得地址(造成資源浪費(fèi))
$server_name? 請(qǐng)求到達(dá)的服務(wù)器名
$server_port? 請(qǐng)求到達(dá)的服務(wù)器端口號(hào)
$server_protocol? 請(qǐng)求的協(xié)議版本,"HTTP/1.0"或"HTTP/1.1"
$uri? 請(qǐng)求的URI,可能和最初的值有不同,比如經(jīng)過重定向之類的
ngx.var.xxx
location /var {
??? set $c 3;
??? #處理業(yè)務(wù)
??? content_by_lua_block {
????? local a = tonumber(ngx.var.arg_a) or 0
????? local b = tonumber(ngx.var.arg_b) or 0
????? local c = tonumber(ngx.var.c) or 0
????? ngx.say("sum:", a + b + c )
??? }
}
注意:ngx.var.c 此變量必須提前聲明;
另外對(duì)于nginx location中使用正則捕獲的捕獲組可以使用ngx.var[捕獲組數(shù)字]獲取;
location ~ ^/var/([0-9]+) {
?? content_by_lua_block {
??? ngx.say("var[1]:", ngx.var[1] )
? }
}
2)ngx.req請(qǐng)求模塊的常用api
?? ngx.req.get_headers:獲取請(qǐng)求頭,
?? 獲取帶中劃線的請(qǐng)求頭時(shí)請(qǐng)使用如headers.user_agent這種方式;如果一個(gè)請(qǐng)求頭有多個(gè)值,則返回的是table;
-----------test.lua-------------------
local headers = ngx.req.get_headers() ?
ngx.say("============headers begin===============", "<br/>") ?
ngx.say("Host : ", headers["Host"], "<br/>") ?
ngx.say("headers['user-agent'] : ", headers["user-agent"], "<br/>") ?
ngx.say("headers.user_agent : ", headers.user_agent, "<br/>")
ngx.say("-------------遍歷headers-----------", "<br/>")
for k,v in pairs(headers) do ?
??? if type(v) == "table" then ?
??????? ngx.say(k, " : ", table.concat(v, ","), "<br/>") ?
??? else ?
??????? ngx.say(k, " : ", v, "<br/>") ?
??? end ?
end ?
ngx.say("===========headers end============", "<br/>") ?
ngx.say("<br/>") ?
3)獲取請(qǐng)求參數(shù)
? ngx.req.get_uri_args:獲取url請(qǐng)求參數(shù),其用法和get_headers類似;
? ngx.req.get_post_args:獲取post請(qǐng)求內(nèi)容體,其用法和get_headers類似,
???????????????????????? 但是必須提前調(diào)用ngx.req.read_body()來讀取body體
???????????????????????? (也可以選擇在nginx配置文件使用lua_need_request_body on;開啟讀取body體,
?????????????????????????? 但是官方不推薦);
? ngx.req.get_body_data:為解析的請(qǐng)求body體內(nèi)容字符串。
---------------test.lua---------------
--get請(qǐng)求uri參數(shù) ?
ngx.say("===========uri get args begin==================", "<br/>") ?
local uri_args = ngx.req.get_uri_args() ?
for k, v in pairs(uri_args) do ?
??? if type(v) == "table" then ?
??????? ngx.say(k, " : ", table.concat(v, ", "), "<br/>") ?
??? else ?
??????? ngx.say(k, ": ", v, "<br/>") ?
??? end ?
end ?
ngx.say("===========uri get args end==================", "<br/>")
?
--post請(qǐng)求參數(shù) ?
ngx.req.read_body() ?
ngx.say("=================post args begin====================", "<br/>") ?
local post_args = ngx.req.get_post_args() ?
for k, v in pairs(post_args) do ?
??? if type(v) == "table" then ?
??????? ngx.say(k, " : ", table.concat(v, ", "), "<br/>") ?
??? else ?
??????? ngx.say(k, ": ", v, "<br/>") ?
??? end ?
end ?
ngx.say("================post args end=====================", "<br/>") ?
?
4) ngx.req其他常用的api
--請(qǐng)求的http協(xié)議版本 ?
ngx.say("ngx.req.http_version : ", ngx.req.http_version(), "<br/>") ?
--請(qǐng)求方法 ?
ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "<br/>") ?
--原始的請(qǐng)求頭內(nèi)容 ?
ngx.say("ngx.req.raw_header : ",? ngx.req.raw_header(), "<br/>") ?
--請(qǐng)求的body內(nèi)容體 ?
ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data(), "<br/>") ?
ngx.say("<br/>") ?
ngx.req.raw_header()這個(gè)函數(shù)返回值為字符串
5)編碼解碼
ngx.escape_uri/ngx.unescape_uri : uri編碼解碼;
ngx.encode_args/ngx.decode_args:參數(shù)編碼解碼;
ngx.encode_base64/ngx.decode_base64:BASE64編碼解碼;
-------test.lua
--未經(jīng)解碼的請(qǐng)求uri ?
local request_uri = ngx.var.request_uri; ?
ngx.say("request_uri : ", request_uri, "<br/>");
--編碼
local escape_uri = ngx.escape_uri(request_uri)
ngx.say("escape_uri : ", escape_uri, "<br/>");
--解碼 ?
ngx.say("decode request_uri : ", ngx.unescape_uri(escape_uri), "<br/>");
--參數(shù)編碼
local request_uri = ngx.var.request_uri;
local question_pos, _ = string.find(request_uri, '?')
if question_pos>0 then
? local uri = string.sub(request_uri, 1, question_pos-1)
? ngx.say("uri sub=",string.sub(request_uri, question_pos+1),"<br/>");
?
? --對(duì)字符串進(jìn)行解碼
? local args = ngx.decode_args(string.sub(request_uri, question_pos+1))
?
? for k,v in pairs(args) do
??? ngx.say("k=",k,",v=", v, "<br/>");
? end
?
? if args and args.userId then
??? args.userId = args.userId + 10000
??? ngx.say("args+10000 : ", uri .. '?' .. ngx.encode_args(args), "<br/>");
? end
end
6)md5加密api
--MD5 ?
ngx.say("ngx.md5 : ", ngx.md5("123"), "<br/>") ?
7)nginx獲取時(shí)間
之前介紹的os.time()會(huì)涉及系統(tǒng)調(diào)用,性能比較差,推薦使用nginx中的時(shí)間api
ngx.time()? --返回秒級(jí)精度的時(shí)間戳
ngx.now()?? --返回毫秒級(jí)精度的時(shí)間戳
就是通過這兩種方式獲取到的只是nginx緩存起來的時(shí)間戳,不是實(shí)時(shí)的。
所以有時(shí)候會(huì)出現(xiàn)一些比較奇怪的現(xiàn)象,比如下面代碼:
local t1 = ngx.now()
for i=1,1000000 do
end
local t2 = ngx.now()
ngx.say(t1, ",", t2) -- t1和t2的值是一樣的,why?
ngx.exit(200)
正常來說,t2應(yīng)該大于t1才對(duì),但由于nginx沒有及時(shí)更新(緩存的)時(shí)間戳,所以導(dǎo)致t2和t1獲取到的時(shí)間戳是一樣的。
那么怎樣才能強(qiáng)迫nginx更新緩存呢?調(diào)用多一個(gè)ngx.update_time()函數(shù)即可:
local t1 = ngx.now()
for i=1,1000000 do
end
ngx.update_time()
local t2 = ngx.now()
ngx.say(t1, ",", t2)
ngx.exit(200)
8)ngx.re模塊中正則表達(dá)式相關(guān)的api
ngx.re.match
ngx.re.sub
ngx.re.gsub
ngx.re.find
ngx.re.gmatch
我們這里只簡單的介紹 ngx.re.match,詳細(xì)用法可以自行去網(wǎng)上學(xué)習(xí)
ngx.re.match
只有第一次匹配的結(jié)果被返回,如果沒有匹配,則返回nil;或者匹配過程中出現(xiàn)錯(cuò)誤時(shí),
也會(huì)返回nil,此時(shí)錯(cuò)誤信息會(huì)被保存在err中。
當(dāng)匹配的字符串找到時(shí),一個(gè)Lua table captures會(huì)被返回,
captures[0]中保存的就是匹配到的字串,
captures[1]保存的是用括號(hào)括起來的第一個(gè)子模式(捕獲分組)的結(jié)果,
captures[2]保存的是第二個(gè)子模式(捕獲分組)的結(jié)果,依次類似。
---------------------
local m, err = ngx.re.match("hello, 1234", "[0-9]+")
if m then
? ngx.say(m[0])
else
? if err then
??? ngx.log(ngx.ERR, "error: ", err)
??? return
? end
? ngx.say("match not found")
end
上面例子中,匹配的字符串是1234,因此m[0] == "1234",
--------------
local m, err = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
ngx.say(m[0],"<br/>")
ngx.say(m[1])
---------------------------------------------------------
備注:有沒有注意到,我們每次修改都要重啟nginx,這樣太過于麻煩,我們可以用
content_by_lua_file 引入外部lua,這樣的話 只要修改外部的lua,就可以了,不需要重啟nginx了。
注意需要把lua_code_cache 設(shè)置為off,實(shí)際生產(chǎn)環(huán)境是需要設(shè)置為on的
語法:lua_code_cache on | off
默認(rèn): on
適用上下文:http、server、location、location if
這個(gè)指令是指定是否開啟lua的代碼編譯緩存,開發(fā)時(shí)可以設(shè)置為off,以便lua文件實(shí)時(shí)生效,
如果是生產(chǎn)線上,為了性能,建議開啟。
最終nginx.conf修改為
以后我們只要修改test.lua 文件就可以了。
**********生產(chǎn)環(huán)境不建議修改
9)標(biāo)準(zhǔn)日志輸出
ngx.log(log_level, ...)
日志輸出級(jí)別
ngx.STDERR???? -- 標(biāo)準(zhǔn)輸出
ngx.EMERG????? -- 緊急報(bào)錯(cuò)
ngx.ALERT????? -- 報(bào)警
ngx.CRIT?????? -- 嚴(yán)重,系統(tǒng)故障,觸發(fā)運(yùn)維告警系統(tǒng)
ngx.ERR??????? -- 錯(cuò)誤,業(yè)務(wù)不可恢復(fù)性錯(cuò)誤
ngx.WARN?????? -- 告警,業(yè)務(wù)中可忽略錯(cuò)誤
ngx.NOTICE???? -- 提醒,業(yè)務(wù)比較重要信息
ngx.INFO?????? -- 信息,業(yè)務(wù)瑣碎日志信息,包含不同情況判斷等
ngx.DEBUG????? -- 調(diào)試
-------------------------------------
#user? nobody;
worker_processes? 1;
error_log? logs/error.log error;??? # 日志級(jí)別
#pid??????? logs/nginx.pid;
events {
??? worker_connections? 1024;
}
http {
??? server {
??????? listen??? 80;
??????? location / {
??????????? content_by_lua_block {
??????????????? local num = 55
??????????????? local str = "string"
??????????????? local obj
??????????????? ngx.log(ngx.ERR, "num:", num)
??????????????? ngx.log(ngx.INFO, " string:", str)
??????????????? print([[i am print]])
??????????????? ngx.log(ngx.ERR, " object:", obj)
??????????? }
??????? }
??? }
}
日志輸出級(jí)別使用的 error,只有等于或大于這個(gè)級(jí)別的日志才會(huì)輸出
ngx.DEBUG
ngx.WARN
對(duì)于應(yīng)用開發(fā),一般使用 ngx.INFO 到 ngx.CRIT 就夠了。生產(chǎn)中錯(cuò)誤日志開啟到 error 級(jí)別就夠了
10)重定向 ngx.redirect
-----重定向
location = /bar {
? content_by_lua_block {
??? ngx.say([[I am bar]])
? }
}
location = /foo {
? rewrite_by_lua_block {
??? return ngx.redirect('/bar');
? }
}
11)不同階段共享變量
ngx.ctx 全局共享變量
在 OpenResty 的體系中,可以通過共享內(nèi)存的方式完成不同工作進(jìn)程的數(shù)據(jù)共享,
本地內(nèi)存方式 去讓不同的工作進(jìn)程共享數(shù)據(jù)
openresty有不同處理階段,后面的課程會(huì)介紹。在不同的處理階段,如何共享數(shù)據(jù)
可以通過 Lua 模塊方式完成單個(gè)進(jìn)程內(nèi)不同請(qǐng)求的數(shù)據(jù)共享。如何完成單個(gè)請(qǐng)求內(nèi)不同階段的數(shù)據(jù)共享呢?
ngx.ctx 表就是為了解決這類問題而設(shè)計(jì)的。參考下面例子:
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)
???? }
?}
?ngx.ctx.xxxxx
首先 ngx.ctx 是一個(gè)表,所以我們可以對(duì)他添加、修改。它用來存儲(chǔ)基于請(qǐng)求的 Lua 環(huán)境數(shù)據(jù),
其生存周期與當(dāng)前請(qǐng)求相同 (類似 Nginx 變量)。它有一個(gè)最重要的特性:
單個(gè)請(qǐng)求內(nèi)的 rewrite (重寫),access (訪問),和 content (內(nèi)容) 等各處理階段是保持一致的。
額外注意,每個(gè)請(qǐng)求,包括子請(qǐng)求,都有一份自己的 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)
???? }
?}
ngx.ctx 表查詢需要相對(duì)昂貴的元方法調(diào)用,這比通過用戶自己的函數(shù)參數(shù)直接傳遞基于請(qǐng)求的數(shù)據(jù)要慢得多。
所以不要為了節(jié)約用戶函數(shù)參數(shù)而濫用此 API,因?yàn)樗赡軐?duì)性能有明顯影響。
由于 ngx.ctx 保存的是指定請(qǐng)求資源,所以這個(gè)變量是不能直接共享給其他請(qǐng)求使用的。
更多api使用? https://www.nginx.com/resources/wiki/modules/lua/#nginx-api-for-lua
操作指令? 說明
ngx.arg 指令參數(shù),如跟在content_by_lua_file后面的參數(shù)
ngx.var 變量,ngx.var.VARIABLE引用某個(gè)變量
ngx.ctx 請(qǐng)求的lua上下文
ngx.header? 響應(yīng)頭,ngx.header.HEADER引用某個(gè)頭
ngx.status? 響應(yīng)碼
API 說明
ngx.log 輸出到error.log
print 等價(jià)于 ngx.log(ngx.NOTICE, ...)
ngx.send_headers? 發(fā)送響應(yīng)頭
ngx.headers_sent? 響應(yīng)頭是否已發(fā)送
ngx.resp.get_headers? 獲取響應(yīng)頭
ngx.timer.at? 注冊(cè)定時(shí)器事件
ngx.is_subrequest 當(dāng)前請(qǐng)求是否是子請(qǐng)求
ngx.location.capture? 發(fā)布一個(gè)子請(qǐng)求
ngx.location.capture_multi? 發(fā)布多個(gè)子請(qǐng)求
ngx.exec? ?
ngx.redirect? ?
ngx.print 輸出響應(yīng)
ngx.say 輸出響應(yīng),自動(dòng)添加'n'
ngx.flush 刷新響應(yīng)
ngx.exit? 結(jié)束請(qǐng)求
ngx.eof ?
ngx.sleep 無阻塞的休眠(使用定時(shí)器實(shí)現(xiàn))
ngx.get_phase ?
ngx.on_abort? 注冊(cè)client斷開請(qǐng)求時(shí)的回調(diào)函數(shù)
ndk.set_var.DIRECTIVE ?
ngx.req.start_time? 請(qǐng)求的開始時(shí)間
ngx.req.http_version? 請(qǐng)求的HTTP版本號(hào)
ngx.req.raw_header? 請(qǐng)求頭(包括請(qǐng)求行)
ngx.req.get_method? 請(qǐng)求方法
ngx.req.set_method? 請(qǐng)求方法重載
ngx.req.set_uri 請(qǐng)求URL重寫
ngx.req.set_uri_args? ?
ngx.req.get_uri_args? 獲取請(qǐng)求參數(shù)
ngx.req.get_post_args 獲取請(qǐng)求表單
ngx.req.get_headers 獲取請(qǐng)求頭
ngx.req.set_header? ?
ngx.req.clear_header? ?
ngx.req.read_body 讀取請(qǐng)求體
ngx.req.discard_body? 扔掉請(qǐng)求體
ngx.req.get_body_data ?
ngx.req.get_body_file ?
ngx.req.set_body_data ?
ngx.req.set_body_file ?
ngx.req.init_body ?
ngx.req.append_body ?
ngx.req.finish_body ?
ngx.req.socket? ?
ngx.escape_uri? 字符串的url編碼
ngx.unescape_uri? 字符串url解碼
ngx.encode_args 將table編碼為一個(gè)參數(shù)字符串
ngx.decode_args 將參數(shù)字符串編碼為一個(gè)table
ngx.encode_base64 字符串的base64編碼
ngx.decode_base64 字符串的base64解碼
ngx.crc32_short 字符串的crs32_short哈希
ngx.crc32_long? 字符串的crs32_long哈希
ngx.hmac_sha1 字符串的hmac_sha1哈希
ngx.md5 返回16進(jìn)制MD5
ngx.md5_bin 返回2進(jìn)制MD5
ngx.sha1_bin? 返回2進(jìn)制sha1哈希值
ngx.quote_sql_str SQL語句轉(zhuǎn)義
ngx.today 返回當(dāng)前日期
ngx.time? 返回UNIX時(shí)間戳
ngx.now 返回當(dāng)前時(shí)間
ngx.update_time 刷新時(shí)間后再返回
ngx.localtime ?
ngx.utctime ?
ngx.cookie_time 返回的時(shí)間可用于cookie值
ngx.http_time 返回的時(shí)間可用于HTTP頭
ngx.parse_http_time 解析HTTP頭的時(shí)間
ngx.re.match? ?
ngx.re.find ?
ngx.re.gmatch ?
ngx.re.sub? ?
ngx.re.gsub ?
ngx.shared.DICT ?
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? ?
ngx.socket.udp? ?
udpsock:setpeername ?
udpsock:send? ?
udpsock:receive ?
udpsock:close ?
udpsock:settimeout? ?
ngx.socket.tcp? ?
tcpsock:connect ?
tcpsock:sslhandshake? ?
tcpsock:send? ?
tcpsock:receive ?
tcpsock:receiveuntil? ?
tcpsock:close ?
tcpsock:settimeout? ?
tcpsock:setoption ?
tcpsock:setkeepalive? ?
tcpsock:getreusedtimes? ?
ngx.socket.connect? ?
ngx.thread.spawn? ?
ngx.thread.wait ?
ngx.thread.kill ?
coroutine.create? ?
coroutine.resume? ?
coroutine.yield ?
coroutine.wrap? ?
coroutine.running ?
coroutine.status? ?
ngx.config.debug? 編譯時(shí)是否有 --with-debug選項(xiàng)
ngx.config.prefix 編譯時(shí)的 --prefix選項(xiàng)
ngx.config.nginx_version? 返回nginx版本號(hào)
ngx.config.nginx_configure? 返回編譯時(shí) ./configure的命令行選項(xiàng)
ngx.config.ngx_lua_version? 返回ngx_lua模塊版本號(hào)
ngx.worker.exiting? 當(dāng)前worker進(jìn)程是否正在關(guān)閉(如reload、shutdown期間)
ngx.worker.pid? 返回當(dāng)前worker進(jìn)程的pid
? ?
常量說明
ngx.OK (0)
ngx.ERROR (-1)
ngx.AGAIN (-2)
ngx.DONE (-4)
ngx.DECLINED (-5)
ngx.nil
HTTP 請(qǐng)求方式
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 返回狀態(tài)
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)
轉(zhuǎn)載于:https://www.cnblogs.com/reblue520/p/11434252.html
總結(jié)
以上是生活随笔為你收集整理的openresty开发系列24--openresty中lua的引入及使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openresty开发系列23--lua
- 下一篇: openresty开发系列25--ope