HTTP- TCP连接建立过程

 
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;
        }
        }


        ngx_conf_parse(&conf, &cycle->conf_file) 


        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);
                }
            }
        }    
-------------------------------------------------------        
    -------------------------从core--到 event 类型  是通过 core 解析events 字段来确定---------------
    conf_parse的时候 解析到event 回调用 ngx_event_block 函数 处理event 
    ------------------------------------
        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;
                }
            }
        }
    
    -----------------------------
        -------------------------从core--到 http 类型  是通过 core 解析 http 字段来确定---------------
        也就解析配置http 的时候会调用 ngx_http_block
         /* the main http context */
        ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
        if (ctx == NULL) {
            return NGX_CONF_ERROR;
        }

        //conf为ngx_conf_handler中的conf = confp[ngx_modules[i]->ctx_index];也就是conf指向的是ngx_cycle_s->conf_ctx[],
        //所以对conf赋值就是对ngx_cycle_s中的conf_ctx赋值
        *(ngx_http_conf_ctx_t **) conf = ctx; //图形化参考:深入理解NGINX中的图9-2  图10-1  图4-2,结合图看,并可以配合http://tech.uc.cn/?p=300看

        /* count the number of the http modules and set up their indices */

        ngx_http_max_module = 0;
        for (m = 0; ngx_modules[m]; m++) {
            if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
                continue;
            }

            ngx_modules[m]->ctx_index = ngx_http_max_module++;  //二级类型按照在ngx_modules中的顺序排序
        }

        /* the http main_conf context, it is the same in the all http contexts */

        ctx->main_conf = ngx_pcalloc(cf->pool,
                                     sizeof(void *) * ngx_http_max_module);
        ngx_http_init_phase_handlers
        ngx_http_optimize_servers(cf, cmcf, cmcf->ports) 根据server 中的 port ip  设置listen 信息
        ---------------------------------------------------------------

        多进程开始运行:
ngx_worker_process_cycle    
        ngx_worker_process_init

        for {

            ngx_process_events_and_timers(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) { //ngx_event_process_init等
                    /* fatal */
                    exit(2); 
            } 
        }
        init_process {
            module = ngx_modules[m]->ctx;

            if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) { //执行epoll module中的ngx_epoll_init
                }// create——add  listen fd
        }

 

 

  •  在Nginx main函数的ngx_init_cycle()方法中,解析配置的时候  调用 HTTP模块通过ngx_http_block()方法进行初始化,然后调用ngx_http_optimize_servers()进行套接字的创建和初始化(ngx_http_init_listening、ngx_http_add_listening、ngx_create_listening)。根据每一个IP地址:port这种配置初始化监听套接字信息。
  • ngx_http_add_listening函数,还会将ls->handler监听套接字的回调函数设置为ngx_http_init_connection。ngx_http_init_connection此函数主要初始化一个客户端连接connection。

 

 

 按照 上述 1-10 的顺序执行:

ngx 的核心流程 

  • init 初始化 
  • 根据初始化结果 选择 epoll/poll init
  • I/O 复用结束后 执行 poroess event

所以在 int_cycle 中比较复杂!!

 cycle = ngx_init_cycle(&init_cycle);
ngx_modules[i]->init_process(cycle)
for{
  ngx_process_events_and_timers  
}

 

posted @ 2020-09-27 10:29  codestacklinuxer  阅读(180)  评论(0编辑  收藏  举报