主要参考两篇文章
主要问题还是依赖软件和依赖库导致。
基本
系统:腾讯云centos7
源码目录:/usr/local/src/
默认安装目录:/usr/local/
日志目录:/usr/local/nginx/log/access.log
加载指定目录配置: include /etc/bbw_nginx/*.conf;
安装依赖
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
安装
cd /usr/local/src
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.5.tar.gz
tar zxf lua-nginx-module-0.10.7.tar.gz
tar zxf LuaJIT-2.0.4.tar.gz
tar zxf nginx-1.11.5.tar.gz
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.5/
# 配置需要的模块
./configure --prefix=/usr/local/nginx --add-module=../lua-nginx-module-0.10.7
make -j2
make install
测试安装:
配置1
log_format mylog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'request: "$request_body"' 'response: $resp_body';
配置2
#记录nginx请求返回值
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
access_log logs/host.access.log mylog;
配置3
location /helloTest {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
验证:curl localhost/helloTest?name=weijia
[root@VM_226_29_centos sbin]# cat /usr/local/nginx-1.4.2/logs/host.access.log
response_body:hello, lua\x0A
127.0.0.1 | - | hello, lua\x0A
127.0.0.1 - - [02/Nov/2019:16:00:36 +0800] "GET /helloTest?name=weijia HTTP/1.1" 200 21 "-" "curl/7.29.0" "-"request: "-"response: hello, lua\x0A
整体文件配置位置
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#配置1
log_format mylog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'request: "$request_body"' 'response: $resp_body';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#配置2
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
access_log logs/host.access.log mylog;
location / {
root html;
index index.html index.htm;
}
#配置3
location /helloTest {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
查看nginx配置:
[root@VM_226_29_centos sbin]# ./nginx -V
nginx version: nginx/1.4.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx-1.4.2 --add-module=../lua-nginx-module-0.8.6
问题
1、启动时会nginx可能会报错
./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: N
找不到libluajit-5.1.so.2这个文件
解决办法
1.找到 libluajit-5.1.so.2,libluajit-5.1.so.2.0.2这两个文件复制到 对应的lib下
64位是 /usr/lib64
32位是 /usr/lib
问题2
access.log日志中文乱码
nginx升级版本到1.12.1;lua模块升级v0.10.9rc7.tar.gz;增加参数escape=json
log_format main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'request: "$request_body"' 'response: $resp_body';