Features of Nginx HTTP Filtering module
A request can be processed by any HTTP module;
When the normal HTTP module processes the request and calls Ngx_http_send_header () to send the HTTP header or call Ngx_http_output_filter () to send the HTTP package body, The two methods will call all HTTP filtering modules at once to process the request. The HTTP Filtering module handles only the responses that the server sends to the client, not the HTTP requests that the client sends to the server.
The sequence of multiple filter modules and Nginx Filter module please refer to the original book.
Second, write an HTTP filter module
Demonstrates how to write an HTTP filter module by adding a section of string "[My filter prefix]" to the text format returned to the user in response to the package body. The source code comes from the deep understanding of nginx.
1.config File Writing
Unlike the HTTP modules of the previous posts, the HTTP Filtering module needs to be http_filter_modules to compile all the filtering modules, so config writes:
Ngx_addon_name=ngx_http_myfilter_module
http_filter_modules= "$HTTP _filter_modules ngx_http_myfilter_module"
ngx_addon_srcs= "$NGX _addon_srcs $ngx _addon_dir/ngx_http_myfilter_module.c"
When the Configure is carried out, the--add-module=path is the same.
2. Writing module basic content: module definition, configuration item processing
Because of the need to add a flag type of add_fix to the nginx.conf to control the use of this filter module, the ngx_http_myfilter_create_conf (), ngx_http_myfilter_, associated with this configuration item processing Merge_conf (), ngx_http_mytest_commands[] need to be processed accordingly.
typedef struct {
ngx_flag_t enable;
} ngx_http_myfilter_conf_t;
typedef struct {
ngx_int_t add_prefix;
} ngx_http_myfilter_ctx_t;
Static void* ngx_http_myfilter_create_conf (ngx_conf_t *cf)
{
ngx_http_myfilter_conf_t *mycf;
MYCF = (ngx_http_myfilter_conf_t *) Ngx_pcalloc (cf->pool,sizeof (ngx_http_myfilter_conf_t));
if (MYCF = = null) {return
null;
}
mycf->enable = Ngx_conf_unset;
return MYCF;
}
Ngx_http_myfilter_create_conf ()
Static char* 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;
}
Ngx_http_myfilter_merge_conf
Static ngx_command_t ngx_http_mytest_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
};
Ngx_http_mytest_commands[]
This is followed by the context and module definition of the module:
Static ngx_http_module_t Ngx_http_myfilter_module_ctx = {
null,
ngx_http_myfilter_init,
null,
NULL ,
null,
null,
ngx_http_myfilter_create_conf,
ngx_http_myfilter_merge_conf
};
Ngx_http_myfilter_module_ctx
ngx_module_t ngx_http_myfilter_module = {
ngx_module_v1,
&ngx_http_myfilter_module_ctx,
ngx_ Http_mytest_commands,
ngx_http_module,
null,
null
,
null, NULL, NULL, NULL,
NULL,
Ngx_module_v1_padding
};
Ngx_http_myfilter_module
As can be seen from the module context, the filtering function starts after the module completes the configuration item processing, and its initialization method is Ngx_myfilter_init ().