main
|
|__>ngx_init_cycle
|
|__>ngx_conf_parse
ngx_cycle_t *
ngx_init_cycle(ngx_cycle_t *old_cycle){
…………
ngx_conf_t conf;//保存解析后的每项配置
…………
for (i = 0; cycle->modules[i]; i++) {
if (cycle->modules[i]->type != NGX_CORE_MODULE) {
continue;
}
//ngx_module_s结构初始化时将一个ngx_core_module_t的全局变量指针赋值给ctx
//ngx_core_module_t包含回调函数create_conf
module = cycle->modules[i]->ctx;
if (module->create_conf) {
//create_conf回调函数会返回一个palloc分配的结构指针
//该结构就是模块的配置结构如ngx_core_conf_t
rv = module->create_conf(cycle);
if (rv == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
//将模块的配置结构复制到全局的cyccle中
cycle->conf_ctx[cycle->modules[i]->index] = rv;
}
}
…………
//将module结构赋值给conf,建立module与conf的关联
conf.ctx = cycle->conf_ctx;
conf.cycle = cycle;
conf.pool = pool;
conf.log = log;
conf.module_type = NGX_CORE_MODULE;
conf.cmd_type = NGX_MAIN_CONF;
…………
//读取配置
if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
environ = senv;
ngx_destroy_cycle_pools(&conf);
return NULL;
}
}
ngx_conf_s
|
|__>ctx
|
|__>ngx_cycle_s
| |
| |__conf_ctx[]<___________________
| | |
| |__ngx_module_t[] |
| | |
| |__ctx |
| | |
|__>create_conf()________|
配置解析
ngx_conf_parse
|
|__>ngx_conf_read_token//解析关键字
|
|__>ngx_conf_handler//通过set回调设置cf->handler
|
|__>cf->handler//解析命令
char *
ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename){
…………
//读取配置文件
fd = ngx_open_file(filename->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
…………
//循环解析配置
//解析配置文件
for ( ;; ) {
rc = ngx_conf_read_token(cf);
…………
//cf->handler 回调函数
//用于解析具体的命令,该命令就是ngx_command_s中的name
//通过ngx_command_s中的set设置该回调函数
if (cf->handler) {
…………
//解析ngx_command_s项配置
rv = (*cf->handler)(cf, NULL, cf->handler_conf);
…………
}
…………
//在该函数中调用ngx_command_s中的set设置该回调函数
rc = ngx_conf_handler(cf, rc);
…………
}
}
static ngx_int_t
ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last){
…………
//ngx_command_t结构中的set回调函数
//http模块的set就是ngx_http_block
rv = cmd->set(cf, cmd, conf);
…………
}