$request_body bug?

agentzh agentzh at gmail.com
Thu Oct 22 11:57:02 MSD 2009

On Mon, Oct 5, 2009 at 7:08 PM, dennis cao <dennis__cao at hotmail.com> wrote:
> When I use this variable in log_format directive ,it can print the request_body in the log file
> but somewhere else it can't!

The $request_body variable only has values when the underlying content
handler or upstream handler or anything else has actually *read* the
request body from the tcp connection and stored it into
r->request_body slot.

The variable itself merely reads that buffered data and will never
read from socket itself. See the ngx_http_variable_request_body
function in src/http/ngx_http_variables.c in the Nginx source tree.

It can be easily demonstrated by means of my echo module [1]:

      location /echo_body {
        echo [$request_body];

Accessing /echo from the client side gives

    $ echo "my body" | lwp-request -m POST 'http://localhost:8080/echo_body'

As we can see, it's empty, because the "echo" module does NOT read the
client request body [2]. Now let's use something that actually reads
the body, like the standard proxy module:

    location /echo_body2 {
        proxy_pass $scheme://$server_port/dummy;
        echo_after_body [$request_body];
    location /dummy { echo_duplicate 0 ''; }

Accessing /echo_body2 gives the *desired* results:

    $ echo "my body" | lwp-request -m POST 'http://localhost:8080/echo_body2'
    [my body

Hope this helps :)


[1] echo-nginx-module wiki page: http://wiki.nginx.org/NginxHttpEchoModule
[2] On the C level, it's usually the ngx_http_read_client_request_body
function that does the body reading work, but can be also done by any
other code (as in the "upload" module).

More information about the nginx mailing list