1.http模块何时将http请求各个阶段的处理函数注册到nginx的http框架中,
会在该http模块的postconfiguration回调函数被调用过程中将Http请求某阶段本模块干预处理的函数注册进去;
具体参考代码ngx_http_block函数中如下代码(ngx_http.c中)
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->postconfiguration) {
if (module->postconfiguration(cf) != NGX_OK) { //本处函数调用是关键,它会调用本模块注册的postconfiguration函数,如果本模块对http请求的某个阶段感兴趣可以在此本 //模块的此回调函数中注册对http请求某个阶段的处理方法
return NGX_CONF_ERROR;
}
}
}
具体可以参考ngx_http_log_module模块,它对http请求的NGX_HTTP_LOG_PHASE阶段感兴趣,它会对http请求的NGX_HTTP_LOG_PHASE阶段进行自身的定制处理,如下代码:
static ngx_http_module_t ngx_http_log_module_ctx = {
NULL, /* preconfiguration */
ngx_http_log_init, /* postconfiguration */
ngx_http_log_create_main_conf, /* create main configuration */
.......................................................
};
static ngx_int_t
ngx_http_log_init(ngx_conf_t *cf)
{
.....................................
. ......................................
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_log_handler; //此处是注册本模块对http请求的NGX_HTTP_LOG_PHASE阶段的处理函数
return NGX_OK;
}