您的位置:首页 > 运维架构 > Nginx

Nginx添加lua模块的实现方法

2020-03-20 12:04 549 查看

安装 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!

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Nginx lua模块