how to capture the response body using nginx lua module

1,015 views
Skip to first unread message

Pradeep L

unread,
Sep 25, 2014, 5:20:00 AM9/25/14
to openre...@googlegroups.com
hi all,
I am developing a tool which will display all the request packets that nginx is getting .Now i am planning to display all the response packets that nginx gives .
so i need to log all the response body  to  some log file .
how do i achieve this using nginx lua module ?
i was able to capture the response body using ngx.location.capture().here i was able to capture  the response body of a particular location ,what i need is  that all the response body that the nginx sends should be logged regardless of location ..
i am newbie to nginx and lua .
please help me ..

Yichun Zhang (agentzh)

unread,
Sep 25, 2014, 3:34:09 PM9/25/14
to openresty-en
Hello!

On Thu, Sep 25, 2014 at 2:20 AM, Pradeep L wrote:
> I am developing a tool which will display all the request packets that nginx
> is getting .Now i am planning to display all the response packets that nginx
> gives .

This is gonna be quite expensive :) You've been warned.

> so i need to log all the response body to some log file .
> how do i achieve this using nginx lua module ?

If you really want to log all your response body data, you can use
body_filter_by_lua to write the data chunks to files as soon as they
arrive. This way you do not have to buffer all the response body data
in memory, which is very expensive for huge responses.

> i was able to capture the response body using ngx.location.capture().

This API buffers all the response body data in memory and for your use
case, you can avoid that. See above.

> what i need
> is that all the response body that the nginx sends should be logged
> regardless of location ..

Well, just use body_filter_by_lua or body_filter_by_lua_file in your
server {} level or even http {} level, then all the inner locations
will inherit this configuration.

Regards,
-agentzh

Pradeep L

unread,
Sep 26, 2014, 3:56:20 AM9/26/14
to openre...@googlegroups.com
hi agentzh ,
         thanks for help .it serverd the purpose. i am capturing the response body for short duration , will caching of response body 
for short duration ,affect the perfarmace of nginx ?
regards 
pradeep 

Yichun Zhang (agentzh)

unread,
Sep 26, 2014, 2:41:32 PM9/26/14
to openresty-en
Hello!

On Fri, Sep 26, 2014 at 12:56 AM, Pradeep L wrote:
> thanks for help .it serverd the purpose. i am capturing the
> response body for short duration , will caching of response body
> for short duration ,affect the perfarmace of nginx ?

Yes. A lot. No matter how long you keep the file data, because it's irrelevant.

Always keep in mind that file I/O is almost always blocking the nginx
event loop, not to mention the high overhead involved in the file I/O
system calls themselves.

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages