HTTP模块的数据结构

1.ngx_module_t

1.1数据定义

背景:

ngx_module_t结构体作为所有模块的通用接口,它只定义了init_master、init_module、init_process、init_thread、ext_thread、exit_process、exit_master这7个回调方法(事实上,init_master、init_thread、exit_thread这3个方法目前都没有使用),它们负责模块的初始化和退出,同时它们的权限也非常高

 基础解析:

*下面的ctx_index、index、spare0、spare1、spare2、spare3、version变量不需要在定义时赋值,可以用Nginx准备好的宏NGX_MODULE_V1来定义,它已经定义好了这7个值
#define NGX_MODULE_V  0,0,0,0,0,0,1

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 types;

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);



}

ctx_index:

ctx_index表示当前模块在这类模块中的序号。这个成员常常是由管理这类模块的一个Nginx核心模块设置的,对于所有的HTTP模块而言,ctx_index是由核心模块ngx_http_module设置的。ctx_index非常重要,Nginx的模块化设计非常依赖于各个模块的顺序,它们既用于表达优先级,也用于明每个模块的位置,借以帮助Nginx框架快速获得某个模块的数据

index:

当前模板块在ngx_modules数组中的序号。注意,ctx_index表示的是当前模块在一类模块中的序号,而index表示当前模块在所有模块中的序号,它同样关键。Nginx启动时会根据ngx_modules数组设置各模块的index值。

spare:

spare系列的保留变量,暂未使用

version:

//模块的版本,便于将来的扩展。目前只有一种,默认为1

ctx:

Nginx模块有许多种类,不同类模块之间的功能差别很大。例如,事件类型的模块主要处理I/O事件相关的功能,HTTP类型的模块主要处理HTTP应用层的功能。这样,每个模块都有了自己的特性,而ctx将会指向特定类型模块的公共接口

type:

ype表示该模块的类型,它与ctx指针是紧密相关的。在官方Nginx中,它的取值范围是NGX_HTTP_MODULE、NGX_CORE_MODULE、NGX_CONF_MODULE、NGX_EVENT_MODULE、NGX_MAIL_MODULE。这5种模块间的关系参考上图。实际上,还可以自定义新的模块类型

执行点:在Nginx的启动、停止过程中,以下7个函数指针表示有7个执行点会分别调用这7种方法

init_master和exit_master:虽然从字面上理解应当在master进程启动时回调init_master,但到目前为止,框架代码从来不会调用它,因此,可将init_master设为NULL。exit_master回调方法将在master进程退出前被调用

init_module:init_module回调方法在初始化所有模块时被调用。在master/worker模式下,这个阶段将在启动worker子进程前完成 

init_process和exit_process:init_process回调方法在正常服务前被调用。在master/worker模式下,多个worker子进程已经产生,在每个worker进程的初始化过程会调用所有模块的init_process函数,exit_process回调方法在服务停止前调用。在master/worker模式下,worker进程会在退出前调用它

init_thread和exit_thread:由于Nginx暂不支持多线程模式,所以init_thread在框架代码中没有被调用过,设为NULL

spare_hook(未列出):8个spare_hook变量也是保留字段,目前没有使用,但可用Nginx提供的NGX_MODULE_V1_PADDING宏来填充。看一下该宏的定义:#define NGX_MODULE_V1_PADDING 0,0,0,0,0,0,0,0

note:

1.定义一个HTTP模块时,务必把type字段设为NGX_HTTP_MODULE。

2.对于下列回调方法:init_module、init_process、exit_process、exit_master,调用它们的是Nginx的框架代码。换句话说,这4个回调方法与HTTP框架无关,即使nginx.conf中没有配置http{……}这种开启HTTP功能的配置项,这些回调方法仍然会被调用。因此,通常开发HTTP模块时都把它们设为NULL空指针

3.定义HTTP模块时,最重要的是要设置ctx和commands这两个成员。对于HTTP类型的模块来说,ngx_module_t中的ctx指针必须指向ngx_http_module_t接口(HTTP框架的要求)。

1.2解析数据:
 

1.八个ngx_module_t):

1.//解析配置文件前调用

ngx_int_t(*preconfiguration)(ngx_conf_t*cf);

