ngx lua模組學習筆記

2021-07-04 16:59:32 字數 1249 閱讀 8555

- lua_code_cache

1. 語法: lua_code_cache on | off; (預設為on)

2. 作用:是否開啟lua**快取。

3. 注意:關閉(off)時,不用重啟ngx服務,方便測試。生產環境,千萬不要關閉該開關。

- lua_transform_underscores_in_response_headers

1. 語法: lua_transform_underscores_in_response_headers on|off; (預設為on)

2. 作用:(下劃線_是否等同短橫線-)

注:http頭部名稱大小寫不敏感,該開關僅對_和-線進行是否區分處理。

(1)當為預設設定on時:

ngx.header.content_type = 'text/plain' 等同於: ngx.header["content-type"] = 'text/plain'

(2)當設定為off時,(1)中的header就不等同。

- ngx.redirect

1. 語法:ngx.redirect(uri, status = 302)

2. 作用:臨時(302)或永久重定向到uri, 響應頭location的值為該uri

3. 示例:

3.1 return ngx.redirect("/foo") 臨時重定向到「/foo」,等效的rewrite指令配置:

rewrite ^ /foo? redirect;

3.2 return ngx.redirect("") 臨時重定向到外部uri

3.3 return ngx.redirect("/foo", 301) 永久重定向到"/foo",等效的rewrite指令配置:

rewrite ^ /foo? permanent;

3.4 return ngx.redirect("/foo?a=3&b=4")

- ngx.escape_uri

1. 語法:newstr = ngx.escape_uri(str)

2. 作用:以urlencode編碼

3. 示例:

- ngx.unescape_uri

1. 語法:newstr = ngx.unescape_uri(str)

2. 作用: 以urldecode解碼

3. 示例:

3.1 newstr = ngx.unescape_uri("b%20r56+7") 得到:b r56 7

ngx Lua模組中的重定向

在nginx中實現重定向可以通過rewrite指令,具體可參考 nginx學習 http rewrite module的rewrite指令 通過lua模組也可以實現同樣的功能,lua模組提供了相關的api來實現重定向的功能,主要有 ngx.exec 語法 ngx.exec uri,args?主要實現...

ngx Lua模組中的加密api介面

在lua模組中提供了多種用於是實現各種字串加密演算法的api介面,主要包括 ngx.crc32 short 語法 digest ngx.crc32 short str 該方法主要是計算給定字串str的迴圈校驗碼 cyclic redundancy code 的摘要,計算出來的結果是乙個很大的整數 n...

ngx lua模組中的共享記憶體字典項API

在ngx lua模組中使用共享記憶體字典項相關api的前提條件是已經使用lua shared dict命令定義了乙個字典項物件,該命令的具體用法為 語法 lua shared dict 該命令主要是定義一塊名為name的共享記憶體空間,記憶體大小為size。通過該命令定義的共享記憶體物件對於ngin...