从 $request_body 记录 POST 数据 [英] Logging POST data from $request_body

查看:36
本文介绍了从 $request_body 记录 POST 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的配置设置来处理一堆 GET 请求,这些请求呈现的像素可以很好地处理分析和解析查询字符串以进行日志记录.使用额外的第三方数据流,我需要处理对给定 url 的 POST 请求,该 url 在其请求正文中具有预期的可记录格式的 JSON.我不想使用带有 proxy_pass 的辅助服务器,只想将整个响应记录到关联的日志文件中,就像它对 GET 请求所做的那样.我正在使用的一些代码片段如下所示:

I have my config setup to handle a bunch of GET requests which render pixels that work fine to handle analytics and parse query strings for logging. With an additional third party data stream, I need to handle a POST request to a given url that has JSON in an expected loggable format inside of it's request body. I don't want to use a secondary server with proxy_pass and just want to log the whole response into an associated log file like what it does with GET requests. A snippet of some code that I'm using looks like the following:

GET 请求(效果很好):

GET request (which works great):

location ^~ /rl.gif {
  set $rl_lcid $arg_lcid;
  if ($http_cookie ~* "lcid=(.*S)")
  {
    set $rl_lcid $cookie_lcid;
  }
  empty_gif;
  log_format my_tracking '{ "guid" : "$rl_lcid", "data" : "$arg__rlcdnsegs" }';
  access_log  /mnt/logs/nginx/my.access.log my_tracking;
  rewrite ^(.*)$ http://my/url?id=$cookie_lcid? redirect;
}

这是我想要做的事情:POST 请求(不起作用):

Here is kinda what I am trying to do: POST request (which does not work):

location /bk {
  log_format bk_tracking $request_body;
  access_log  /mnt/logs/nginx/bk.access.log bk_tracking;
}

Curling curl http://myurl/bk -d name=example 给我一个 404 页面未找到.

Curling curl http://myurl/bk -d name=example gives me a 404 page not found.

然后我尝试了:

location /bk.gif {
  empty_gif;
  log_format bk_tracking $request_body;
  access_log  /mnt/logs/nginx/bk.access.log bk_tracking;
}

Curling curl http://myurl/bk.gif -d name=example 给我一个 405 Not Allowed.

Curling curl http://myurl/bk.gif -d name=example gives me a 405 Not Allowed.

我当前的版本是 nginx/0.7.62.非常感谢任何在正确方向上的帮助!谢谢!

My current version is nginx/0.7.62. Any help in the right direction is very much appreciated! Thanks!

更新所以现在我的帖子看起来像这样:

UPDATE So now my post looks like this:

location /bk {
  if ($request_method != POST) {
    return 405;
  }
  proxy_pass $scheme://127.0.0.1:$server_port/dummy;
  log_format my_tracking $request_body;
  access_log  /mnt/logs/nginx/my.access.log my_tracking;
}
location /dummy { set $test 0; }

它正确记录了帖子数据,但在请求者端返回 404.如果我将上面的代码更改为像这样返回 200:

It is logging the post data correctly, but returns a 404 on the requesters end. If I change the above code to return a 200 like so:

location /bk {
  if ($request_method != POST) {
    return 405;
  }
  proxy_pass $scheme://127.0.0.1:$server_port/dummy;
  log_format my_tracking $request_body;
  access_log  /mnt/logs/nginx/my.access.log my_tracking;
  return 200;
}
location /dummy { set $test 0; }

然后正确返回200,但不再记录post数据.

Then it return the 200 correctly, but no longer records the post data.

另一个更新Kinda找到了一个可行的解决方案.希望这可以帮助其他人.

ANOTHER UPDATE Kinda found a working solution. Hopefully this can help other on their way.

推荐答案

这个解决方案就像一个魅力(在 2017 年更新以尊重 log_format 需要在 nginx 配置的 http 部分):

This solution works like a charm (updated in 2017 to honor that log_format needs to be in the http part of the nginx config):

log_format postdata $request_body;

server {
    # (...)

    location = /post.php {
       access_log  /var/log/nginx/postdata.log  postdata;
       fastcgi_pass php_cgi;
    }
}

我认为诀窍是让 nginx 相信你会调用一个 cgi 脚本.

I think the trick is making nginx believe that you will call a cgi script.

这篇关于从 $request_body 记录 POST 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