nginx源码分析(7)-模块化(2)

分析nginx的模块化架构的实现方式,就要从ngx_module_t结构体入手。ngx_module_t的声明在src/core/ngx_conf_file.h中:

#define NGX_MODULE_V1          0, 0, 0, 0, 0, 0, 1
#define NGX_MODULE_V1_PADDING  0, 0, 0, 0, 0, 0, 0, 0

struct ngx_module_s {
    ngx_uint_t            ctx_index;
    ngx_uint_t            index;

    ngx_uint_t            spare0;
    ngx_uint_t            spare1;
    ngx_uint_t            spare2;
    ngx_uint_t            spare3;

    ngx_uint_t            version;

    void                 *ctx;
    ngx_command_t        *commands;
    ngx_uint_t            type;

    ngx_int_t           (*init_master)(ngx_log_t *log);

    ngx_int_t           (*init_module)(ngx_cycle_t *cycle);

    ngx_int_t           (*init_process)(ngx_cycle_t *cycle);
    ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);
    void                (*exit_thread)(ngx_cycle_t *cycle);
    void                (*exit_process)(ngx_cycle_t *cycle);

    void                (*exit_master)(ngx_cycle_t *cycle);

    uintptr_t             spare_hook0;
    uintptr_t             spare_hook1;
    uintptr_t             spare_hook2;
    uintptr_t             spare_hook3;
    uintptr_t             spare_hook4;
    uintptr_t             spare_hook5;
    uintptr_t             spare_hook6;
    uintptr_t             spare_hook7;
};

index是一个模块计数器,按照每个模块在ngx_modules[]数组中的声明顺序(见objs/ngx_modules.c),从0开始依次给每个模块进行编号:

    ngx_max_module = 0;
    for (i = 0; ngx_modules[i]; i++) {
        ngx_modules[i]->index = ngx_max_module++;
    }(见src/core/nginx.c)

ctx_index是分类的模块计数器,nginx的模块可以分为四种:core、event、http和mail,每一种的模块又会各自计数一下,这个ctx_index就是每个模块在其所属类组的计数值:

    ngx_event_max_module = 0;
    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
            continue;
        }

        ngx_modules[i]->ctx_index = ngx_event_max_module++;
    }(见src/event/ngx_event.c)

    ngx_http_max_module = 0;
    for (m = 0; ngx_modules[m]; m++) {
        if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
            continue;
        }

        ngx_modules[m]->ctx_index = ngx_http_max_module++;
    }(见src/http/ngx_http.c)

    ngx_mail_max_module = 0;
    for (m = 0; ngx_modules[m]; m++) {
        if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
            continue;
        }

        ngx_modules[m]->ctx_index = ngx_mail_max_module++;
    }(见src/mail/ngx_mail.c)

ctx是模块的上下文,不同种类的模块有不同的上下文,四类模块就有四种模块上下文,实现为四个不同的结构体,所以ctx是void *。这是一个很重要的字段,后面会详细剖析。

commands 是模块的指令集,nginx的每个模块都可以实现一些自定义的指令,这些指令写在配置文件的适当配置项中,每一个指令在源码中对应着一个 ngx_command_t结构的变量,nginx会从配置文件中把模块的指令读取出来放到模块的commands指令数组中,这些指令一般是把配置项的 参数值赋给一些程序中的变量或者是在不同的变量之间合并或转换数据(例如include指令),指令可以带参数也可以不带参数,你可以把这些指令想象为 unix的命令行或者是一种模板语言的指令。在nginx的wiki上有每个系统内置模块的指令说明。指令集会在下一篇中详细剖析。

type就是模块的种类,前面已经说过,nginx模块分为core、event、http和mail四类,type用宏定义标识四个分类。

init_master、 init_module、init_process、init_thread、exit_thread、exit_process、 exit_master是函数指针,指向模块实现的自定义回调函数,这些回调函数分别在初始化master、初始化模块、初始化工作进程、初始化线程、退 出线程、退出工作进程和退出master的时候被调用,如果模块需要在这些时机做处理,就可以实现对应的函数,并把它赋值给对应的函数指针来注册一个回调 函数接口。

其余的参数没研究过,貌似从来没有用过,在定义模块的时候,最前面的7个字段和最后面的8个字段的初始化是使用宏NGX_MODULE_V1和NGX_MODULE_V1_PADDING完成的,例如:

ngx_module_t  ngx_core_module = {
    NGX_MODULE_V1,
    &ngx_core_module_ctx,                  /* module context */
    ngx_core_commands,                     /* module directives */
    NGX_CORE_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
};(见src/core/nginx.c)

接 下来剖析一下ngx_module_t的ctx成员,这个成员的意义是每个模块的上下文,所谓的上下文,也就是这个模块究竟可以做什么,从前面的分析可以 知道nginx把所有模块分为四类(core/event/http/mail),对应的,nginx也认为模块的上下文是四种,分别用四个结构体表 示:ngx_core_module_t、ngx_event_module_t、ngx_http_module_t、 ngx_mail_module_t。也就是说,如果一个模块属于core分类,那么其上下文就是ngx_core_module_t结构的变量,其他类 推。这四个结构体类似于ngx_module_t,也是一些函数指针的集合,每个模块根据自己所属的分类,自定义一些操作函数,通过把这些操作函数赋值为 对应分类结构体中的函数指针,这就注册了一个回调函数接口,从而就可以实现更细致的功能了,例如可以为event模块添加事件处理函数,可以为http模 块添加过滤函数等。

这四个结构体和commands会在下一篇中详细剖析,现在把注意力转移一下,来品味一下nginx的命名,找出其规律,这对阅读源码的帮助是非常大的。

首先是模块,模块是ngx_module_t结构的变量,其命名格式为:ngx_<module name>_module。
然 后是模块上下文,模块上下文根据不同的模块分类分别是ngx_core_module_t、ngx_event_module_t、 ngx_http_module_t和ngx_mail_module_t结构的变量,其命名格式为:ngx_<module name>_module_ctx。
接着是模块命令集,模块命令集是ngx_command_t的指针数组,其命名格式为:ngx_<module name>_commands。

go on

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值