ngx_lua 模塊

ngx_lua模塊的原理:

1、每個worker(工作進程)創建一個Lua VM,worker內所有協程共享VM;
2、將Nginx I/O原語封裝後注入 Lua VM,允許Lua代碼直接訪問;
3、每個外部請求都由一個Lua協程處理,協程之間數據隔離;
4、Lua代碼調用I/O操作等異步接口時,會掛起當前協程(並保護上下文數據),而不阻塞worker;
5、I/O等異步操作完成時還原相關協程上下文數據,並繼續運行;

 

ngx_lua 模塊提供的指令和API等:

指令名稱 說明
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  

 

 

 

table 說明
ngx.arg 指令參數,如跟在content_by_lua_file後面的參數
ngx.var 變量,ngx.var.VARIABLE引用某個變量
ngx.ctx 請求的lua上下文
ngx.header 響應頭,ngx.header.HEADER引用某個頭
ngx.status 響應碼
   
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
   
   
   
常量 說明
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章