1. 文档

在nginx中想利用$request_body命令获取post请求的body参数,并落日志,但是发现该变量值为空,查看官网中对$request_body的描述如下:

$request_body
    request body

The variable’s value is made available in locations processed by the proxy_pass, fastcgi_pass, uwsgi_pass, and scgi_pass directives when the request body was read to a memory buffer.

意思是只有location中用到proxy_pass,fastcgi_pass,scgi_pass命令时,该变量才有值。

2.使用proxy_pass,fastcgi_pass, scgi_pass等命令获取$request_body值

试了下用proxy_pass,的确可以。配置如下:

log_format main_post '$remote_addr\t$remote_user\t[$time_local]\t"$request"\t$status\t$bytes_sent\t'
'"$http_referer"\t"$http_user_agent"\t"$http_x_forwarded_for"\t"$request_body"';
worker_processes  ;        #nginx worker 数量
error_log logs/error.log; #指定错误日志文件路径
events {
worker_connections ;
} http {
log_format dm ' "$request_body" '; upstream bk_servers_2 {
server 127.0.0.1:;
} server {
listen ;
location /post/ {
proxy_pass http://bk_servers_2/api/log/letv/env;
access_log /home/shuhao/openresty-test/logs/post.log dm;
}
location /api/log/letv/env {
return ;
}
}
}

使用curl命令模拟post请求

curl -i  -d "arg1=1&arg2=2" "http://127.0.0.1:6699/post/"

日志用打印出结果:

 "arg1=1&arg2=2" 

3.使用lua获取$request_body值

条件:使用openresty或者nginx编译了lua模块。

方法:

server中使用lua_need_request_body on; 或者在location lua代码块中使用 ngx.req.read_body()

注意:

1)lua代码块中必须有执行语句,否则lua不执行,无法获取request_body;

2)不要使用return 200;等命令,有return命令,lua代码不执行。

worker_processes  ;        #nginx worker 数量
error_log ~/openresty-test/logs/error.log debug; #指定错误日志文件路径
events {
worker_connections ;
} http {
log_format dm '"$request_body"';
lua_need_request_body on;
server {
listen ;
location /post/ {
content_by_lua '
ngx.say("-------")
ngx.req.read_body()
';
access_log ~/openresty-test/logs/post.log dm;
#return ;
}
}
}

4. 自定义变量存放request body

方法:

1)在server 块中使用set $resp_body ""; 声明变量;

2)在location使用  ngx.var.resp_body = ngx.req.get_body_data() or "-"    为变量赋值

worker_processes  ;        #nginx worker 数量
error_log /home/shuhao/openresty-test/logs/error.log debug; #指定错误日志文件路径
events {
worker_connections ;
} http {
log_format dm ' "$request_body" -- "$resp_body"';
lua_need_request_body on;
server {
listen ;
set $resp_body "";
location /post/ {
lua_need_request_body on;
content_by_lua '
local resp_body = ngx.req.get_body_data() or "-"
ngx.var.resp_body = resp_body
';
access_log /home/shuhao/openresty-test/logs/post.log dm;
#return ;
}
}
}

效果:

(完)

nginx记录post body/payload数据的更多相关文章

  1. nginx记录响应与POST请求日志

    生产环境中的某些api出现故障,但是问题无法重现,但是又很想解决掉问题以及我们新项目上线,需要跟踪请求与响应的信息,可以预先找到一些bug,减少大面积的损失. 安装nginx与ngx_lua 响应日志 ...

  2. thinkphp5 数据库查询之paginate: 同时获取记录总数和分页数据

    thinkphp5中要想同时获得查询记录的总数量以及分页的数据, 可以用paginate(), 真的非常方便! 表结构: CREATE TABLE `t_users` ( `id` int(11) u ...

  3. [日常] nginx记录post数据

    1.使用log_format指令来更改日志格式,该指令只能放在http{}段 log_format  日志名  '日志内容'; server { access_log /var/log/nginx/d ...

  4. nginx记录post数据日志

    1.vi nginx.conf 找到http {}中log_foramt ,定义post 日志格式 #log_format main '$remote_addr - $remote_user [$ti ...

  5. Flunetd 用于统一日志记录层的开源数据收集器

    传统的日志查看方式 使用fluentd之后 一.介绍 Fluentd是一个开源的数据收集器,可以统一对数据收集和消费,以便更好地使用和理解数据. 几大特色: 使用JSON统一记录 简单灵活可插拔架构 ...

  6. Nginx记录-nginx 负载均衡5种配置方式(转载)

    nginx 负载均衡5种配置方式 1.轮询(默认)   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.  2.weight 指定轮询几率,weight和访问比率成 ...

  7. Nginx记录-Nginx基础(转载)

    1.Nginx常用功能 1.Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理. Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能.Nginx可以根据不同的 ...

  8. Nginx记录用户请求Header到access log

    为了统计和其它用途,经常有人需要自定义Nginx日志,把http请求中的某个字段记录到日志中,刚好在看lua+nginx的文章,第一想到的是用lua赋值来做,但是想想有点小恶心,于是Google了一番 ...

  9. ViewPager使用记录1——展示固定数据

    ViewPager是v4支持库中的一个控件,相信几乎所有接触Android开发的人都对它不陌生.之所以还要在这里翻旧账,是因为我在最近的项目中有多个需求用到了它,觉得自己对它的认识不够深刻.我计划从最 ...

随机推荐

  1. JS处理数据四舍五入

    一,使用Math.toFixed toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法NumberObject.toFixed(num) 但是网友说toFixed bug比 ...

  2. linux关闭防火墙及开放端口

    1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...

  3. MVC异步AJAX的三种方法(JQuery的Get方法、JQuery的Post方法和微软自带的异步方法)

    异步是我们在网站开发过程中必不可少的方法,MVC框架的异步方法也有很多,这里介绍三种方法: 一.JQuery的Get方法 view @{ Layout = null; } <!DOCTYPE h ...

  4. springboot 02-PropertiesFile 自定义配置属性,多环境配置

    application.properties: # 自定义配置 test.hello.world = HelloWorld test.person.name = 哈哈 test.person.sex ...

  5. MySQL api

    今天看去年年中写的代码,留意到一个关键时刻能提高效率的api:on duplicate key update: 语法: INSERT INTO INSERT INTO g_iot_user_build ...

  6. LinkedQueue的底层实现

    package zy813ture; public class MyLinkedQueue1 { private Node head; private Node last = head = new N ...

  7. 2017CCPC秦皇岛 M题Safest Buildings&&ZOJ3993【复杂模拟】

    题意: 给出两个半径R,r,R表示第一次的大圈半径,r表示第二次的小圈半径.第一次大圈的圆心位于(0,0),第二次小圈的圆心未知,但在大圈内,给你一个n,然后给出n个屋子的位置,问这些屋子中,第二次在 ...

  8. 第28月第23天 lineFragmentPadding

    1.lineFragmentPadding https://blog.csdn.net/lwb102063/article/details/78748186

  9. Mybatis--课程中循序渐进的体会 《黑马程序员_Springmvc+Mybatis由浅入深全套视频教程video》

    mybatis 刚开始是用最原始的方法开发了一个入门程序,(没有dao) 1.将数据源配置在SqlMapConfig.xml 2.写一个po类 和映射文件User.xml(中的namespace=“名 ...

  10. windows10+ubuntu16.04双系统搭建

    0 - 环境 操作系统: Windows 10 + Ubuntu 16.04 显卡: GTX 950M Cuda 9.0 cudnn v7.0 tensorflow-gpu==1.7.0 1 - ub ...