Nginx集成 lua-nginx-module

商棋
2023-12-01

1.软件版本

  • 系统版本:CentOS Linux release 7.3.1611 (Core)
  • nginx版本:nginx-1.11.10
  • nginx-lua-moudle版本:lua-nginx-module 0.10.7

2.环境准备

  • 安装编译所需的依赖
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

3.软件安装

  • 编译安装nginx
wget https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.10.7
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
wget https://nginx.org/download/nginx-1.11.10.tar.gz
wget https://codeload.github.com/cfsego/ngx_log_if/zip/master -O ngx_log_if-master.zip

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/
mv v0.10.7 lua-nginx-module-0.10.7.tar.gz
tar -zxvf ./nginx-1.11.10.tar.gz

tar -zxvf ./lua-nginx-module-0.10.7.tar.gz

tar -zxvf ./LuaJIT-2.0.4.tar.gz

unzip ngx_log_if-master.zip -d /usr/local/nginx/

cd LuaJIT-2.0.4/

make && make install

cat >> /etc/profile <<EOF export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH EOF

source /etc/profile

cd ../nginx-1.11.10/

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../lua-nginx-module-0.10.7 --add-module=../ngx_log_if-master

make -j2

make install
  • 测试nginx配置是否成功
cd ../sbin

./nginx -t
  • 提示如下代表成功:
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  • 安装dkjson.lua插件

cd /usr/local/share/lua/5.1

wget http://dkolf.de/src/dkjson-lua.fsl/raw/dkjson.lua?name=16cbc26080996d9da827df42cb0844a25518eeb3 -O dkjson.lua

cd /usr/local/nginx

touch dkjson.lua
vim dkjson.lua
  • dkjson.lua 文件内容如下
local s = ngx.var.request_body

local dkjson = require("dkjson")

local obj = { code = 0, message = "ok" }

local str = dkjson.encode(obj, {indent = true})

ngx.say(str)
 类似资料: