Nginx:http Filter Module

Source: Internet
Author: User

Reference < in-depth understanding nginx>

The HTTP filter module is also an HTTP module, unlike ordinary HTTP processing modules:

1. A request is handled by only one HTTP processing module, and can be handled by any HTTP filtering module

2. The normal HTTP module tends to fulfill the core function of the request, and the HTTP filter module does some processing for the HTTP response packet sent to the user.

A simple example of an HTTP filter module

The filtering module realizes that the user's request is handled by a static file module, which returns the files on the disk to the user according to the URI, and then adds a string of strings before returning the corresponding package to the user: "[my filter prefix]"

Static static file module will call Ngx_http_send_header and Ngx_http_output_filter to call the filter module when processing is complete.

1. Writing the config file

Unlike normal HTTP modules, the Http_modules variable is changed to Http_filter_modules

Ngx_addon_name=ngx_http_myfilter_modulehttp_filter_modules="$HTTP _filter_modules ngx_http _myfilter_module"ngx_addon_srcs="$NGX _addon_srcs $ngx _addon_dir/ngx_http_ myfilter_module.c"

2. Configuration Items and contexts

The filter module wants a configuration item in nginx.conf that controls whether the current HTTP filter module is active, and its parameter value is on or off. First, create the following structure to store the configuration items

struct {    ngx_flag_t enable;} ngx_http_myfilter_conf_t;

The ngx_http_myfilter_create_conf implemented below is used to allocate memory for the structure that stores configuration items

Static void *ngx_http_myfilter_create_conf (ngx_conf_t *CF) {    *mycf;    MYCF= (ngx_http_myfilter_conf_t *) Ngx_pcalloc (cf->pool,sizeof(ngx_http_filter_conf_t));     if (mycf==null) {        return  null;    }    MYCF->enable=ngx_conf_unset;     return MYCF;}

Because of the nginx asynchronous processing, an HTTP packet processing method may be called multiple times in 1 requests, but in fact we only want to add 1 prefixes in the header. Therefore, if you create an HTTP context structure to handle HTTP packets, add a prefix

struct {    ngx_int_t add_prefix;} ngx_http_myfilter_ctx_t;

3. Define the HTTP filter module, similar to the normal HTTP module, not much explanation.

