# Nginx\_Lua API
#
## 一、介紹
各種**\* \_by\_lua**,**\* \_by\_lua\_block**和**\* \_by\_lua\_file**配置指令用作nginx.conf文件中Lua API的網關。 下面描述的Nginx Lua API只能在這些配置指令的上下文中運行的用戶Lua代碼中調用。API以兩個標準軟件包ngx和ndk的形式暴露給Lua。 這些軟件包位于ngx\_lua中的默認全局范圍內,并且始終可在ngx\_lua指令中使用。
### ngx\_lua \[指令\]表
| 指令 | 說明 |
| :--- | :--- |
| lua\_use\_default\_type | 是否使用default\_type指令定義的Content-Type默認值 |
| lua\_code\_cache | \*\_by\_lua\_file文件是否cache |
| lua\_regex\_cache\_max\_entries | |
| lua\_regex\_match\_limit | |
| lua\_package\_path | 用Lua寫的lua外部庫路徑(.lua文件) |
| lua\_package\_cpath | 用C寫的lua外部庫路徑(.so文件) |
| init\_by\_lua | master進程啟動時掛載的lua代碼 |
| init\_by\_lua\_file | |
| init\_worker\_by\_lua | worker進程啟動時掛載的lua代碼,常用來執行一些定時器任務 |
| init\_worker\_by\_lua\_file | |
| set\_by\_lua | 設置變量 |
| set\_by\_lua\_file | |
| content\_by\_lua | handler模塊 |
| content\_by\_lua\_file | |
| rewrite\_by\_lua | |
| rewrite\_by\_lua\_file | |
| access\_by\_lua | |
| access\_by\_lua\_file | |
| header\_filter\_by\_lua | header filter模塊 |
| header\_filter\_by\_lua\_file | |
| body\_filter\_by\_lua | body filter模塊,ngx.arg\[1\]代表輸入的chunk,ngx.arg\[2\]代表當前chunk是否為last |
| body\_filter\_by\_lua\_file | |
| log\_by\_lua | |
| log\_by\_lua\_file | |
| lua\_need\_request\_body | 是否讀請求體,跟ngx.req.read\_body\(\)函數作用類似 |
| lua\_shared\_dict | 創建全局共享的table(多個worker進程共享) |
| lua\_socket\_connect\_timeout | TCP/unix 域socket對象connect方法的超時時間 |
| lua\_socket\_send\_timeout | TCP/unix 域socket對象send方法的超時時間 |
| lua\_socket\_send\_lowat | 設置cosocket send buffer的low water值 |
| lua\_socket\_read\_timeout | TCP/unix 域socket對象receive方法的超時時間 |
| lua\_socket\_buffer\_size | cosocket讀buffer大小 |
| lua\_socket\_pool\_size | cosocket連接池大小 |
| lua\_socket\_keepalive\_timeout | cosocket長連接超時時間 |
| lua\_socket\_log\_errors | 是否打開cosocket錯誤日志 |
| lua\_ssl\_ciphers | |
| lua\_ssl\_crl | |
| lua\_ssl\_protocols | |
| lua\_ssl\_trusted\_certificate | |
| lua\_ssl\_verify\_depth | |
| lua\_http10\_buffering | |
| rewrite\_by\_lua\_no\_postpone | |
| lua\_transform\_underscores\_in\_response\_headers | |
| lua\_check\_client\_abort | 是否監視client提前關閉請求的事件,如果打開監視,會調用ngx.on\_abort\(\)注冊的回調 |
| lua\_max\_pending\_timers | |
| lua\_max\_running\_timers | |
### ngx\_lua \[table\]表
| | 說明 |
| :--- | :--- |
| ngx.arg | 指令參數,如跟在content\_by\_lua\_file后面的參數 |
| ngx.var | 變量,ngx.var.VARIABLE引用某個變量 |
| ngx.ctx | 請求的lua上下文 |
| ngx.header | 響應頭,ngx.header.HEADER引用某個頭 |
| ngx.status | 響應碼 |
### ngx\_lua \[lua-內置api\]表
| API | 說明 |
| :--- | :--- |
| ngx.log | 輸出到error.log |
| print | 等價于 ngx.log\(ngx.NOTICE, ...\) |
| ngx.send\_headers | 發送響應頭 |
| ngx.headers\_sent | 響應頭是否已發送 |
| ngx.resp.get\_headers | 獲取響應頭 |
| ngx.timer.at | 注冊定時器事件 |
| ngx.is\_subrequest | 當前請求是否是子請求 |
| ngx.location.capture | 發布一個子請求 |
| ngx.location.capture\_multi | 發布多個子請求 |
| ngx.exec | |
| ngx.redirect | |
| ngx.print | 輸出響應 |
| ngx.say | 輸出響應,自動添加'\n' |
| ngx.flush | 刷新響應 |
| ngx.exit | 結束請求 |
| ngx.eof | |
| ngx.sleep | 無阻塞的休眠(使用定時器實現) |
| ngx.get\_phase | |
| ngx.on\_abort | 注冊client斷開請求時的回調函數 |
| ndk.set\_var.DIRECTIVE | |
| ngx.req.start\_time | 請求的開始時間 |
| ngx.req.http\_version | 請求的HTTP版本號 |
| ngx.req.raw\_header | 請求頭(包括請求行) |
| ngx.req.get\_method | 請求方法 |
| ngx.req.set\_method | 請求方法重載 |
| ngx.req.set\_uri | 請求URL重寫 |
| ngx.req.set\_uri\_args | |
| ngx.req.get\_uri\_args | 獲取請求參數 |
| ngx.req.get\_post\_args | 獲取請求表單 |
| ngx.req.get\_headers | 獲取請求頭 |
| ngx.req.set\_header | |
| ngx.req.clear\_header | |
| ngx.req.read\_body | 讀取請求體 |
| ngx.req.discard\_body | 扔掉請求體 |
| 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編碼為一個參數字符串 |
| ngx.decode\_args | 將參數字符串編碼為一個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進制MD5 |
| ngx.md5\_bin | 返回2進制MD5 |
| ngx.sha1\_bin | 返回2進制sha1哈希值 |
| ngx.quote\_sql\_str | SQL語句轉義 |
| ngx.today | 返回當前日期 |
| ngx.time | 返回UNIX時間戳 |
| ngx.now | 返回當前時間 |
| ngx.update\_time | 刷新時間后再返回 |
| ngx.localtime | |
| ngx.utctime | |
| ngx.cookie\_time | 返回的時間可用于cookie值 |
| ngx.http\_time | 返回的時間可用于HTTP頭 |
| ngx.parse\_http\_time | 解析HTTP頭的時間 |
| 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 | 編譯時是否有 --with-debug選項 |
| ngx.config.prefix | 編譯時的 --prefix選項 |
| ngx.config.nginx\_version | 返回nginx版本號 |
| ngx.config.nginx\_configure | 返回編譯時 ./configure的命令行選項 |
| ngx.config.ngx\_lua\_version | 返回ngx\_lua模塊版本號 |
| ngx.worker.exiting | 當前worker進程是否正在關閉(如reload、shutdown期間) |
| ngx.worker.pid | 返回當前worker進程的pid |
## ngx\_lua \[常量\]表
| 常量 | 說明 |
| :--- | :--- |
| Core constants | ngx.OK \(0\) ngx.ERROR \(-1\) ngx.AGAIN \(-2\) ngx.DONE \(-4\) ngx.DECLINED \(-5\) ngx.nil |
| HTTP method constants | 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 constants | 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.STDERR ngx.EMERG ngx.ALERT ngx.CRIT ngx.ERR ngx.WARN ngx.NOTICE ngx.INFO ngx.DEBUG |
##
##
- 1 Lua介紹及環境
- 2 基本語法
- 3 數據類型
- 4 Lua 變量
- 5 循環
- 6 流程控制
- 7 函數
- 8 運算符
- 9 字符串
- 10 數組
- 11 迭代器
- 12 table
- 13 Lua 模塊與包
- 14 Lua 元表(Metatable)
- 14.1 元表案例
- 15 Lua 協同程序(coroutine)
- 16 Lua 文件IO
- 17 Lua 面向對象
- 17.1 類
- 17.2 繼承
- 17.3 封裝
- 18 Lua 與 Mysql
- 19 Lua 與 redis
- 20 Lua 與 JSON
- 21 Lua 與 http
- 22 Lua 與 Nginx
- 22.1 Nginx_Lua的安裝及環境
- 22.2 ngx_lua API(全表)
- 22.3 常用命令介紹
- 22 Lua 人工智能
- (1) Torch的安裝
- (2)Tensor
- Lua與C混合編程