#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 $var '<lua-script>' [$arg1 $arg2];
set_by_lua_file $var <path-to-lua-script-file> [$arg1 $arg2 ...];
做用:設置一個Nginx變量,變量值從lua腳本里運算由return返回,能夠實現複雜的賦值邏輯;此處是阻塞的,Lua代碼要作到很是快.
另外能夠將已有的ngx變量看成參數傳進Lua腳本里去,由ngx.arg[1],ngx.arg[2]等方式訪問。
做用域:main, server, location, server if, location if
處理階段:rewrite
content_by_lua '<lua script>';
content_by_lua_file luafile;
做用域:location, location if
說明:內容處理器,接收請求處理並輸出響應,content_by_lua直接在nginx配置文件裏編寫較短Lua代碼後者使用lua文件。
rewrite_by_lua '<lua script>'
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
php
說明:每一個Nginx Worker進程啓動時調用的計時器,若是Master進程不容許則只會在init_by_lua以後調用;一般用於定時拉取配置/數據,或者後端服務的健康檢查。java
######################nginx
方法和常量
######################
正則表達式
[plain] view plaincopy後端
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 #不懂