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. bash test命令探秘

    shell 测试条件命令 http://blog.csdn.net/yangruibao/article/details/7427503 test 和 [ 命令 虽然 Linux 和 UNIX 的每个 ...

  2. jqGrid api 中文说明

    JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面是转自其他人b ...

  3. 二十七、Linux 进程与信号---进程组和组长进程

    27.1 进程组 27.1.1 进程组介绍 进程组为一个或多个进程的集合 进程组可以接受同一终端的各种信号,同一个信号发送进程组等于发送给组中的所有进程 每个进程组有唯一的进程组 ID 进程组的消亡要 ...

  4. 基于WebSocket 私聊、ws_session、httpsession

    [解码器跟编码器]为了可以直接sendObject 解码 => 解成计算机需要的码 => 将用户输入的文本或者二进制 序列化成消息对象.    (dll 给机器吃的) 编码 => 编 ...

  5. EF 事物Transaction简单操作

    /// <summary> /// 申请提现 /// </summary> /// <param name="userId">用户id</ ...

  6. webwork框架

    以前都没有用过WebWork这个框架,只是听说过.没想到现在要用,所以就自学了一下.做了个小例子给大家分享下中间遇到的苦难和经验. 准备工作:首先要去下载WebWork框架的开发包.我用的2.2.6版 ...

  7. 【mmall】Guava库学习Collections

    参考链接 Guava库学习:学习Collections(三)Sets

  8. eclipse使用异常An error has occurred.see error log for more details eclipse

    eclipse使用异常An error has occurred.see error log for more details eclipse 解决Eclipse,MyEclipse出现An erro ...

  9. pwnable.kr fb

    fb-1 pt 连接到服务器,发现 有三个文件,fd脚本,fd.c脚本的源程序,flag是要看的东西,无权限 来我们分析一下源码 如果只传进去一个值,print” pass argv[1] a num ...

  10. tomcat目录映射

    环境:CentOS 6 + tomcat 7 + jdk 7 目前使用2种方法: 1.tomcat/conf/server.xml <Host name="localhost" ...