您的位置:首页 > 运维架构 > Nginx

nginx 源码学习笔记(三)——nginx精粹-模块

2012-03-14 17:48 573 查看
下面开始真正"nginx之旅",屏住呼吸吧!

首先找好入手点,对于nginx的入手点就是ngx_module_t结构,他的声明在src/core/ngx_conf_file.h中(我的版本nginx-1.0.13)

#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[]数组中的声明顺序,从0开始一次给每个模块赋值:

声明顺序在文件objs/ngx_modules.c中

ngx_module_t *ngx_modules[] = {
&ngx_core_module,
&ngx_errlog_module,
&ngx_conf_module,
&ngx_events_module,
&ngx_event_core_module,
&ngx_epoll_module,
&ngx_http_module,
&ngx_http_core_module,
&ngx_http_log_module,


从src/core/nginx.c中可以找到顺序赋值的代码:

ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]->index = ngx_max_module++;
}


ctx_index是分类的模块计数器,nginx模块可以分为四种:core、event 、http和mail,每个模块都会各自技术,ctx_index就是每个模块在其所属类组的技术:代码如下

src/event/ngx_event.c
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/http/ngx_http.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/mail/ngx_mail.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++;
}


ctx是模块的上下文,不同种类的模块有不同的上下文,因此实现了四种结构体。(这里非常重要)

commands是模块的指令集。每一个指令在源码中对应着一个ngx_command_t结构变量,

static ngx_command_t  ngx_core_commands[] = {

{ ngx_string("daemon"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_core_conf_t, daemon),
NULL },.....不全部列举


type就是模块的种类,用于区分前面提到的core event http和mail .

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

其余暂且不详。。

接下来剖析一下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模块添加过滤函数等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: