Nginx Filter Module
The development steps of the HTTP filter module
- Determine the source code file name;
- Create a config script that adds the directory when configure is executed;
- Define the filter module, instantiate the module structure of the ngx_module_t type;
- Handle the configuration items of interest by setting the ngx_command_t array in the ngx_module_t structure;
- To 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 list of filter modules;
- Implement NGX_HTTP_OUTPUT_HEADER_FILTER_PT and NGX_HTTP_OUTPUT_BODY_FILTER_PT functions;
- After compiling the installation, modify the Filter module option in the nginx.conf configuration file to turn it on or not.
Configuration scripts
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>//declareStaticngx_int_t Ngx_http_myfilter_header_filter (ngx_http_request_t *);Staticngx_int_t Ngx_http_myfilter_body_filter (ngx_http_request_t*,ngx_chain_t*);StaticNgx_http_output_header_filter_pt Ngx_http_next_header_filter;StaticNgx_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 MemoryMYCF = (ngx_http_myfilter_conf_t*) ngx_pcalloc (Cf->pool,sizeof(ngx_http_myfilter_conf_t));if(MYCF = =NULL)returnNULL; mycf->enable = Ngx_conf_unset;returnMYCF;}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);returnNGX_CONF_OK;}/* ------------------------------------------- *///state for prefixtypedefstruct{/* Add_prefix = * 0 The filter module is off * 1 can add prefix * 2 have been added prefix alread Y * /ngx_int_t Add_prefix;} ngx_http_myfilter_ctx_t;//analyse ConfigureStaticngx_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,en ABLE),NULL}, Ngx_null_command};StaticNgx_int_tngx_http_myfilter_init (ngx_conf_t *cf) {//insert before the first nodeNgx_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;returnNGX_OK;}/* ------------------------------------------- *///parseStaticNgx_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_com Mands, Ngx_http_module,NULL,NULL,NULL,NULL,NULL,NULL,NULL, ngx_module_v1_padding};/* ------------------------------------------- */Staticngx_str_t Filter_prefix = ngx_string ("[My filter Module]");//filter to process the headerStaticNgx_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) {returnNgx_http_next_header_filter (R); }//get ContextCTX = Ngx_http_get_module_ctx (r,ngx_http_myfilter_module);if(CTX) {returnNgx_http_next_header_filter (R); }//get Configure by ngx_http_myfilter_conf_tconf = ngx_http_get_module_loc_conf (r,ngx_http_myfilter_module);if(Conf->enable = =0){//add_prefix offreturnNgx_http_next_header_filter (R); }//create ngx_http_myfilter_ctx_tCTX = Ngx_pcalloc (R->pool,sizeof(ngx_http_myfilter_ctx_t));if(CTX = =NULL){returnNgx_error; }//add_prefix = 0 Express do not add prefixCtx->add_prefix =0;//set ContextNgx_http_set_ctx (R,ctx,ngx_http_myfilter_module);//myfilter module only 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_prefixCtx->add_prefix =1;if(R->headers_out. Content_length_n>0) r->headers_out. Content_length_n+ = Filter_prefix. Len; }returnNgx_http_next_header_filter (r);}//filter to process the bodyStaticNgx_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)returnNgx_http_next_body_filter (R,in);//to Make sure never add prefix againCtx->add_prefix =2;//get prefix stringngx_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 bginningngx_chain_t *cl = Ngx_alloc_chain_link (R->pool); Cl->buf = b; Cl->next = in;returnNgx_http_next_body_filter (R,CL);}
Configuration file nginx.conf
In http{}, add
Add_prefix on;
Turn on the filter module
Compile
./configure --add-module=模块路径/makemake install
The HTTP filter module is also an HTTP module, so the HTTP module is written roughly the same way.
Copyright notice: Pain is just in your mind.
The above describes the Nginx HTTP Filter module development, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.