Lua是一个小巧的脚本语言, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能
一、安装lua解释器 LuaJIT
1、安装LuaJIT
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar xf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5/
make install PREFIX=/usr/local/luajit
2、导出LuaJIT的库文件
vim /etc/ld.so.conf.d/luajit.conf
/usr/local/luajit/lib
ldconfig
<br /> 3、配置luajit的环境变量
vim /etc/profile
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
source /etc/profile
<br /> 4、测试解释器
lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
>
> print("hello lua")
hello lua
<br /> <br />
二、安装nginx_lua三方模块
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.10.tar.gz
1、 解压缩模块
tar xf v0.3.0.tar.gz
tar xf v0.10.10.tar.gz
tar xf nginx-1.14.0.tar.gz
2、为nginx添加三方模块
cd nginx-1.14.0/
yum -y install pcre-devel
yum -y install openssl openssl-devel
./configure --prefix=/app/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-http_secure_link_module --with-threads --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.10
make && make install
3、验证模块
cd /app/nginx/sbin/
./nginx -V
4、创建虚拟主机测试lua模块
vim /app/nginx/conf/lua_test.conf
server {
listen 80;
server_name localhost;
location /test1 {
content_by_lua '
ngx.say("hello lua")
ngx.log(ngx.ERR, "err err")';
}
location /test2 {
content_by_lua_file "/tmp/test.lua";
}
}
实际的配置片段
log_format main '{"request":"$request","request_body":"$request_body","time":"[$time_iso8601]","remote_addr":"$http_x_forwarded_for","remote_user":"$remote_user","response_time":"$upstream_response_time","request_time":"$request_time","browser_type":"$http_user_agent","response_body":"$resp_body"}';
upstream H5_server {
server 127.0.0.1:9500 weight=100;
}
server {
listen 8099;
server_name localhost;
root /usr/share/nginx/html;
set $resp_body "";
access_log /app/nginx/logs/access.log main buffer=1024000k flush=1m;
location /face/video/liveness_detection {
lua_need_request_body on;
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 300000)
ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
5、启动nginx
./nginx
6、测试访问
vim /tmp/test.lua
ngx.say("hello lua_xjs")
curl lua.linux.com/test1
hello lua
curl lua.linux.com/test2
hello lua_xjs