NginxHTTP filter module development

Source: Internet
Author: User
: This article mainly introduces NginxHTTP filter module development. if you are interested in the PHP Tutorial, refer to it. Nginx filter module

HTTP filter module development steps

  1. Determine the source code file name;
  2. Create a config script and add the directory when you execute configure;
  3. Define the filter module and instantiate the ngx_module_t module structure;
  4. You can set the ngx_command_t array in the ngx_module_t structure to process the configuration items you are interested in;
  5. Implement the initialization function. the initialization method is to insert the ngx_http_output_header_filter_t and ngx_http_output_body_filter_t functions into the header of the filter module linked list;
  6. Implement functions ngx_http_output_header_filter_pt and ngx_http_output_body_filter_pt;
  7. After compilation and installation, modify the filter module option in the nginx. conf configuration file.

Configuration script

ngx_addHTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_myfilter_module"NGX_ADD>"$NGX_ADDON_SRCS$ngx_addon_dir/ngx_http_myfilter_module.c"

Module content

#include <../src/core/ngx_config.h>#include <../src/core/ngx_core.h>#include <../src/http/ngx_http.h>//Declarestatic ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *);static ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t*,ngx_chain_t*);static ngx_http_output_header_filter_pt ngx_http_next_header_filter;static ngx_http_output_body_filter_pt ngx_http_next_body_filter;//On/Offtypedefstruct {    ngx_flag_t enable;}ngx_http_myfilter_conf_t;staticvoid *ngx_http_myfilter_create_conf(ngx_conf_t *cf){    ngx_http_myfilter_conf_t *mycf;    //Allocate memory    mycf = (ngx_http_myfilter_conf_t*)ngx_pcalloc(cf->pool,sizeof(ngx_http_myfilter_conf_t));    if(mycf == NULL)        returnNULL;    mycf->enable = NGX_CONF_UNSET;    return mycf;}staticchar *ngx_http_myfilter_merge_conf(ngx_conf_t *cf,void *parent,void *child){    ngx_http_myfilter_conf_t *prev = (ngx_http_myfilter_conf_t*)parent;    ngx_http_myfilter_conf_t *conf = (ngx_http_myfilter_conf_t*)child;    ngx_conf_merge_value(conf->enable,prev->enable,0);    return NGX_CONF_OK;}/* ------------------------------------------- *///State for prefixtypedefstruct {    /* add_prefix =      * 0    the filter module is off     * 1    can add prefix     * 2    has been added prefix already     */    ngx_int_t add_prefix;} ngx_http_myfilter_ctx_t;//Analyse configurestatic ngx_command_tngx_http_myfilter_commands[]={    {ngx_string("add_prefix"),        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG,        ngx_conf_set_flag_slot,        NGX_HTTP_LOC_CONF_OFFSET,        offsetof(ngx_http_myfilter_conf_t,enable),        NULL},    ngx_null_command};static ngx_int_tngx_http_myfilter_init(ngx_conf_t *cf){    //Insert before the first node    ngx_http_next_header_filter = ngx_http_top_header_filter;    ngx_http_top_header_filter = ngx_http_myfilter_header_filter;    ngx_http_next_body_filter = ngx_http_top_body_filter;    ngx_http_top_body_filter = ngx_http_myfilter_body_filter;    return NGX_OK;}/* ------------------------------------------- *///Parsestatic ngx_http_module_tngx_http_myfilter_module_ctx = {    NULL,    ngx_http_myfilter_init,    NULL,    NULL,    NULL,    NULL,    ngx_http_myfilter_create_conf,    ngx_http_myfilter_merge_conf};/* ------------------------------------------- *///Module informationngx_module_t ngx_http_myfilter_module = {    NGX_MODULE_V1,    &ngx_http_myfilter_module_ctx,    ngx_http_myfilter_commands,    NGX_HTTP_MODULE,    NULL,    NULL,    NULL,    NULL,    NULL,    NULL,    NULL,    NGX_MODULE_V1_PADDING};/* ------------------------------------------- */static ngx_str_t filter_prefix = ngx_string("[my filter module]");//Filter to process the headerstatic ngx_int_tngx_http_myfilter_header_filter(ngx_http_request_t *r){    ngx_http_myfilter_ctx_t *ctx;    ngx_http_myfilter_conf_t *conf;    if(r->headers_out.status != NGX_HTTP_OK){        return ngx_http_next_header_filter(r);    }    //Get context    ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module);    if(ctx){        return ngx_http_next_header_filter(r);    }    //Get configure by ngx_http_myfilter_conf_t    conf = ngx_http_get_module_loc_conf(r,ngx_http_myfilter_module);    if(conf->enable == 0){//add_prefix offreturn ngx_http_next_header_filter(r);    }    //Create ngx_http_myfilter_ctx_t    ctx = ngx_pcalloc(r->pool,sizeof(ngx_http_myfilter_ctx_t));    if(ctx == NULL){        return NGX_ERROR;    }    //add_prefix = 0 express do not add prefix    ctx->add_prefix = 0;    //Set context    ngx_http_set_ctx(r,ctx,ngx_http_myfilter_module);    //myfilter module only figure out Content-Type="text/plain"if(r->headers_out.content_type.len>=sizeof("text/plain") - 1 &&            ngx_strncasecmp(r->headers_out.content_type.data,(u_char*)"text/plain",sizeof("text/plain")-1)==0){        //Set add_prefix        ctx->add_prefix = 1;        if(r->headers_out.content_length_n>0)            r->headers_out.content_length_n += filter_prefix.len;    }        return ngx_http_next_header_filter(r);}//Filter to process the bodystatic ngx_int_tngx_http_myfilter_body_filter(ngx_http_request_t *r,ngx_chain_t *in){    ngx_http_myfilter_ctx_t *ctx;    ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module);    //If cannot get context or the add_prefix is 0/2,do not processif(ctx == NULL || ctx->add_prefix !=1)        return ngx_http_next_body_filter(r,in);    //To make sure never add prefix again    ctx->add_prefix = 2;    //Get prefix string    ngx_buf_t *b = ngx_create_temp_buf(r->pool,filter_prefix.len);    b->start = b->pos = filter_prefix.data;    b->last = b->pos + filter_prefix.len;    //Get and set ngx_chain_t list at the bginning    ngx_chain_t *cl = ngx_alloc_chain_link(r->pool);    cl->buf = b;    cl->next = in;    return ngx_http_next_body_filter(r,cl);}

Configuration file nginx. conf

Add in http {}
Add_prefix on;
Enable filter module

Compile

./Configure -- add-module = module path/makemake install

The HTTP filter module is also an HTTP module, so the writing of the HTTP module is similar.

Copyright: Pain is just in your mind.

The above describes the development of the Nginx HTTP filter module, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.