Nginx write new module as filter.

Simon Liu simohayha.bobo at gmail.com
Sun Oct 9 02:00:21 UTC 2011


You should use  HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES
ngx_http_corley_filter_module" rather than  HTTP_MODULES="$HTTP_MODULES
ngx_http_corley_filter_module"

On Sun, Oct 9, 2011 at 5:42 AM, Walter Dal Mut <info at walterdalmut.com>wrote:

> Hi everybody I am new to nginx and actually I am interested on nginx
> extensions.
>
> I need to write a new module for nginx and compile a version with this
> module. I don't want to write down an handler but a filter, I need to work
> on headers.
>
> For that reason I start my module using gzip as a base and cut different
> part that I don't want use.
>
> I compile nginx with my module and all works fine. After that I start the
> server with my personal configuration (corley on;) in the main conf file and
> the server start correctly. The problem is that my module seems never used.
> The filter functionality is pretty simple, I want to force text/plain
> header.
>
> I don't understand why my module is never called. Any one can help me?
>
> The main filename is: *ngx_http_corley_filter_module.c*
> The configuration filename is:* config*
> *
> *
> The config content:
> *
> *
> ngx_addon_name=ngx_http_corley_filter_module
> HTTP_MODULES="$HTTP_MODULES ngx_http_corley_filter_module"
> NGX_ADDON_SRCS="$NGX_ADDON_SRCS
> $ngx_addon_dir/ngx_http_corley_filter_module.c"
>
>
> The ngx_http_corley_filter_module.c content:
>
> #include <ngx_config.h>
> #include <ngx_core.h>
> #include <ngx_http.h>
>
> typedef struct {
>    ngx_flag_t           enable;
> } ngx_http_corley_conf_t;
>
> static ngx_int_t ngx_http_corley_filter_init(ngx_conf_t *cf);
> static void * ngx_http_corley_create_conf(ngx_conf_t *cf);
> static char * ngx_http_corley_merge_conf(ngx_conf_t *cf, void *parent, void
> *child);
> static void * ngx_http_corley_create_conf(ngx_conf_t *cf);
>
> static ngx_command_t  ngx_http_corley_filter_commands[] = {
>
>    { ngx_string("corley"),
>
>  NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
>      ngx_conf_set_flag_slot,
>      NGX_HTTP_LOC_CONF_OFFSET,
>      offsetof(ngx_http_corley_conf_t, enable),
>      NULL },
>
>      ngx_null_command
> };
>
>
> static ngx_http_module_t  ngx_http_corley_filter_module_ctx = {
>    NULL,                                  /* preconfiguration */
>    ngx_http_corley_filter_init,           /* postconfiguration */
>    NULL,                                  /* create main configuration */
>    NULL,                                  /* init main configuration */
>    NULL,                                  /* create server configuration */
>    NULL,                                  /* merge server configuration */
>    ngx_http_corley_create_conf,           /* create location configuration
> */
>    ngx_http_corley_merge_conf             /* merge location configuration
> */
> };
>
>
> ngx_module_t  ngx_http_corley_filter_module = {
>    NGX_MODULE_V1,
>    &ngx_http_corley_filter_module_ctx,      /* module context */
>    ngx_http_corley_filter_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_output_header_filter_pt  ngx_http_next_header_filter;
> static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
>
>
> *static ngx_int_t
> ngx_http_corley_header_filter(ngx_http_request_t *r)
> {
>    ngx_http_corley_conf_t  *conf;
>    conf = ngx_http_get_module_loc_conf(r, ngx_http_corley_filter_module);
>
>    ngx_table_elt_t       *h;
>
>    h = ngx_list_push(&r->headers_out.headers);
>    if (h == NULL) {
>        return NGX_ERROR;
>    }
>
>    h->hash = 1;
>    ngx_str_set(&h->key, "Content-Encoding");
>    ngx_str_set(&h->value, "text/plain");
>
>    r->headers_out.content_encoding = h;
>
>    r->main_filter_need_in_memory = 1;
>
>    ngx_http_clear_content_length(r);
>    ngx_http_clear_accept_ranges(r);
>
>
>    return ngx_http_next_header_filter(r);
> }
> *
>
> static ngx_int_t
> ngx_http_corley_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
> {
>    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
>              "not well formed XML document");
>    return ngx_http_next_body_filter(r, in);
> }
>
> static void *
> ngx_http_corley_create_conf(ngx_conf_t *cf)
> {
>    ngx_http_corley_conf_t  *conf;
>
>    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_corley_conf_t));
>    if (conf == NULL) {
>        return NULL;
>    }
>
>    /*
>     * set by ngx_pcalloc():
>     *
>     *     conf->bufs.num = 0;
>     *     conf->types = { NULL };
>     *     conf->types_keys = NULL;
>     */
>
>    conf->enable = NGX_CONF_UNSET;
>
>    return conf;
> }
>
>
> static char *
> ngx_http_corley_merge_conf(ngx_conf_t *cf, void *parent, void *child)
> {
>    ngx_http_corley_conf_t *prev = parent;
>    ngx_http_corley_conf_t *conf = child;
>
>    ngx_conf_merge_value(conf->enable, prev->enable, 0);
>
>    return NGX_CONF_OK;
> }
>
> static ngx_int_t
> ngx_http_corley_filter_init(ngx_conf_t *cf)
> {
>    ngx_http_next_header_filter = ngx_http_top_header_filter;
>    ngx_http_top_header_filter = ngx_http_corley_header_filter;
>
>    ngx_http_next_body_filter = ngx_http_top_body_filter;
>    ngx_http_top_body_filter = ngx_http_corley_body_filter;
>
>    return NGX_OK;
> }
>
> Thanks to all.
> Walter
>
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
>



-- 
douban:www.douban.com/people/mustang/

blog: www.pagefault.info

twitter: www.twitter.com/minibobo

weibo:  www.weibo.com/diaoliang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20111009/0e0912fe/attachment-0001.html>


More information about the nginx-devel mailing list