5

I need to log POST data. I have added to my config

 location / {
     echo_read_request_body;
     access_log     /var/log/nginx/domains/site.post.log postdata;
 }

And also added this to http section

log_format  postdata '$time_local $request_body';

But in my log I can see only local time and dash:

23/Jul/2016:16:07:49 +0000 - 

What's the problem?

0

3 Answers 3

3

I was having the same problem: no post data were being logged. I fixed my problem by ensuring the endpoint returned something. The following worked for me:

location /log {
    echo_read_request_body;
    access_log /var/log/nginx/postdata.log postdata;
    default_type text/html;
    echo "POST";
}

The key is:

  1. set the Content-Type headers with default_type
  2. provide some content in the response with echo
1
  • Worked even without the default_type for me, just with echo.
    – tonso
    Commented Feb 2, 2021 at 16:32
2
location = / {
  log_format postdata $request_body;
  access_log /var/log/nginx/domains/site.post.log postdata;
  fastcgi_pass php_cgi;
}

nginx doesnt touch client request body unless it has to, so it does not satisfy the $request_body variable, there are a few exceptions 1) if it sends request to a proxy 2)or a fastcgi server. so in this example above the trick is have nginx believe that you will call a cgi script.

3
  • 3
    Rather than only providing this example code, could you please explain your answer? Understanding is far more important than following a recipe, after all.
    – Spooler
    Commented Feb 15, 2017 at 18:52
  • How is this different from what the OP did?
    – gxx
    Commented Feb 15, 2017 at 23:16
  • 1
    @Spooler this answer is partially copied from here, and there seems to be more info Commented Oct 10, 2019 at 8:32
1

By default, Nginx is not shipped with the module which contains the echo_read_request_body. You need to compile it by yourself with the module.

In our use case, to log a request body, what we need is to use the echo_read_request_body command and the $request_body variable (contains the request body of the Echo module). However, this module is not distributed with NGINX by default, and to be able to use it we have to build NGINX from its source code by building it with the source code of the Echo module included."

from https://developers.redhat.com/blog/2016/05/23/configuring-nginx-to-log-post-data-on-linux-rhel/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .