Lua是一个小巧的脚本语言, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能

一、安装lua解释器 LuaJIT

  1. 1、安装LuaJIT
  1. wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
  2. tar xf LuaJIT-2.0.5.tar.gz
  3. cd LuaJIT-2.0.5/
  4. make install PREFIX=/usr/local/luajit
  1. 2、导出LuaJIT的库文件
  1. vim /etc/ld.so.conf.d/luajit.conf
  2. /usr/local/luajit/lib
  1. ldconfig
  1. <br /> 3、配置luajit的环境变量
  1. vim /etc/profile
  2. export LUAJIT_LIB=/usr/local/luajit/lib
  3. export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
  1. source /etc/profile
  1. <br /> 4、测试解释器
  1. lua
  2. Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
  3. >
  4. > print("hello lua")
  5. hello lua
  1. <br /> <br />

二、安装nginx_lua三方模块

  1. wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
  2. wget https://github.com/openresty/lua-nginx-module/archive/v0.10.10.tar.gz

1、 解压缩模块

  1. tar xf v0.3.0.tar.gz
  2. tar xf v0.10.10.tar.gz
  3. tar xf nginx-1.14.0.tar.gz

2、为nginx添加三方模块

  1. cd nginx-1.14.0/
  2. yum -y install pcre-devel
  3. yum -y install openssl openssl-devel
  4. ./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
  5. make && make install

3、验证模块

  1. cd /app/nginx/sbin/
  2. ./nginx -V

4、创建虚拟主机测试lua模块

  1. vim /app/nginx/conf/lua_test.conf
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. location /test1 {
  6. content_by_lua '
  7. ngx.say("hello lua")
  8. ngx.log(ngx.ERR, "err err")';
  9. }
  10. location /test2 {
  11. content_by_lua_file "/tmp/test.lua";
  12. }
  13. }

实际的配置片段

  1. 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"}';
  2. upstream H5_server {
  3. server 127.0.0.1:9500 weight=100;
  4. }
  5. server {
  6. listen 8099;
  7. server_name localhost;
  8. root /usr/share/nginx/html;
  9. set $resp_body "";
  10. access_log /app/nginx/logs/access.log main buffer=1024000k flush=1m;
  11. location /face/video/liveness_detection {
  12. lua_need_request_body on;
  13. body_filter_by_lua '
  14. local resp_body = string.sub(ngx.arg[1], 1, 300000)
  15. ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body
  16. if ngx.arg[2] then
  17. ngx.var.resp_body = ngx.ctx.buffered
  18. end
  19. ';

5、启动nginx

  1. ./nginx

6、测试访问

  1. vim /tmp/test.lua
  2. ngx.say("hello lua_xjs")
  3. curl lua.linux.com/test1
  4. hello lua
  5. curl lua.linux.com/test2
  6. hello lua_xjs