一、为什么需要安装Lua?
尽管Nginx官方自带的核心模块外加第三方的模块能够满足我们大部分的业务需要,但是业务需求、业务场景变化需要添加些额外的功能,自己去开发一个nginx模块相对来说比较笨重,我们可以使用lua脚本直接内嵌到nginx当中实现一些业务逻辑,完成一些特殊的功能需求。
二、安装lua模块
1、编译安装
下载地址 https://github.com/openresty/luajit2 当前下载的是v2.1版本
git clone https://github.com/openresty/luajit2.git
tar -zxvf v2.1-20200102.tar.gz
cd luajit2-2.1-20200102
make install PREFIX=/usr/local
2、添加环境变量
vim /etc/profile
#最后两行添加如下内容:
export LUAJIT_LIB=/usr/local/luajit2/lib
export LUAJIT_INC=/usr/local/luajit2/include/luajit-2.1
未添加环境变量将会报错:
./configure: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as /usr/local/luajit2/lib and LUAJIT_INC (path for lua.h) /usr/local/luajit2/include/luajit-2.1, but we cannot find LuaJIT there.
3、下载ngx_devel_kit模块
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
NDK(nginx development kit)模块是一个拓展nginx服务器核心功能的模块,第三方模块开发可以基于它来快速实现。 NDK提供函数和宏处理一些基本任务,减轻第三方模块开发的代码量。
4、下载lua-nginx-module模块
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
lua-nginx-module模块使nginx中能直接运行lua脚本
5、重新编译nginx源文件
注意:编译需要添加上之前编译的模块并且使用--add_module添加第三方模块;make之后不需要make install;
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_slice_module --with-http_stub_status_module --add-module=../ngx_cache_purge --add-module=/usr/local/src/nginx-upsync-module --add-module=/usr/local/src/ngx_devel_kit-0.3.1 --add-module=/usr/local/src/lua-nginx-module-0.10.14