2.//完成配置文件的解析后调用

ngx_int_t(*postconfiguration)(ngx_conf_t*cf);

3.*当需要创建数据结构用于存储main级别(直属于http{……}块的配置项)的全局配置项时,可以通过create_main_conf回调方法创建存储全局配置项的结构体*/

4.//常用于初始化main级别配置项

char*(*init_main_conf)(ngx_conf_t*cf,void*conf);

5.*当需要创建数据结构用于存储srv级别(直属于虚拟主机server{……}块的配置项)的配置项时,可以通过实现create_srv_conf回调方法创建存储srv级别配置项的结构体*/

void*(*create_srv_conf)(ngx_conf_t*cf)

6.//merge_srv_conf回调方法主要用于合并main级别和srv级别下的同名配置项

char*(*merge_srv_conf)(ngx_conf_t*cf,void*prev,void*conf)

7.*当需要创建数据结构用于存储loc级别(直属于location{……}块的配置项)的配置项时,可以实现create_loc_conf回调方法*/

void*(*create_loc_conf)(ngx_conf_t*cf)

8.//merge_loc_conf回调方法主要用于合并srv级别和loc级别下的同名配置项

*(*merge_loc_conf)(ngx_conf_t*cf,void*prev,void*conf);

不过,这8个阶段的调用顺序与上述定义的顺序是不同的。在Nginx启动过程中,HTTP框架调用这些回调方法的实际顺序有可能是这样的(与nginx.conf配置项有关):

1)create_main_conf
2)create_srv_conf
3)create_loc_conf
4)preconfiguration
5)init_main_conf
6)merge_srv_conf
7)merge_loc_conf
8)postconfiguration

2.commands数组

commands数组用于定义模块的配置文件参数,每一个数组元素都是ngx_command_t类型,数组的结尾用ngx_null_command表示。Nginx在解析配置文件中的一个配置项时首先会遍历所有的模块,对于每一个模块而言,即通过遍历commands数组进行,另外,在数组中检查到ngx_null_command时,会停止使用当前模块解析该配置项。每一个ngx_command_t结构体定义了自己感兴趣的一个配置项:

ngx_command_t
struct ngx_command ds{

ngx_str_t
name;

ngx_uint_t
type;

char
*(*set)(ngx_conf_t*cf,ngx_command_t*cmd,void*conf)

ngx_uint_t
conf;

ngx_uint_t
offset

void
*post;
};

官方文件(节选与用户过滤模板)

ngx_module_t  ngx_http_userid_filter_module = {
    NGX_MODULE_V1,
    &ngx_http_userid_filter_module_ctx,    /* module context */
    ngx_http_userid_commands,              /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    ngx_http_userid_init_worker,           /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
}

static ngx_http_module_t  ngx_http_userid_filter_module_ctx = {
    ngx_http_userid_add_variables,         /* preconfiguration */
    ngx_http_userid_init,                  /* postconfiguration */

    NULL,                                  /* create main configuration */
    NULL,                                  /* init main configuration */

    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */

    ngx_http_userid_create_conf,           /* create location configuration */
    ngx_http_userid_merge_conf             /* merge location configuration */
};

static ngx_command_t  ngx_http_userid_commands[] = {

    { ngx_string("userid"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_enum_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_userid_conf_t, enable),
      ngx_http_userid_state },

    { ngx_string("userid_service"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_userid_conf_t, service),
      NULL },

    { ngx_string("userid_name"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_userid_conf_t, name),
      NULL },

    { ngx_string("userid_domain"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_userid_conf_t, domain),
      &ngx_http_userid_domain_p },

    { ngx_string("userid_path"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_userid_conf_t, path),
      &ngx_http_userid_path_p },

    { ngx_string("userid_expires"),NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_http_userid_expires,
      NGX_HTTP_LOC_CONF_OFFSET,
      0,
      NULL },

    { ngx_string("userid_p3p"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_userid_conf_t, p3p),
      &ngx_http_userid_p3p_p },

    { ngx_string("userid_mark"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_http_userid_mark,
      NGX_HTTP_LOC_CONF_OFFSET,
      0,
      NULL },

      ngx_null_command
};


 


 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值