在这一节我们将会对通常的模块开发过程中,每个模块所包含的一些常用的部分进行说明。这些部分有些是必须的,有些不是必须的。同时这里所列出的这些东西对于其他类型的模块,例如 filter 模块等也都是相同的。
基本上每个模块都会提供一些配置指令,以便于用户可以通过配置来控制该模块的行为。那么这些配置信息怎么存储呢?那就需要定义该模块的配置结构来进行存储。
大家都知道 nginx 的配置信息分成了几个作用域(scope,有时也称作上下文),这就是 main,server 以及 location。同样的每个模块提供的配置指令也可以出现在这几个作用域里。那对于这三个作用域的配置信息,每个模块就需要定义三个不同的数据结构去进行存储。当然,不是每个模块都会在这三个作用域都提供配置指令的。那么也就不一定每个模块都需要定义三个数据结构去存储这些配置信息了。视模块的实现而言,需要几个就定义几个。
有一点需要特别注意的就是,在模块的开发过程中,我们最好使用 nginx 原有的命名习惯。这样跟原代码的契合度更高,看起来也更舒服。
对于模块配置信息的定义,命名习惯是ngx_http_<module name>_(main|srv|loc)_conf_t
。这里有个例子,就是从我们后面将要展示给大家的 hello module 中截取的。
typedef struct
{
ngx_str_t hello_string;
ngx_int_t hello_counter;
}ngx_http_hello_loc_conf_t;
一个模块的配置指令是定义在一个静态数组中的。同样地,我们来看一下从 hello module 中截取的模块配置指令的定义。
static ngx_command_t ngx_http_hello_commands[] = {
{
ngx_string("hello_string"),
ngx_http_loc_conf|ngx_conf_noargs|ngx_conf_take1,
ngx_http_hello_string,
ngx_http_loc_conf_offset,
offsetof(ngx_http_hello_loc_conf_t, hello_string),
null },
{
ngx_string("hello_counter"),
ngx_http_loc_conf|ngx_conf_flag,
ngx_http_hello_counter,
ngx_http_loc_conf_offset,
offsetof(ngx_http_hello_loc_conf_t, hello_counter),
null },
ngx_null_command
};
其实看这个定义,就基本能看出来一些信息。例如,我们是定义了两个配置指令,一个是叫 hello_string,可以接受一个参数,或者是没有参数。另外一个命令是 hello_counter,接受一个 ngx_conf_flag 类型的参数。除此之外,似乎看起来有点迷惑。没有关系,我们来详细看一下 ngx_command_t,一旦我们了解这个结构的详细信息,那么我相信上述这个定义所表达的所有信息就不言自明了。
ngx_command_t 的定义,位于src/core/ngx_conf_file.h
中。
struct ngx_command_s {
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;
};
name: 配置指令的名称。
type: 该配置的类型,其实更准确一点说,是该配置指令属性的集合。nginx 提供了很多预定义的属性值(一些宏定义),通过逻辑或运算符可组合在一起,形成对这个配置指令的详细的说明。下面列出可在这里使用的预定义属性值及说明。
可以组合多个属性,比如一个指令即可以不填参数,也可以接受1个或者2个参数。那么就是ngx_conf_noargs|ngx_conf_take1|ngx_conf_take2
。如果写上面三个属性在一起,你觉得麻烦,那么没有关系,nginx 提供了一些定义,使用起来更简洁。
最后要说明的是,无论如何,nginx 的配置指令的参数个数不可以超过 ngx_conf_max_args 个。目前这个值被定义为 8,也就是不能超过 8 个参数值。
下面介绍一组说明配置指令可以出现的位置的属性。
对于我们编写的大多数模块而言,都是在处理http相关的事情,也就是所谓的都是ngx_http_module,对于这样类型的模块,其配置可能出现的位置也是分为直接出现在http里面,以及其他位置。
set: 这是一个函数指针,当 nginx 在解析配置的时候,如果遇到这个配置指令,将会把读取到的值传递给这个函数进行分解处理。因为具体每个配置指令的值如何处理,只有定义这个配置指令的人是最清楚的。来看一下这个函数指针要求的函数原型。
char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
先看该函数的返回值,处理成功时,返回 ngx_ok,否则返回 ngx_conf_error 或者是一个自定义的错误信息的字符串。
再看一下这个函数被调用的时候,传入的三个参数。
cf: 该参数里面保存从配置文件读取到的原始字符串以及相关的一些信息。特别注意的是这个参数的args字段是一个 ngx_str_t类型的数组,该数组的首个元素是这个配置指令本身,第二个元素是指令的第一个参数,第三个元素是第二个参数,依次类推。
cmd: 这个配置指令对应的 ngx_command_t 结构。
为了更加方便的实现对配置指令参数的读取,nginx 已经默认提供了对一些标准类型的参数进行读取的函数,可以直接赋值给 set 字段使用。下面来看一下这些已经实现的 set 类型函数。
conf: 该字段被 ngx_http_module 类型模块所用 (我们编写的基本上都是 ngx_http_moudle,只有一些 nginx 核心模块是非 ngx_http_module),该字段指定当前配置项存储的内存位置。实际上是使用哪个内存池的问题。因为 http 模块对所有 http 模块所要保存的配置信息,划分了 main, server 和 location 三个地方进行存储,每个地方都有一个内存池用来分配存储这些信息的内存。这里可能的值为 ngx_http_main_conf_offset、ngx_http_srv_conf_offset 或 ngx_http_loc_conf_offset。当然也可以直接置为 0,就是 ngx_http_main_conf_offset。
offset: 指定该配置项值的精确存放位置,一般指定为某一个结构体变量的字段偏移。因为对于配置信息的存储,一般我们都是定义个结构体来存储的。那么比如我们定义了一个结构体 a,该项配置的值需要存储到该结构体的 b 字段。那么在这里就可以填写为 offsetof(a, b)。对于有些配置项,它的值不需要保存或者是需要保存到更为复杂的结构中时,这里可以设置为 0。
post: 该字段存储一个指针。可以指向任何一个在读取配置过程中需要的数据,以便于进行配置读取的处理。大多数时候,都不需要,所以简单地设为 0 即可。
看到这里,应该就比较清楚了。ngx_http_hello_commands 这个数组每 5 个元素为一组,用来描述一个配置项的所有情况。那么如果有多个配置项,只要按照需要再增加 5 个对应的元素对新的配置项进行说明。
需要注意的是,就是在ngx_http_hello_commands这个数组定义的最后,都要加一个ngx_null_command作为结尾。
这是一个 ngx_http_module_t 类型的静态变量。这个变量实际上是提供一组回调函数指针,这些函数有在创建存储配置信息的对象的函数,也有在创建前和创建后会调用的函数。这些函数都将被 nginx 在合适的时间进行调用。
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;
preconfiguration: 在创建和读取该模块的配置信息之前被调用。
postconfiguration: 在创建和读取该模块的配置信息之后被调用。
create_main_conf: 调用该函数创建本模块位于 http block 的配置信息存储结构。该函数成功的时候,返回创建的配置对象。失败的话,返回 null。
init_main_conf: 调用该函数初始化本模块位于 http block 的配置信息存储结构。该函数成功的时候,返回 ngx_conf_ok。失败的话,返回 ngx_conf_error 或错误字符串。
create_srv_conf: 调用该函数创建本模块位于 http server block 的配置信息存储结构,每个 server block 会创建一个。该函数成功的时候,返回创建的配置对象。失败的话,返回 null。
merge_srv_conf: 因为有些配置指令既可以出现在 http block,也可以出现在 http server block 中。那么遇到这种情况,每个 server 都会有自己存储结构来存储该 server 的配置,但是在这种情况下 http block 中的配置与 server block 中的配置信息发生冲突的时候,就需要调用此函数进行合并,该函数并非必须提供,当预计到绝对不会发生需要合并的情况的时候,就无需提供。当然为了安全起见还是建议提供。该函数执行成功的时候,返回 ngx_conf_ok。失败的话,返回 ngx_conf_error 或错误字符串。
create_loc_conf: 调用该函数创建本模块位于 location block 的配置信息存储结构。每个在配置中指明的 location 创建一个。该函数执行成功,返回创建的配置对象。失败的话,返回 null。
nginx 里面的配置信息都是上下一层层的嵌套的,对于具体某个 location 的话,对于同一个配置,如果当前层次没有定义,那么就使用上层的配置,否则使用当前层次的配置。
这些配置信息一般默认都应该设为一个未初始化的值,针对这个需求,nginx 定义了一系列的宏定义来代表各种配置所对应数据类型的未初始化值,如下:
#define ngx_conf_unset -1
#define ngx_conf_unset_uint (ngx_uint_t) -1
#define ngx_conf_unset_ptr (void *) -1
#define ngx_conf_unset_size (size_t) -1
#define ngx_conf_unset_msec (ngx_msec_t) -1
又因为对于配置项的合并,逻辑都类似,也就是前面已经说过的,如果在本层次已经配置了,也就是配置项的值已经被读取进来了(那么这些配置项的值就不会等于上面已经定义的那些 unset 的值),就使用本层次的值作为定义合并的结果,否则,使用上层的值,如果上层的值也是这些unset类的值,那就赋值为默认值,否则就使用上层的值作为合并的结果。对于这样类似的操作,nginx 定义了一些宏操作来做这些事情,我们来看其中一个的定义。
#define ngx_conf_merge_uint_value(conf, prev, default) \
if (conf == ngx_conf_unset_uint) { \
conf = (prev == ngx_conf_unset_uint) ? default : prev; \
}
显而易见,这个逻辑确实比较简单,所以其它的宏定义也类似,我们就列具其中的一部分吧。
ngx_conf_merge_value
ngx_conf_merge_ptr_value
ngx_conf_merge_uint_value
ngx_conf_merge_msec_value
ngx_conf_merge_sec_value
等等。
下面来看一下 hello 模块的模块上下文的定义,加深一下印象。
static ngx_http_module_t ngx_http_hello_module_ctx = {
null, /* preconfiguration */
ngx_http_hello_init, /* postconfiguration */
null, /* create main configuration */
null, /* init main configuration */
null, /* create server configuration */
null, /* merge server configuration */
ngx_http_hello_create_loc_conf, /* create location configuration */
null /* merge location configuration */
};
注意:这里并没有提供 merge_loc_conf 函数,因为我们这个模块的配置指令已经确定只出现在 ngx_http_loc_conf 中这一个层次上,不会发生需要合并的情况。
对于开发一个模块来说,我们都需要定义一个 ngx_module_t 类型的变量来说明这个模块本身的信息,从某种意义上来说,这是这个模块最重要的一个信息,它告诉了 nginx 这个模块的一些信息,上面定义的配置信息,还有模块上下文信息,都是通过这个结构来告诉 nginx 系统的,也就是加载模块的上层代码,都需要通过定义的这个结构,来获取这些信息。
我们先来看下 ngx_module_t 的定义
typedef struct ngx_module_s ngx_module_t;
struct ngx_module_s {
ngx_uint_t ctx_index;
ngx_uint_t index;
ngx_uint_t spare0;
ngx_uint_t spare1;
ngx_uint_t abi_compatibility;
ngx_uint_t major_version;
ngx_uint_t minor_version;
void *ctx;
ngx_command_t *commands;
ngx_uint_t type;
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);
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;
};
#define ngx_number_major 3
#define ngx_number_minor 1
#define ngx_module_v1 0, 0, 0, 0, \
ngx_dso_abi_compatibility, ngx_number_major, ngx_number_minor
#define ngx_module_v1_padding 0, 0, 0, 0, 0, 0, 0, 0
再看一下 hello 模块的模块定义。
ngx_module_t ngx_http_hello_module = {
ngx_module_v1,
&ngx_http_hello_module_ctx, /* module context */
ngx_http_hello_commands, /* module directives */
ngx_http_module, /* module type */
null, /* init master */
null, /* init module */
null, /* init process */
null, /* init thread */
null, /* exit thread */
null, /* exit process */
null, /* exit master */
ngx_module_v1_padding
};
模块可以提供一些回调函数给 nginx,当 nginx 在创建进程线程或者结束进程线程时进行调用。但大多数模块在这些时刻并不需要做什么,所以都简单赋值为 null。