linux下获取handler有多种方法:
今天通过自己的理解,又重新写了一种方法,在main初始化的时候,通过ngx_http_conf_get_core_module()函数去获取core_moduler, ,然后在通过ngx_array_push()函数,在core_module里面获取一片handler的地址,然年把自己的handler赋值添加上去,添加到core_moduler里面, 这样的话,是在初始化启动的时候就把handler指定好了,所以我们要更给模块的上下文模块,更改为
static ngx_int_t ngx_http_teston_init( ngx_conf_t *cf );
38 static ngx_http_module_t ngx_http_test_module_ctx = {
39 NULL,
40 ngx_http_teston_init, //这里要提前指定好初始化函数,当ngx在启动时候,会读取并初始化。。
41 NULL,
42 NULL,
43 NULL,
44 NULL,
45 ngx_http_test_create_loc_conf,
46 ngx_http_test_merge_loc_conf
47 };
获取core_moduler的handler如下
114 static ngx_int_t ngx_http_teston_init( ngx_conf_t *cf )
115 {
116 ngx_http_core_main_conf_t *cmcf;
117 ngx_http_handler_pt *h; //源码中是ngx_http_handler_pt 是一个函数指针的定义 typdef ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r );
118
119 cmcf = ngx_http_conf_get_module_main_conf( cf, ngx_http_core_module );
120
121 h = ngx_array_push( &(cmcf->phases[ NGX_HTTP_ACCESS_PHASE ].handlers) ); // 一般我们自己写的模块都在ngx_http_content_phase阶段执行
122 if( h == NULL )
123 {
124 return NGX_ERROR;
125 }
126 *h = ngx_http_teston_handler;
127
128 return NGX_OK;
129
130 }