nginx记录post body/payload数据 日志用打印出结果

时间:2022-07-23
本文章向大家介绍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_addrt$remote_usert[$time_local]t"$request"t$statust$bytes_sentt'
                '"$http_referer"t"$http_user_agent"t"$http_x_forwarded_for"t"$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;
        }
    }
}

效果:

(完)