记录来自$ request_body的POST数据 [英] Logging POST data from $request_body

查看:115
本文介绍了记录来自$ 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;
}

卷曲卷曲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;
}

卷曲 curl http:// myurl / bk。 gif -d name = example 给我一个 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 ,但不再记录帖子数据。

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天全站免登陆