nginx-http模块的数据结构

在编写HTTP第三方模块时,需要了解nginx中HTTP模块的数据结构


定义HTTP模块方式很简单

ngx_module_t ngx_http_mytest_module;


就是这么简单,


下面先分析一下nginx模块中的所有成员:

typedef struct ngx_module_s ngx_module_t;
struct ngx_module_s
{
    //分别赋值ctx_index.index.spare0.spare2.sapre3.version
    #define NGX_MODULE_V1     0,0,0,0,0,0,1
    
    //同一类模块的序号,有管理这类模块的nginx核心模块设置。很重要。因为nginx模块依赖于顺序。
    ngx_uint_t ctx_index;
    
    /*nginx启动时根据ngx_modules数组设置各模块的index值
    ngx_max_module = 0;
    for(i = 0; ngx_modules[i];i++)
    {
        ngx_modules[i]->index = ngx_max_module++;
    }
    */
    //index表示当前模块在ngx_modules中的序号
    ngx_uint_t index;
    
    //spare系列保留变量,暂时没有使用
    ngx_uint_t     spare0;
    ngx_uint_t     spare1;
    ngx_uint_t     spare2;
    ngx_uint_t     spare3;
    
    //模块的版本号
    ngx_unit_t    version;
    
    //用于指向一类模块的上下文结构体。ctx指向特定类型模块的公共接口
    //例:http模块中,ctx指向ngx_http_module_t结构体
    void        *ctx;
    
    //nginx.conf配置项
    ngx_command_t    *command;
    
    //表示模块类型。与ctx相关。
    //5种取值。NGX_HTTP_MODULE.NGX_CORE_MODULE,NGX_CONF_MODULE.NGX_EVENT_MODULE
    //NGX_MAIL_MODULE
    ngx_uint_t    type;
    
    //下面是7个函数指针表示有7个执行点会分别调用这7种方法。
    //1.启动时回调init_master
    ngx_int_t     (*init_master)(ngx_log_t *log);
    
    //2.init_module初始化所有模块时被调用。master/worker模式,在启动worker子进程前完成
    ngx_int_t     (init_module)(ngx_cycle_t *cycle);
    
    //3.正常服务前被调用,master/worker模式,多个worker已经残生,每个worker初始化会调用
    ngx_int_t         (init_process)(ngx_cycle_t *cycle);
    
    //4.这个可以忽视,就是代表多线程模式的初始化,Nginx不支持多线程。1.015版本是这样,后面不清楚
     ngx_int_t         (init_thread)(ngx_cycle_t *cycle);
     
     //5.同上
     void              (*exit_thread)(ngx_cycle_t *cycle);
     
     //6.服务停止前调用,worker进程在退出前调用
     void              (*exit_process)(ngx_cycle_t *cycle);
     
     //7.master进程退出前调用
     void              (*exit_master)(ngx_cycle_t *cycle);
     
     //保留字段,可以使用NGX_MODULE_V1_PADDING填充,同第一个
     uintprt_t        spare_hook0;
     uintprt_t        spare_hook1;
     uintprt_t        spare_hook2;
     uintprt_t        spare_hook3;
     uintprt_t        spare_hook4;
     uintprt_t        spare_hook5;
     uintprt_t        spare_hook6;
     uintprt_t        spare_hook7;
    
};

ps:定义一个HTTP模块时,type字段必须设置为NGX_HTTP_MODULE;

 ngx_module_t 中的ctx必须指向ngx_http_module_t接口。

-------------------------------------------------------------------------------------------


下面来解释一下ngx_http_module_t结构,他描述了HTTP矿在在读取。重载配置是定义的8个阶段。启动过程中调用。NULL则不调用

typedef struct
{
    //解析配置文件前调用
    ngx_int_t     (*preconfiguration)(ngx_conf_t *cf);
    
    //完成配置文件的解析后调用
    ngx_int_t     (*postconfiguration)(ngx_conf_t *cf);
    
    //创建数据结构用于存储只属于http{...}块的全局配置项。
    void*         (*create_main_conf)(ngx_conf_t *cf);
    
    //初始化上面这个
    char*         (*init_main_conf)(ngx_conf_t *cd,void *conf);
    
    //srv级别(server{...}快)配置项
    void*         (*create_srv_conf)(ngx_conf_t *cf);
    
    //合并main/srv级别下的同名配置项
    char*         (*merge_srv_conf)(ngx_conf_t *cd,void *prev,void* conf);
    
        
    //loc级别(location{...}快)配置项
    void*         (*create_loc_conf)(ngx_conf_t *cf);
    
    //合并loc/srv级别下的同名配置项
    char*         (*merge_srv_conf)(ngx_conf_t *cd,void *prev,void* conf);
}

ps:回调方法与内部无关,与nginx.conf配置项有关。

-------------------------------------------------------------------------------------------

这里在提一个概念,commadns数组(元素为ngx_command_t)。用于定义模块的配置文件。ngx解析配置文件中的一个配置项首先会遍历所有模块,对于一个模块而言,通过遍历commands数组进行,ngx_null_command为结束。

typedef struct ngx_command_s ngx_commadn_t;

struct ngx_command_s
{
    //配置项名称,如“gzip”
    ngx_str_t    name;
    
    //配置项类型,制定出现位置,以及可以携带的参数个数
    ngx_uint_t   type;
    
    //出现了Name中指定的配置项后,将会调用set处理配置项的参数
    char*        (*set)(ngx_conf_t *cf,ngx_command_t *cmd,void* conf);
    
    //配置文件中的偏移量
    ngx_uint_t     conf;
    
    //使用预设的解析方法解析配置项
    ngx_uint_t     offset;
    //配置项读取后的处理方法,必须是ngx_conf_post_t结构的指针
    void *post;
}
ngx_null_command为一个空的ngx_command_t.
#define ngx_null_command {ngx_null_string,0,NULL,0,0,NULL}


以上

本文出自 “剩蛋君” 博客,转载请与作者联系!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值