Nginx 日志打印POST数据
在工作中,开发希望能从Nginx日志中获取POST的数据信息,先记录下来
在日志格式后面加上 $request_body 配置信息 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_body'; 在server中添加打印日志的操作
access_log logs/access.log main;
本以为问题解决了,开发有要求在日志中添加上 服务器响应返回的数据
目前的 nginx 是不支持输出 response 报文体的 使用body_filter_by_lua
来分配请求报文体给一个nginx
变量。下面是一个示例
1:下载安装LuaJIT
# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
# tar -xzvf LuaJIT-2.0.2.tar.gz
# cd LuaJIT-2.0.2
# make 出现如下内容表示编译成功
OK Successfully built LuaJIT
make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.2/src'
==== Successfully built LuaJIT 2.0.2 ==== # make install
出现如下内容,表示安装成功
==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2:下载准备nginx lua模块
wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz
tar -xzvf lua-nginx-module-0.8.6.tar.gz
mv lua-nginx-module-0.8.6 /usr/local/src/lua-nginx-module-0.8.6
3:安装nginx
tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1
//先导入环境变量,告诉nginx去哪里找luajit
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0 ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/lua-nginx-module-0.8.6 --with-http_ssl_module --with-http_stub_status_module --with-pcre make j2
make install
常见错误处理:
/usr/local/nginx-1.4.2/sbin/nginx -v
./objs/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
解决方法:
# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
4:配置Nginx
nginx配置文件加入如下配置: location /test {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, ttlsa lua")';
}
5:访问测试
curl http://127.0.0.1/test
hello, ttlsa lua //使用curl测试,返回数据表示安装成功
6:Nginx日志配置
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_body resp_body:"$resp_body"';
在server中添加相应的配置:
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
';
如下事例:
server {
listen 443 ssl;
server_name xxxx.com; # ssl on;
ssl_certificate /usr/local/nginx/conf/keys/public.pem;
ssl_certificate_key /usr/local/nginx/conf/keys/private.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!3DES:!aNULL:!eNULL;
ssl_prefer_server_ciphers on; 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
'; location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Referer $http_referer;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_buffer_size 512k;
proxy_buffers 16 512k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
}
access_log /tmp/faceauth.log main; }
启动Nginx,打印日志,就可以看到相关数据。
以上,是打印POST数据的一种方法,比较麻烦,下面介绍一种比较简单的方法:
在日志中添加“$request_body”,由于现实原因,需要添加 escape=json 转换为json格式
log_format access escape=json '$remote_addr - $remote_user [$time_local] "$request" - "$request_body" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$upstream_addr" "$upstream_status" "$request_time" "$upstream_response_time" $bytes_sent $request_length';
Nginx 日志打印POST数据的更多相关文章
- 利用 ELK系统分析Nginx日志并对数据进行可视化展示
一.写在前面 结合之前写的一篇文章:Centos7 之安装Logstash ELK stack 日志管理系统,上篇文章主要讲了监控软件的作用以及部署方法.而这篇文章介绍的是单独监控nginx 日志分析 ...
- ELK系统分析Nginx日志并对数据进行可视化展示
结合之前写的一篇文章:ELK日志分析平台搭建全过程,上篇文章主要讲了部署方法.而这篇文章介绍的是单独监控nginx 日志分析再进行可视化图形展示. 本文环境与上一篇环境一样,前提 elasticsea ...
- Nginx 日志记录post数据,并使用goaccess进行日志分析
nginx日志默认不会记录post数据 在nginx配置文件的http节 log_format 日志格式标识 [escape=json] 日志格式 比如:日志格式标识设置为main,添加escape= ...
- nginx 日志打印响应时间 request_time 和 upstream_response_time
设置log_format,添加request_time,$upstream_response_time,位置随意 og_format main '"$request_time" ...
- nginx日志打印请求响应时间
log_format timed_combined '$remote_addr - $remote_user [$time_local] "$request" ' '$stat ...
- nginx 日志打印post请求参数
在日志格式后面加上 $request_body 配置信息 log_format main '$remote_addr - $remote_user [$time_local] "$reque ...
- Centos7 搭建 Flume 采集 Nginx 日志
版本信息 CentOS: Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x ...
- nginx1.14.0日志打印
nginx日志打印 http属性log_format来设置日志格式 ,参考 https://www.jb51.net/article/52573.htm <nginx日志配置指令详解> ...
- ELK+Redis+Nginx服务数据存储以及Nginx日志的收集
PS:此片文章是承接上篇ELK部署文档,再次便不详细说明了 [安装Redis] [root@Redis ~]# wget http://download.redis.io/releases/redi ...
随机推荐
- Select,poll,epoll复用
Select,poll,epoll复用 1)select模块以列表的形式接受四个参数,分别是可读对象,可写对象,产生异常的对象,和超时设置.当监控符对象发生变化时,select会返回发生变化的对象列表 ...
- java8学习之Lambda表达式深入与流初步
Lambda表达式深入: 在上一次[http://www.cnblogs.com/webor2006/p/8135873.html]中介绍Lambda表达式的作用时,其中说到这点: 如标红处所说,既然 ...
- PHP程序员的技能图谱
PHP知识图谱
- kettle案例实现
案例一.把stu1的数据按id同步到stu2,stu2有相同id则更新数据 在kettle中新建转换 点击左上角文件—新建—转换到核心对象界面,点击输入,找到表输入拖拽到中间 双击表输入,在数据库连接 ...
- 题解 【SDOI2009】HH的项链
题面 解析 这题本来莫队可以过的. 然而,对于某些加强的数据,莫队就得吸氧了.. 所以,本题解还将介绍另一种算法——树状数组. 首先,莫队就不用讲了吧(毕竟只是板子). 那么,开始进入正题(似乎有点啰 ...
- What is Double 11 in China? Is it a famous festival?
"1" means single, 11th, November is quadruple single!! What a tragedy for those single you ...
- php文件夹上传源码
1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...
- easyUI datagrid中checkbox选中事件以及行点击事件,翻页之后还可以选中
DataGrid其中与选择,勾选相关 DataGrid属性:singleSelect boolean 如果为true,则只允许选择一行. false ctrlSelect boolean 在启用多行选 ...
- sh_16_字符串判断方法
sh_16_字符串判断方法 # 1. 判断空白字符 space_str = " \t\n\r" print(space_str.isspace()) # 2. 判断字符串中是否只包 ...
- Laydate 使用注意事项
1.laydate 切记不能放在laytpl 模板语法中使用,否则可能会导致无法触发的情况 不在laytpl中使用 <div class="layui-form-item"& ...