http://blog.csdn.net/benbendy1984/article/details/6003779
零零碎碎的看了nginx的源码,自己感觉还是比较的乱,所以花点时间把它的整体结构给理下,

一 模块的结构(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 spare2;
- ngx_uint_t spare3;
- ngx_uint_t 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;
- };
index 是指模块的下标,系统在初始化的是,给了每个模块一个唯一id,就好比每个学生都有一个学号,每个员工都有一个工号,下标从0开始。
ctx 是模块的上下文,不同的模块具有不同的上下文,这个具体在第二部分介绍
command 处理指令,在配置文件中的每条指令,在模块中都有对应的处理,因为每个模块可以有多个指令,所以这里可以存放多个指令处理的指令
其它暂时不介绍。
二 模块上下文(ngx_xxx_module_ctx)
这里的xxx表示模块的类型(core、 event 、http、email)
name 模块上下文的名字
ngx_xxx_module_create_conf 模块配置的创建函数,这个函数创建的模块配置需要保存在 conf_ctx中的,那么放到那里呢?就是通过模块的index来定位的: cycle->conf_ctx[ngx_modules[i]->index] = rv; (ngx_cycle.c: ngx_init_cycle()函数)
ngx_xxx_module_init_conf 模块配置的初始化函数
注意:不是每个模块都有上面的函数的,有些模块(event, http) 没有配置信息,那就不用这两个函数的。
三 模块的配置 conf_ctx
这里面只是存放了指向各个模块conf的指针,各个模块的指针根据他们的index 有序存放。
系统的配置结果 ngx_conf_t 和cycle 都有成员指向这个结构,这样便于获取模块的配置信息
四 模块指令(ngx_command)
- 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 模块的类型,这个比较的复杂,我还没有看懂
set(cf, cmd ,conf) 设置的函数,所有工作都是在这个函数中完成的,不同模块的不同指令,都有不同的函数
conf 这个是定位到那个被赋值模块的配置,但是不是所有的模块会使用这个变量的,一般的模块直接通过index 就定位到了的,具体是什么样的情况使用这个,还需要深入的看下。
offset 这个是被赋值模块的配置内部的下标。
post 不知知道
总的来说,模块指令完成类似下面的工作 *(config + offset) = cf->args ,对找到模块内的制定位置赋值。
五 全局数据(ngx_cycle_t)
这个保存了系统的全局数据,在整个程序执行过程中,都会使用到
- struct ngx_cycle_s {
- void ****conf_ctx;
- ngx_pool_t *pool;
- ngx_log_t *log;
- ngx_log_t new_log;
- ngx_connection_t **files;
- ngx_connection_t *free_connections;
- ngx_uint_t free_connection_n;
- ngx_array_t listening;
- ngx_array_t pathes;
- ngx_list_t open_files;
- ngx_list_t shared_memory;
- ngx_uint_t connection_n;
- ngx_uint_t files_n;
- ngx_connection_t *connections;
- ngx_event_t *read_events;
- ngx_event_t *write_events;
- ngx_cycle_t *old_cycle;
- ngx_str_t conf_file;
- ngx_str_t conf_param;
- ngx_str_t conf_prefix;
- ngx_str_t prefix;
- ngx_str_t lock_file;
- ngx_str_t hostname;
- };
conf_ctx 指向的就是模块的配置信息
六 指令参数 ngx_conf_t
这里存放了从配置文件中读取的,需要传给模块配置信息的数据。
- struct ngx_conf_s {
- char *name;
- ngx_array_t *args;
- ngx_cycle_t *cycle;
- ngx_pool_t *pool;
- ngx_pool_t *temp_pool;
- ngx_conf_file_t *conf_file;
- ngx_log_t *log;
- void *ctx;
- ngx_uint_t module_type;
- ngx_uint_t cmd_type;
- ngx_conf_handler_pt handler;
- char *handler_conf;
- };