nginx通过ngx_array_push()获取handler

本文介绍了一种在Linux环境下为Nginx模块注册Handler的方法。通过在初始化阶段使用ngx_http_conf_get_core_module()获取核心配置,并利用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 }



    


static ngx_int_t ngx_http_flv_live_init(ngx_conf_t *cf) { ngx_str_t *value; ngx_array_t a; ngx_http_handler_pt *h; ngx_http_flv_live_fmt_t *fmt; ngx_http_flv_live_main_conf_t *lmcf; ngx_http_core_main_conf_t *cmcf; lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_flv_live_module); if (lmcf->combined_used) { if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) { return NGX_ERROR; } value = ngx_array_push(&a); if (value == NULL) { return NGX_ERROR; } *value = ngx_http_combined_fmt; fmt = lmcf->formats.elts; if (ngx_http_flv_live_compile_format(cf, NULL, fmt->ops, &a, 0) != NGX_CONF_OK) { return NGX_ERROR; } } 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_flv_live_handler; ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_flv_parser_body_filter; return NGX_OK; } static ngx_http_module_t ngx_http_flv_live_module_ctx = { NULL, /* preconfiguration */ ngx_http_flv_live_init, /* postconfiguration */ ngx_http_flv_live_create_main_conf, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_flv_live_create_loc_conf, /* create location configuration */ ngx_http_flv_live_merge_loc_conf /* merge location configuration */ }; ngx_module_t ngx_http_flv_live_module = { NGX_MODULE_V1, &ngx_http_flv_live_module_ctx, /* module context */ ngx_http_flv_live_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING };
最新发布
08-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值