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 ...
随机推荐
- 建立一个可以不停地接收客户端新的连接,但不能处理复杂的业务的C/S网络程序
在Windows平台上主要有两个版本的Socket Api函数:WinSock 1.1和WinSock 2.2 , 2.2版本默认兼容1.1版本,1.1 winsock.h wsock32.lib w ...
- [ZOJ 3610] Yet Another Story of Rock-paper-scissors
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4713 题目很水,但是注意,越是简单的题目越应该关注细节,比如说输出上 ...
- Hive(七)Hive参数操作和运行方式
Hive参数操作和运行方式 1.Hive参数操作 1.hive参数介绍 hive当中的参数.变量都是以命名空间开头的,详情如下表所示: 命名空间 读写权限 含义 hiveconf 可读写 hive ...
- 【经验分享】-PHP程序员的技能图谱
一.技术知识积累作为参与工作一定年限的程序员,最重要的就是静下心来把遇到的和遗漏的知识点记录下来,做好学习和总结的准备.学习方面,除了看书上网查资料之外,实践也是非常重要的一点,很多不懂的或者不明白的 ...
- 2019.9.29 FlutterToast使用
引入 fluttertoast: ^ 增加头文件 import 'package:fluttertoast/fluttertoast.dart'; 样式 1 Fluttertoast.showToas ...
- linux 远程配置docker加速器
https://www.jianshu.com/p/dca49964af04 curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh ...
- Pandas中DataFrame数据合并、连接(concat、merge、join)之concat
一.concat:沿着一条轴,将多个对象堆叠到一起 concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, key ...
- AngularJs-变量
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 如何开始使用Laravel
访问http://www.golaravel.com/download/,下载v5.2.15 解压 复制到项目目录 3.然后访问 http://localhost/LaravelTest/server ...
- vue中点击按钮自动截图并下载图片
点击一个按钮,截取对应区域的界面,才对截取的界面进行裁切并下载 下载 html2canvas npm install html2canvas --save 引用 : import htm ...