nginx源码分析-nginx模块定义和各大模块的上下文模块定义

本文深入探讨了Nginx中核心模块、事件模块、HTTP模块和邮件模块的上下文定义,解释了如何通过数组遍历和类型区分来处理各模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx模块定义和各大模块的上下文模块定义:
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;

ngx_module_s
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;
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值