Welcome! Log In Create A New Profile

Advanced

ngx_http_catch_body_filter doesn't appear to work

October 17, 2019 07:44PM
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;
}
Subject Author Posted

ngx_http_catch_body_filter doesn't appear to work

ptcell October 17, 2019 07:44PM

Re: ngx_http_catch_body_filter doesn't appear to work

ptcell October 18, 2019 04:44PM

Re: ngx_http_catch_body_filter doesn't appear to work

redflag July 13, 2020 10:09AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 113
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready