Logging POST data from $request_body

This solution works like a charm:

http {

   log_format postdata $request_body;

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

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

Edit 2022-03-15: there is some discussion on where log_format should be set. The documentation clearly says that it needs to be in the http context: http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

If you put log_format in the server context, nginx will fail to load the config: nginx: [emerg] "log_format" directive is not allowed here in <path>:<line> (tested with nginx 1.20 on ubuntu 18.04)

Leave a Comment