nginx通过ngx_array_push()获取handler

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 }



    


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值