Handling large POST data in nginx module

Mapper Uno lists at ruby-forum.com
Sun Mar 30 10:20:30 UTC 2014


I wrote an nginx module that handles POST data and returns back the same
(something similar to echo_read_request_body  in echo module) however,
in echo module the input buffer is passed as is to output buffer.
However, in my case, I need to copy the incoming buffer and then process
it and then send it back to client.

What I am seeing in my POST handler routine is, the incoming request
size is 1377344 bytes. However, the file I am sending via curl command
is 1420448 bytes. Where could I be losing the rest of the bytes.
Moreover the curl command on client hangs for a while and eventually
returns 18 error code.

Here is my POST handler code

static void ngx_my_post_handler(ngx_http_request_t *r)
    ngx_chain_t *cl, *in = r->request_body->bufs;
    ngx_buf_t *b;

    for (cl = in; cl; count++, cl=cl->next)
        b = cl->buf;
  if (!ngx_buf_in_memory(b)) {
            if (b->in_file) {
                ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log,
                               " What to do here ??????");

        } else {
            in_sz += b->last - b->pos;

    u_char *out_data = (unsigned char *)ngx_pcalloc(r->pool, in_sz);

    // Copy incoming data to outgoing data buffer
    for (cl = in; cl; count++, cl = cl->next) {
        b = cl->buf;

        if (ngx_buf_in_memory(b)) {
            out_data = ngx_copy(out_data, b->pos, b->last - b->pos);

    // Allocate response buffer and fill it out
    b = (ngx_buf_t *) ngx_pcalloc(r->pool, sizeof (ngx_buf_t));
    if (b == NULL)

    b->pos = out_data;
    b->last = out_data + out_len;
    b->memory = 1;
    b->last_buf = 1;
    b->last_in_chain = 1;

    out_chain.buf = b;
    out_chain.next = NULL;

    // Setup HTTP response headers
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = out_len;
    r->headers_out.content_type.len = sizeof ("text/html") - 1;
    r->headers_out.content_type.data = (u_char *) "text/html";

    ngx_http_output_filter(r, &out_chain);

Posted via http://www.ruby-forum.com/.

More information about the nginx mailing list