Nginx 使用Lua腳本


安裝 lua

wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz

tar -zxvf LuaJIT-2.0.5. tar .gz
cd LuaJIT-2.0.5
make && make install PREFIX= /usr/local/LuaJIT
 
etc/profile 加入並使之生效
# lua
export LUAJIT_LIB= /usr/local/LuaJIT/lib
export LUAJIT_INC= /usr/local/LuaJIT/include/luajit-2 .0
執行
#source etc/profile
 

下載 ngx_devel_kit 模塊

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

 

NDK (nginx development kit) 模塊是一個拓展 nginx 服務器核心功能的模塊,第三方模塊開發可以基於它來快速實現。 NDK 提供函數和宏處理一些基本任務, 減輕第三方模塊開發的代碼量

 

下載 lua-nginx-module 模塊

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz

 

lua-nginx-module 模塊使 nginx 中能直接運行 lua

 

查看原始編譯

/opt/nginx/sbin/nginx -V

如:
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-http_v2_module

進入 nginx 原始目錄:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-http_v2_module --add-module=/root/lua-nginx-module-0.10.9rc7/ --add-module=/root/ngx_devel_kit-0.3.0

 

只 make,不執行 make install。

編譯報錯應該就是 lua 環境變量不對。

nginx -V 命令報錯

./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
 
解決:
echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
 
ldconfig
 

成功之后可以 nginx -V 查看,無報錯即可。

把原來的 nginx 備份為 nginx_old

cp objs/nginx 到原來的 nginx 並覆蓋。

 

在編譯目錄執行 (在nginx是啟動狀態)

make upgrade

 

Nginx 添加 lua 模塊

測試:

server{

  ...
  location /lua {
   default_type 'text/html' ;
   content_by_lua '
     ngx.say( "hello, lua!" )
   ';
  }
  ...
}
 

瀏覽器打開:

http://blog.13sai.com/lua

可以看到 hello, lua!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

 

 

一些配置信息

lua常用系統變量

方法 類型 說明
ngx.var 請求 如果要賦值如 ngx.var.b = 2,此變量必須提前聲明;另外對於 nginx location 中使用 正則捕獲的捕獲組可以使用 ngx.var [捕獲組數字]獲取;
ngx.req.get_headers 請求 獲取請求頭,默認只獲取前100,如果想要獲取所以可以調用ngx.req.get_header s(0);獲取帶中划線的請求頭時請使用如 headers.user_agent 這種方式;如果一個請求頭有多個值,則返回的 是 table;
ngx.req.get_uri_args 請求 獲取 url 請求參數,其用法和 get_headers 類似;
ngx.req.get_post_args 請求 獲取 post 請求內容體,其用法和 get_headers 類似,但是必須提前調用 ngx.req.r ead_body() 來讀取 body 體(也可以選擇在 nginx 配置文件使用lua_need_request_body on;開啟讀取 bod y 體,但是官方不推薦);
ngx.req.raw_header 請求 未解析的請求頭字符串;
ngx.req.get_body_data 請求 為解析的請求 body 體內容字符串。
ngx.req.get_method 請求 獲取請求的大寫字母形式的請求方式
ngx.header 響應 通過ngx.header.header_name的形式獲取或設置響應頭信息。
ngx.exit 響應 以某個狀態碼返回響應內容
ngx.redirect 響應 重定向當前請求到新的 url
ngx.log 其他 輸出到log日志
ngx.re.match 其他 正則匹配
ngx.md5 其他 md5編碼
ngx.encode_base64 其他 base64解碼
ngx.decode_base64 其他 base64編碼

模塊指令

每個模塊都有*_lua(指令)、*_lua_block(代碼塊)、*_lua_file(腳本文件)

指令 所在階段 使用范圍 說明
init_by_lua 加載配置文件 http 可以用於初始化全局配置
set_by_lua rewrite server location location if 復雜邏輯的變量賦值,注意是阻塞的
rewrite_by_lua rewrite http server location location if 實現復雜邏輯的轉發或重定向
content_by_lua content location location if 處理請求並輸出響應
header_filter_by_lua 響應頭信息過濾 http server location location if 設置響應頭信息
body_filter_by_lua 輸出過濾 http server location location if 對輸出進行過濾或修改

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM