ngx_http_catch_body_filter doesn't appear to work

ptcell nginx-forum at forum.nginx.org
Thu Oct 17 23:44:14 UTC 2019


I'm trying to use the ngx_http_catch_body_filter example to capture request
bodies from post (etc.) requests.   

I started with the example here in the hg repo:
https://www.nginx.com/resources/wiki/extending/examples/body_filter/

I changed the config to make it a dynamic nginx module.

I can see the `ngx_http_catch_body_init()`  method getting called in the
logs (I put an error log in that method).

When I try a proxied POST or file upload, `ngx_http_catch_body_filter()`
never gets called.

Here's my modified source code and config.   What am I doing wrong?

---- nginx.conf ----
...
load_module modules/ngx_http_catch_body_filter_module.so;
...
        location /app1/ {
            catch_body on;
            proxy_pass http://localhost:8180/java_test_app/;

        }

---- config ----
# (C) Maxim Dounin
# Configuration for ngx_http_catch_body_filter_module.

ngx_addon_name="ngx_http_catch_body_filter_module"

#HTTP_MODULES="$HTTP_MODULES \
#		ngx_http_catch_body_filter_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
		$ngx_addon_dir/ngx_http_catch_body_filter_module.c"

ngx_module_type=HTTP_AUX_FILTER
ngx_module_srcs=$NGX_ADDON_SRCS
ngx_module_name=$ngx_addon_name

. auto/module

---- ngx_http_catch_body_filter_module.c ----

/*
 * Copyright (C) Maxim Dounin
 */

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <stddef.h>

typedef struct {
    ngx_flag_t enable;
} ngx_http_catch_body_conf_t;


static void *ngx_http_catch_body_create_conf(ngx_conf_t *cf);

static char *ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent,
                                            void *child);

static ngx_int_t ngx_http_catch_body_init(ngx_conf_t *cf);


static ngx_command_t ngx_http_catch_body_commands[] = {

        {ngx_string("catch_body"),
         NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF |
NGX_CONF_FLAG,
         ngx_conf_set_flag_slot,
         NGX_HTTP_LOC_CONF_OFFSET,
         offsetof(ngx_http_catch_body_conf_t, enable),
         NULL},

        ngx_null_command
};


static ngx_http_module_t ngx_http_catch_body_module_ctx = {
        NULL,                          /* preconfiguration */
        ngx_http_catch_body_init,      /* postconfiguration */

        NULL,                          /* create main configuration */
        NULL,                          /* init main configuration */

        NULL,                          /* create server configuration */
        NULL,                          /* merge server configuration */

        ngx_http_catch_body_create_conf, /* create location configuration
*/
        ngx_http_catch_body_merge_conf   /* merge location configuration */
};


ngx_module_t ngx_http_catch_body_filter_module = {
        NGX_MODULE_V1,
        &ngx_http_catch_body_module_ctx, /* module context */
        ngx_http_catch_body_commands,  /* module directives */
        NGX_HTTP_MODULE,               /* module type */
        NULL,                          /* init master */
        NULL,                          /* init module */
        NULL,                          /* init process */
        NULL,                          /* init thread */
        NULL,                          /* exit thread */
        NULL,                          /* exit process */
        NULL,                          /* exit master */
        NGX_MODULE_V1_PADDING
};


static ngx_http_request_body_filter_pt ngx_http_next_request_body_filter;


static ngx_int_t
ngx_http_catch_body_filter(ngx_http_request_t *r, ngx_chain_t *in) {
    ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "catch request body
filter");
    fprintf(stderr, "catch request body filter\n");
    return ngx_http_next_request_body_filter(r, in);
}


static void *
ngx_http_catch_body_create_conf(ngx_conf_t *cf) {
    ngx_http_catch_body_conf_t *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_catch_body_conf_t));
    if (conf == NULL) {
        return NULL;
    }

    conf->enable = NGX_CONF_UNSET;
    return conf;
}


static char *
ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent, void *child) {
    ngx_http_catch_body_conf_t *prev = parent;
    ngx_http_catch_body_conf_t *conf = child;

    ngx_conf_merge_value(conf->enable, prev->enable, 0);
    return NGX_CONF_OK;
}


static ngx_int_t
ngx_http_catch_body_init(ngx_conf_t *cf) {
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "init catch request body
filter");
    ngx_http_next_request_body_filter = ngx_http_top_request_body_filter;
    ngx_http_top_request_body_filter = ngx_http_catch_body_filter;
    return NGX_OK;
}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285923,285923#msg-285923



More information about the nginx mailing list