nginx收集request_body、response_body

素颜马尾好姑娘i 2021-06-22 15:38 1134阅读 0赞

1、收集request_body:

对于get请求,request_body始终是空,对于post请求,request_body是参数信息。request_body的获取有两种方式:

  • 使用nginx ngx_http_core模块的$request_body;
  • openresty中使用lua脚本。

    首先修改配置文件,我这里采集的日志只有request_body字段

    vim /opt/nginx/conf/nginx.conf
    log_format main $request_body;

    access_log logs/access.log main;

    location / {

    1. root /opt/nginx/html;
    2. # 以下为添加内容
    3. fastcgi_pass 127.0.0.1:9000;
    4. fastcgi_index index.php;
    5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    6. include fastcgi_params;

    }

使用post发送数据,就可以在nginx日志中查看到请求参数了。

  1. curl -XPOST "http://10.10.10.13/test.php?id=123" -H "X-Forwarded-For: 10.10.10.5" -H "Referer: http://10.10.10.13" --data "AAAAAAAAAAAAAAAAAA"

使用ngx_http_core模块收集日志有没有办法限制request_body的长度呢?

1)可以在配置文件中使用client_max_body_size 1k; 但是这个配置是不允许用户上传超过1K大小的body内容,如果用户需要上传图片,业务可能就无法正常运行,所以不推荐使用此种方法。

2)使用lua:

  1. vim /opt/nginx/conf/nginx.conf
  2. log_format main $request_body_head;
  3. access_log logs/access.log main;
  4. location / {
  5. root /opt/nginx/html;
  6. # 以下为添加内容
  7. set $request_body_head "";
  8. content_by_lua_block {
  9. ngx.req.read_body()
  10. local req_body = ngx.req.get_body_data()
  11. # 这里的1000代表我们截取request_body的长度,不要取的太长,否则容易导致日志过大
  12. ngx.var.request_body_head = req_body:sub(1,1000)
  13. }
  14. }

2、收集response_body:

对于response_body我们只有使用lua编写脚本来采集。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. # 以下为添加内容
  5. lua_need_request_body on;
  6. set $response_body "";
  7. body_filter_by_lua '
  8. # 这里的1000就代表截取response_body的长度,不要取的太长,否则容易导致日志过大
  9. local response_body = string.sub(ngx.arg[1],1,1000)
  10. ngx.ctx.buffered = (ngx.ctx.buffered or "") .. response_body
  11. if ngx.arg[2] then
  12. ngx.var.response_body = ngx.ctx.buffered
  13. end
  14. ';
  15. }

3、日志json格式化;

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. log_format main escape=json '{'
  5. '"timestamp": $time_local '
  6. '"remote_addr": $remote_addr '
  7. '"remote_user": "$remote_user '
  8. '"request_method": $request_method '
  9. '"request_uri": "$request_uri" '
  10. '"request_protocol": "$server_protocol" '
  11. '"request_length": $request_length '
  12. '"request_time": $request_time '
  13. '"request_body_head": "$request_body_head" '
  14. '"response_status": $status '
  15. '"body_bytes_sent": $body_bytes_sent '
  16. '"bytes_sent": $bytes_sent '
  17. '"response_body": "$response_body" '
  18. '"http_referer": "$http_referer" '
  19. '"http_user_agent": "$http_user_agent" '
  20. '"http_x_forwarded_for": "$http_x_forwarded_for" '
  21. '"http_host": "$http_host" '
  22. '"server_name": "$server_name" '
  23. '"upstream_addr": "$upstream_addr" '
  24. '"upstream_status": $upstream_status'
  25. '}';
  26. access_log logs/access.log main;

https://zhuanlan.zhihu.com/p/100080719

发表评论

表情:
评论列表 (有 0 条评论,1134人围观)

还没有评论,来说两句吧...

相关阅读