在此稍微记下主框架代码和各个模块的基本交互方式.
首先在nginx.c文件中的main函数
点击(此处)折叠或打开
-
ngx_max_module = 0;
-
for (i = 0; ngx_modules[i]; i++) {
-
ngx_modules[i]->index = ngx_max_module++;
- }
接下来调用ngx_init_cycle.
ngx_cycle.c文件中ngx_init_cycle函数
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
-
continue;
-
}
-
-
module = ngx_modules[i]->ctx;
-
-
if (module->create_conf) {
-
rv = module->create_conf(cycle);
-
if (rv == NULL) {
-
ngx_destroy_pool(pool);
-
return NULL;
-
}
-
cycle->conf_ctx[ngx_modules[i]->index] = rv;
-
}
- }
点击(此处)折叠或打开
-
if (ngx_conf_param(&conf) != NGX_CONF_OK) {
-
environ = senv;
-
ngx_destroy_cycle_pools(&conf);
-
return NULL;
- }
点击(此处)折叠或打开
-
if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
-
environ = senv;
-
ngx_destroy_cycle_pools(&conf);
-
return NULL;
- }
点击(此处)折叠或打开
-
static ngx_command_t ngx_events_commands[] = {
-
-
{ ngx_string("events"),
-
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
-
ngx_events_block,
-
0,
-
0,
-
NULL },
-
-
ngx_null_command
- };
ngx_event.c文件ngx_events_block函数:
点击(此处)折叠或打开
-
/* count the number of the event modules and set up their indices */
-
-
ngx_event_max_module = 0;
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
-
continue;
-
}
-
-
ngx_modules[i]->ctx_index = ngx_event_max_module++;
- }
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
-
continue;
-
}
-
-
m = ngx_modules[i]->ctx;
-
-
if (m->create_conf) {
-
(*ctx)[ngx_modules[i]->ctx_index] = m->create_conf(cf->cycle);
-
if ((*ctx)[ngx_modules[i]->ctx_index] == NULL) {
-
return NGX_CONF_ERROR;
-
}
-
}
- }
接下来会解析events配置块中的所有配置项.
接着再调用所有事件模块的init_conf方法用默认值来初始化那些在配置文件中没有配置的配置项:
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
-
continue;
-
}
-
-
m = ngx_modules[i]->ctx;
-
-
if (m->init_conf) {
-
rv = m->init_conf(cf->cycle, (*ctx)[ngx_modules[i]->ctx_index]);
-
if (rv != NGX_CONF_OK) {
-
return rv;
-
}
-
}
- }
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
-
continue;
-
}
-
-
m = ngx_modules[i]->ctx;
-
-
if (m->init_conf) {
-
rv = m->init_conf(cf->cycle, (*ctx)[ngx_modules[i]->ctx_index]);
-
if (rv != NGX_CONF_OK) {
-
return rv;
-
}
-
}
- }
接下来轮到核心模块来初始化配置项了:
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
-
continue;
-
}
-
-
module = ngx_modules[i]->ctx;
-
-
if (module->init_conf) {
-
if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
-
== NGX_CONF_ERROR)
-
{
-
environ = senv;
-
ngx_destroy_cycle_pools(&conf);
-
return NULL;
-
}
-
}
- }
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->init_module) {
-
if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
-
/* fatal */
-
exit(1);
-
}
-
}
- }
ngx_init_conf函数结束. 回到main函数.
mian--->ngx_master_process_cycle-->ngx_start_worker_processes-->ngx_spawn_process-->ngx_worker_process_cycle-->ngx_worker_process_init
点击(此处)折叠或打开
-
for (i = 0; ngx_modules[i]; i++) {
-
if (ngx_modules[i]->init_process) {
-
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
-
/* fatal */
-
exit(2);
-
}
-
}
- }
以上就是nginx主框架和模块之间的交互. 至于模块nginx这个框架中具体是怎么运转的. 需要
看event模块, nginx使用事件驱动模型. 其他的应用模块都依靠event模块来带动. 待仔细看完代码再
总结.