Defines the ngx_http_myfilter_commands array, where configuration items are handled using the Nginx preset method Ngx_conf_set_flag_slot

 static  ngx_command_t ngx_http_myfilter_commands[]={{ //  configuration Item name  ngx_string (  " add_prefix   "  //  configuration item Type        (where you can appear, the number of arguments)  Ngx_http_loc_conf| Ngx_http_lmt_conf| ngx_conf_flag,  //   Ngx_conf_set_flag_slot, Ngx_http_loc_conf_offset, Offsetof (ngx_http_my filter_conf_t,enable), NULL}, Ngx_null_command};  

The definition Ngx_http_module_t,ngx_http_myfilter_init method is used to initialize the HTTP filter module, which has its implementation code

static ngx_http_module_t ngx_http_myfilter_module_ctx={    NULL,    ngx_http_myfilter_init,    NULL,    NULL,    null,    null,    ngx_http_myfilter_create_conf,    null};

Define ngx_module_t

ngx_module_t ngx_http_myfilter_module={    ngx_module_v1,    // point to ngx_http_module_t struct     &ngx_http_myfilter_module_ctx,    // for handling configuration Items     in nginx.conf Ngx_http_myfilter_commands,    // indicates the type of the module     ngx_http_module,    NULL,    NULL,    NULL, NULL, NULL, NULL, NULL,    ngx_module_v1_padding};

4. Initializing the HTTP filter module

A request can be processed by more than one filter module, so the filter module has a call order, which is used to construct the filter list

//used to initialize the HTTP filter moduleStaticngx_http_output_header_filter_pt Ngx_http_next_header_filter;Staticngx_http_output_filter_pt Ngx_http_next_body_filter;Staticngx_int_t Ngx_http_myfilter_init (ngx_conf_t *CF) {    //Insert the header into the head processing method chain listNgx_http_next_header_filter=Ngx_http_top_header_filter; Ngx_http_top_header_filter=Ngx_http_myfilter_header_filter; //Insert the Package Body processing method list headerNgx_http_next_body_filter=Ngx_http_top_body_filter; Ngx_http_top_body_filter=Ngx_http_myfilter_body_filter; returnNgx_ok;}

5. HTTP header for processing requests

1 Staticngx_int_t2Ngx_http_myfilter_header_filter (ngx_http_request_t *R)3 {4ngx_http_myfilter_ctx_t *CTX;5ngx_http_myfilter_conf_t *conf;6     //If the return fails, it is directly referred to the next filter module for processing7     if(r->headers_out.status!=NGX_HTTP_OK) {8         returnNgx_http_next_header_filter (r);9     }Ten     //Get HTTP Context Onectx=Ngx_http_get_module_ctx (r,ngx_http_myfilter_module); A     if(CTX) { -         //if the requested context already exists, Ngx_http_myfilter_header_filter has been called 1 times -         returnNgx_http_next_header_filter (r); the     } -     //gets the ngx_http_myfilter_conf structure of the storage configuration item -conf=ngx_http_get_module_loc_conf (r,ngx_http_myfilter_module); -     //If the enable member is 0, it is delivered directly to the next +     if(conf->enable==0){ -         returnNgx_http_next_header_filter (r); +     } A     //construct context structure body ngx_http_myfilter_ctx_t atCtx=ngx_pcalloc (R->pool,sizeof(ngx_http_myfilter_ctx_t)); -     if(ctx==NULL) { -         returnNgx_error; -     } -     //add_prefix 0 means no prefix -ctx->add_prefix=0; in     //sets the context of the construct to the current request - Ngx_http_set_ctx (r,ctx,ngx_http_myfilter_module); to     //myfilter Filter Module only handles HTTP responses of type 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) the     { *         //A setting of 1 indicates that the prefix needs to be added before the HTTP package body $ctx->add_prefix=1;Panax Notoginseng         //Add the length of the prefix string to the Content-length -         if(r->headers_out.content_length_n>0){ ther->headers_out.content_length_n+=Filter_prefix.len; +         } A     } the     //continue processing with the next filter module +     returnNgx_http_next_header_filter (r); -}
View Code

6. HTTP package for processing requests

1 //handling HTTP packets in a request2 Staticngx_int_t3Ngx_http_myfilter_body_filter (ngx_http_request_t *r,ngx_chain_t *inch)4 {5ngx_http_myfilter_ctx_t *CTX;6ctx=Ngx_http_get_module_ctx (r,ngx_http_myfilter_module);7     //No prefix is added if the context is not obtained, or if Add_prefix is 0 or 2 o'clock in the context structure body8     if(ctx==null| | ctx->add_prefix!=1){9         returnNgx_http_next_body_filter (R,inch);Ten     } One     //set Add_prefix to 2 to prevent repeated prefix additions Actx->add_prefix=2; -     //allocating memory for storing string prefixes -ngx_buf_t *b=ngx_create_temp_buf (r->Pool,filter_prefix.len); the     //point the pointer in ngx_buf_t to the Filter_prefix string -b->start=b->pos=Filter_prefix.data; -b->last=b->pos+Filter_prefix.len; -     //generate the ngx_chain_t linked list to send, plus the ngx_buf_t member you just assigned +Ngx_chain_t *cl=ngx_alloc_chain_link (r->pool); -cl->buf=b; +cl->next=inch; A     //Call the next filter module at     returnNgx_http_next_body_filter (R,CL); -}
View Code

7. Configuring the Nginx.conf File

Add the following configuration, add the Txt/test.txt file under the Nginx installation directory, and access the localhost/test.txt.

Location/ {    root txt;    Add_prefix on;}

Nginx:http Filter Module

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.