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

查看原始編譯

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 並覆蓋。

在編譯目錄執行

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!

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


免責聲明!

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



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