zoukankan      html  css  js  c++  java
  • nginx记录post body/payload数据

    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	$remote_user	[$time_local]	"$request"	$status	$bytes_sent	'
                    '"$http_referer"	"$http_user_agent"	"$http_x_forwarded_for"	"$request_body"';
    worker_processes  1;        #nginx worker 数量
    error_log logs/error.log;   #指定错误日志文件路径
    events {
        worker_connections 1024;
    }
     
    http {
        log_format  dm  ' "$request_body" ';
     
        upstream bk_servers_2 {
            server 127.0.0.1:6699;
        }
     
        server {
            listen 6699;
            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 202;
            }
        }
    }

    使用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  4;        #nginx worker 数量
    error_log ~/openresty-test/logs/error.log debug;   #指定错误日志文件路径
    events {
        worker_connections 1024;
    }
     
    http {
        log_format  dm  '"$request_body"';
        lua_need_request_body on;
        server {
            listen 6699;
            location /post/ {
                content_by_lua '
                    ngx.say("-------")
                    ngx.req.read_body()
                ';
                access_log ~/openresty-test/logs/post.log dm;
                #return 200;
            }
        }
    }

    4. 自定义变量存放request body

    方法:

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

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

    worker_processes  1;        #nginx worker 数量
    error_log /home/shuhao/openresty-test/logs/error.log debug;   #指定错误日志文件路径
    events {
        worker_connections 1024;
    }
     
    http {
        log_format  dm  ' "$request_body"  --  "$resp_body"';
        lua_need_request_body on;
        server {
            listen 6699;
            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 200;
            }
        }
    }

    效果:

    (完)

  • 相关阅读:
    JavaScript模块化CommonJS/AMD/CMD/UMD/ES6Module的区别
    css属性position: static|relative|absolute|fixed|sticky简单解析
    创建自己的library类库包并使用webpack4.x打包发布到npm
    webpack4与babel配合使es6代码可运行于低版本浏览器
    css设置多列等高布局
    css选择器+、~、>
    将已经存在的异步请求callback转换为同步promise
    js属性对象的propertyIsEnumerable方法
    js中的严格模式和非严格模式的比较
    js将某个值转换为String字符串类型或转换为Number数字类型
  • 原文地址:https://www.cnblogs.com/sunsky303/p/9583550.html
Copyright © 2011-2022 走看看