Cannot log post request body to log file

1,538 views
Skip to first unread message

ChangZhuo Chen

unread,
May 5, 2015, 2:57:10 AM5/5/15
to openre...@googlegroups.com
Hi,

I want to log the post request body into log file in openresty 1.7.10.1, but the $request_body
variable is always not set by openresty. The following is the content of nginx.conf in openresty:

    http {
        log_format my_combined '"$request_body"';
    
        server {
            access_log /srv/nginx/log/access_combined.log my_combined;
            error_log /srv/nginx/error_log/error.log;
    
            location /post {
                echo_read_request_body;
                return 200;
            }
    }


ps: I use echo_read_request_body in nginx 1.6.2, and it works fine.

Chris Tanner

unread,
May 6, 2015, 6:05:26 AM5/6/15
to openre...@googlegroups.com
Hi,

Have you tried forcing it to load?


Cheers,
Chris

ChangZhuo Chen (陳昌倬)

unread,
May 6, 2015, 6:39:17 AM5/6/15
to openre...@googlegroups.com
On Wed, May 06, 2015 at 03:05:26AM -0700, Chris Tanner wrote:
> Hi,
>
> Have you tried forcing it to load?
>
> http://wiki.nginx.org/HttpLuaModule#lua_need_request_body
>
> Cheers,
> Chris

I solved the issue by removing "return 200;". The $request_body will not
contain the POST body if there is "return 200;". It is okay when "return
200;" is removed.

Not sure if it is bug or feature.

--
ChangZhuo Chen (陳昌倬) <czc...@gmail.com>
http://czchen.info/
Key fingerprint = EC9F 905D 866D BE46 A896 C827 BE0C 9242 03F4 552D
signature.asc

Yichun Zhang (agentzh)

unread,
May 7, 2015, 3:37:07 AM5/7/15
to openresty-en
Hello!

On Tue, May 5, 2015 at 2:57 PM, ChangZhuo Chen wrote:
> location /post {
> #
> http://wiki.nginx.org/HttpEchoModule#echo_read_request_body
> echo_read_request_body;
> return 200;

Here's an obvious mistake: the "return" directive runs in the
"rewrite" nginx request processing phase while the
"echo_read_request_body" runs in the "content" phase. The "rewrite"
phase *always* runs before "content" phase. So your
"echo_read_request_body" directive will never run because the "return
200" line short-circuits the request processing early (no matter how
you arrange their order in the nginx.conf configuration).

I suggest you read my nginx tutorials for directive processing order
and other topics to save troubles like this in the future:

http://openresty.org/#eBooks

> I solved the issue by removing "return 200;". The $request_body will not
> contain the POST body if there is "return 200;". It is okay when "return
> 200;" is removed.

This explains :)

> Not sure if it is bug or feature.

No, it's not a bug nor a feature. Just a consequence of misuse and
misunderstanding :)

Best regards,
-agentzh

ChangZhuo Chen (陳昌倬)

unread,
May 7, 2015, 3:59:50 AM5/7/15
to openre...@googlegroups.com
On Thu, May 07, 2015 at 03:37:05PM +0800, Yichun Zhang (agentzh) wrote:
> I suggest you read my nginx tutorials for directive processing order
> and other topics to save troubles like this in the future:
>
> http://openresty.org/#eBooks

Thank for the explanation and tutorials.
signature.asc

Mark Moseley

unread,
May 7, 2015, 12:30:47 PM5/7/15
to openre...@googlegroups.com
On Thu, May 7, 2015 at 12:37 AM, Yichun Zhang (agentzh) <age...@gmail.com> wrote:
Hello!
....

I suggest you read my nginx tutorials for directive processing order
and other topics to save troubles like this in the future:

http://openresty.org/#eBooks



Any thoughts about writing something book-sized about Nginx+Lua? Your nginx+lua/openresty work is some pretty exciting stuff. I've read and re-read your API page a number of times, but nothing quite beats code with explanation. I often tag your code samples in this list for future ref, but something in the form of a tutorial would be awesome.

Yichun Zhang (agentzh)

unread,
May 7, 2015, 11:39:08 PM5/7/15
to openresty-en
Hello!

On Fri, May 8, 2015 at 12:30 AM, Mark Moseley wrote:
>
> Any thoughts about writing something book-sized about Nginx+Lua? Your
> nginx+lua/openresty work is some pretty exciting stuff. I've read and
> re-read your API page a number of times, but nothing quite beats code with
> explanation. I often tag your code samples in this list for future ref, but
> something in the form of a tutorial would be awesome.
>

Thanks for the suggestion! Yes, I always want to write more tutorials
on OpenResty. They may eventually reach the book size. But I
definitely need to find some more time to start writing :) Stay tuned.

Best regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages