- auth:农夫-Ben
- email:szjava#126.com(请把#换成@)
- blog:http://blog.youkuaiyun.com/zll_liang
ngx在处理各模块的时候,是使用数组遍历和type来进行的。但为了更好地区分出各大模块的特性,又对各大模块进行上下文的模块定义。
主要有:ngx_core_module_t、ngx_event_module_t、ngx_http_module_t、ngx_mail_module_t四个。
ngx_core_module_t
typedef struct {
ngx_str_t name;
void *(*create_conf)(ngx_cycle_t *cycle); //创建并初始化conf
char *(*init_conf)(ngx_cycle_t *cycle, void *conf); //赋值给conf
} ngx_core_module_t; //核心上下文模块,主要定义核心模块的处理handler
ngx_event_module_t
typedef struct {
ngx_str_t *name;
void *(*create_conf)(ngx_cycle_t *cycle);
char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
ngx_event_actions_t actions; //模块的操作handler, 如epoll的init、add、del等
} ngx_event_module_t; //事件上下文模块
ngx_http_module_t
typedef struct {
ngx_int_t (*preconfiguration)(ngx_conf_t *cf); //读入配置文件前调用
ngx_int_t (*postconfiguration)(ngx_conf_t *cf); //读入配置文件后调用
void *(*create_main_conf)(ngx_conf_t *cf); //创建全局部分调用
char *(*init_main_conf)(ngx_conf_t *cf, void *conf); //初始化全局部份调用
void *(*create_srv_conf)(ngx_conf_t *cf); //在创建主机部分的配置时调用
char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf); //与全局部分配置合并时调用
void *(*create_loc_conf)(ngx_conf_t *cf); //创建位置部分的配置时掉用
char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf); //与主机部分配置合并时调用
} ngx_http_module_t; //http上下文模块,主要是一些http处理的handler
ngx_mail_module_t
typedef struct {
ngx_mail_protocol_t *protocol;
void *(*create_main_conf)(ngx_conf_t *cf);
char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
void *(*create_srv_conf)(ngx_conf_t *cf);
char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
void *conf);
} ngx_mail_module_t;
struct ngx_module_s {
ngx_uint_t ctx_index; //这个index比较重要,是各个模块上下文的编号,不同类的模块有可能重复
ngx_uint_t index; //模块编号,主要在cycle->conf_ctx里根据这个编号调用
ngx_uint_t spare0;
ngx_uint_t spare1;
ngx_uint_t spare2;
ngx_uint_t spare3;
ngx_uint_t version; //模块版本号
void *ctx; //上下文件ctx,每个模块都不一样。
ngx_command_t *commands; //每个模块的命令struct
ngx_uint_t type; //模块类型,是NGX_CORE_MODULE还是NGX_HTTP_MODULE
ngx_int_t (*init_master)(ngx_log_t *log); //init_master钩子
ngx_int_t (*init_module)(ngx_cycle_t *cycle); //初始化模块,在ngx_init_cycle里就调用初始化所有模块
ngx_int_t (*init_process)(ngx_cycle_t *cycle); //初始化处理流程,ngx_worker_process_init调用
ngx_int_t (*init_thread)(ngx_cycle_t *cycle); //初始化线程
void (*exit_thread)(ngx_cycle_t *cycle); //退出线程
void (*exit_process)(ngx_cycle_t *cycle); //退出处理流程,在ngx_worker_process_exit处调用
void (*exit_master)(ngx_cycle_t *cycle); //退出master,ngx_master_process_exit调用
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;
};