How to log response body in nginx access log

9,168 views
Skip to first unread message

lyzh...@gmail.com

unread,
May 21, 2014, 2:29:53 AM5/21/14
to openre...@googlegroups.com
I want to log all requests(post body included) and responses in a log, I saw I can use "ngx.log()", but it will output in error log, is there a good way to log all requests and responses in access log?

Thanks!

Yichun Zhang (agentzh)

unread,
May 21, 2014, 3:06:13 PM5/21/14
to openresty-en
Hello!
You can use the $request_body variable in the standard log_format directive:

http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

BTW, when your request body is large enough to be buffered to a
temporary file, the value of $request_body would be empty. See
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_body

Regards,
-agentzh

luoji...@gmail.com

unread,
Aug 22, 2014, 2:40:08 AM8/22/14
to openre...@googlegroups.com

how about the response body?  is there any module can accomplish this task?
thanks 
-nevin
在 2014年5月22日星期四UTC+8上午3时06分13秒,agentzh写道:

Yichun Zhang (agentzh)

unread,
Aug 22, 2014, 3:43:02 PM8/22/14
to openresty-en
Hello!

On Thu, Aug 21, 2014 at 11:40 PM, luojiaoscu wrote:
>
> how about the response body? is there any module can accomplish this task?

Response bodies can be huge. So logging such things can be very
expensive. Are you sure you want to do this?

Technically speaking, you could use body_filter_by_lua [1] to write
response data chunks to your own files without the overhead of
buffering all the response body data in memory. But still, this is
rather expensive due to the potentially large amount of data.

BTW, please subscribe to this list before posting. See
http://openresty.org/#Community

Regards,
-agentzh

[1] https://github.com/openresty/lua-nginx-module#body_filter_by_lua
Reply all
Reply to author
Forward
0 new messages