【Nginx】http模块的数据结构

定义fttp模块方式很简单,比如:ngx_module_t ngx_http_mytest_module;

其中,ngx_module_t是一个Nginx模块的数据结构。

typedef struct ngx_module_s ngx_module_t;
struct ngx_module_s
{
    #define NGX_MODULE_V1;//下面七个变量不需要在定义时赋值,由该宏来定义
    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;//指向特定类型模块的公共接口,在http模块中,ctx需要指向ngx_http_module_t结构体
    ngx_command_t *commands;//处理nginx.conf中的配置项
    ngx_uint_t type;//该模块的类型
    //...
};

定义一个http模块时,务必把type字段设为NGX_HTTP_MODULE

定义http模块时,最重要的是要设置ctx和commands两个成员。对于http类型的模块来说,ngx_module_t中的ctx指针必须指向ngx_http_module_t接口。

http框架在读取、重载配置文件时定义了由ngx_http_module_t接口描述的8个阶段,http框架在启动过程中会在每个阶段中调用ngx_http_module_t中相应的方法,如果ngx_http_module_t中的某个回调方法设为NULL,那么HTTP框架是不会调用它的。

commands数组用于定义模块的配置文件参数,每一个数组元素都是ngx_command_t类型,数组的结尾用ngx_null_command表示。

typedef struct ngx_command_s ngx_command_t;
struct ngx_command_s
{
    ngx_str_t name;//配置项名称
    ngx_uint_t type;//配置项类型
    char *(*set)(ngx_conf_t *cf,ngx_command_t *cmd,void *conf);//处理name对应配置项参数
    ngx_uint_t conf;
    ngx_uint_t offset;
    void *post;//配置项读取后的处理方法
};
#define ngx_null_command {ngx_null_string,0,null,0,0,null}

 

posted @ 2014-07-05 20:32  合唱团abc  阅读(602)  评论(0编辑  收藏  举报