NGINX通过LUA实现LOG记录 POST数据Body(UBU
2020-04-13 本文已影响0人
Zokoo
参考链接及遇到问题
参考一
链接:https://blog.csdn.net/kissxia/article/details/78063025
按步骤安装好后,会出现如下报错:
nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: module 'resty.core' not found:
no field package.preload['resty.core']
no file './resty/core.lua'
no file '/usr/local/share/luajit-2.0.5/resty/core.lua'
no file '/usr/local/share/lua/5.1/resty/core.lua'
no file '/usr/local/share/lua/5.1/resty/core/init.lua'
no file './resty/core.so'
no file '/usr/local/lib/lua/5.1/resty/core.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/nginx/conf/nginx.conf:139
然后找到解决问题的一个方案,如下
参考二
链接:https://blog.csdn.net/qq_27156945/article/details/104019069
把LuaJIT换成github中openresty的,解决掉Warning, 按照步骤重新编译。还是出现如下问题,加入“lua_load_resty_core off;”无帮助。
nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: module 'resty.core' not found:
no field package.preload['resty.core']
no file './resty/core.lua'
no file '/usr/local/luajit/share/luajit-2.1.0-beta3/resty/core.lua'
no file '/usr/local/share/lua/5.1/resty/core.lua'
no file '/usr/local/share/lua/5.1/resty/core/init.lua'
no file '/usr/local/luajit/share/lua/5.1/resty/core.lua'
no file '/usr/local/luajit/share/lua/5.1/resty/core/init.lua'
no file './resty/core.so'
no file '/usr/local/lib/lua/5.1/resty/core.so'
no file '/usr/local/luajit/lib/lua/5.1/resty/core.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/luajit/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/nginx/conf/nginx.conf:139
问题解决
根据提示,从github中下载对应模块。
解决NO resty.core问题
下载resty.core最新版本:
git clone https://github.com/openresty/lua-resty-core.git
由于在ngnix conf文件中指定的地址是:
lua_package_path "/usr/local/nginx/lua-resty-core/lib/?.lua;;";
所以把文件拷贝到目录:/usr/local/nginx,但出现新问题,找不到resty.lrucache,如下:
nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: /usr/local/nginx/lua-resty-core/lib/resty/core/regex.lua:14: module 'resty.lrucache' not found:
no field package.preload['resty.lrucache']
no file '/usr/local/nginx/lua-resty-core/lib/resty/lrucache.lua'
no file './resty/lrucache.lua'
no file '/usr/local/luajit/share/luajit-2.1.0-beta3/resty/lrucache.lua'
no file '/usr/local/share/lua/5.1/resty/lrucache.lua'
no file '/usr/local/share/lua/5.1/resty/lrucache/init.lua'
no file '/usr/local/luajit/share/lua/5.1/resty/lrucache.lua'
no file '/usr/local/luajit/share/lua/5.1/resty/lrucache/init.lua'
no file './resty/lrucache.so'
no file '/usr/local/lib/lua/5.1/resty/lrucache.so'
no file '/usr/local/luajit/lib/lua/5.1/resty/lrucache.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/luajit/lib/lua/ in /usr/local/nginx/conf/nginx.conf:146
解决NO resty.lrucache的问题
按提示从github中下载resty.lrucache:
git clone https://github.com/openresty/lua-resty-lrucache.git
下载完后,需要添加到目录: '/usr/local/nginx/lua-resty-core':
sudo cp -ru lua-resty-lrucache/* /usr/local/nginx/lua-resty-core/
重新启动nginx,自此问题得到了解决。
最终的conf的http部分配置如下:
http {
include mime.types;
default_type application/octet-stream;
#一次POST Body最大值
client_max_body_size 100M;
#可以放在在$request_body中的最大值。超过这个值,$request_body会是空,log就无法记录了
client_body_buffer_size 10M;
lua_package_path "/usr/local/nginx/lua-resty-core/lib/?.lua;;";
init_by_lua_block {
require "resty.core"
collectgarbage("collect") -- just to collect any garbage
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent $request_body "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
lua_need_request_body on;
server {
listen 80;
server_name localhost;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /testlog {
default_type 'text/html';
content_by_lua '
ngx.say("<html><body>Hello, LUA!!</body></html>")
ngx.req.read_body()
';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
测试效果
root@test:# curl -d "record log" localhost/testlog
<html><body>Hello, LUA!!</body></html>
root@test:# tail /usr/local/nginx/logs/host.access.log
127.0.0.1 - - [13/Apr/2020:00:18:42 +0800] "POST /testlog HTTP/1.1" 200 50 record log "-" "curl/7.58.0" "-"
root@test:#
可以看到用curl POST 字符串:record log已经按照预定格式记录在log的最后。