nginx添加lua支持
前言
淘宝的agentzh和chaoslawful开发的ngx_lua模块通过将lua解释器集成进Nginx。能够採用lua脚本实现业务逻辑,因为lua的紧凑、高速以及内建协程,所以在保证高并发服务能力的同一时候极大地减少了业务逻辑实现成本。
3d 男人看的缺失的拼图
系统依赖包
Server Racks In Data Center.
yum -y install pcre-devel openssl-devel gcc curl zlib-devel gcc-c++
组件介绍
LuaJIT:lua的一个即时编译器
Nginx Devel Kit:NDK是一个nginx模块,以一种可作用于其他nginx模块的方式,扩展nginx核心功能。
Lua Nginx Module:该模块将Lua解释器或LuaJIT嵌入到nginx核心中,并通过nginx子请求将强大的Lua线程(也称为Lua协程)集成到nginx事件模型中。
nginx:web服务
Server Racks In Data Center.
下载编译安装
wget -c http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar zxvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make install PREFIX=/opt/luajit
export LUAJIT_LIB=/opt/luajit/lib
export LUAJIT_INC=/opt/luajit/include/luajit-2.0
( 如果不设置环境变量,在nginx编译的时候会找不到lua模块:./configure: error: ngx_http_lua_module requires the Lua library.)
cd ..
wget -c -O ngx_devel_kit-0.3.0.tar.gz https://codeload.github.com/simplresty/ngx_devel_kit/tar.gz/v0.3.0
tar zxvf ngx_devel_kit-0.3.0.tar.gz
wget -c -O lua-nginx-module-0.10.13.tar.gz https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.10.13
tar zxvf lua-nginx-module-0.10.13.tar.gz
wget -c http://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
wget -c https://www.openssl.org/source/openssl-1.0.2p.tar.gz
tar zxvf openssl-1.0.2p.tar.gz
wget -c https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar zxvf pcre-8.42.tar.gz
wget -c http://nginx.org/download/nginx-1.12.2.tar.gz
tar zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/opt/nginx --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.13 --with-openssl=../openssl-1.0.2p --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.42 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module
make
make install
软连接或者添加环境变量
nginx报错: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
可以执行:
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
或者添加环境变量:
echo 'export LD_LIBRARY_PATH=/opt/luajit/lib:$LD_LIBRARY_PATH' >> /etc/profile
source /etc/profie
验证
vim /opt/nginx/conf/nginx.conf
#在nginx.conf中,server->location配置
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
Server Racks In Data Center.
启动nginx
/opt/nginx/sbin/nginx
访问
http://[服务器IP]/hello
可以看到
hello, lua