安装 Lua 和 Lua for nginx

#安装lua
yum install -y lua
lua -v

安装更高效的 Lua 语言解释器 LuaJIT

wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -zxvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make install PREFIX=/usr/local/LuaJIT/2.0.2
export LUAJIT_LIB=/usr/local/LuaJIT/2.0.2/lib
export LUAJIT_INC=/usr/local/LuaJIT/2.0.2/include/luajit-2.0
echo "/usr/local/LuaJIT/2.0.2/lib" >> /etc/ld.so.conf
ldconfig

安装 ngx_devel_kit 和 lua-nginx-module

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

#先make clean,然后在原有的nginx ./configure基础上,添加新的模块
#如果出现undefined reference to `X509_check_host'错误,需要添加--with-openssl选项,指向openssl 1.0.2安装包源码所在的目录。
./configure --add-module=/path/to/ngx_devel_kit-0.3.0 --add-module=/path/to/lua-nginx-module-0.10.9rc7
--with-openssl=/path/to/openssl-1.0.2k
make && make install

使用nginx日志输出 request 或 response 数据

Nginx调用lua

set_by_lua, set_by_lua_file设置Nginx变量
access_by_llua, access_by_lua_file访问控制
content_by_lua, content_by_lua_file接收请求,输出响应

Lua调用Nginx

ngx.varnginx变量
ngx.req.get_headers获取请求头
ngx.req.get_uri_args获取请求参数
ngx.redirect重定向
ngx.print输出内容
ngx.say同ngx.print,会多一个换行符
ngx.header输出响应头
......

专题阅读

03-05 21:28