cycle = ngx_init_cycle(&init_cycle):
1、在內存池中分配一個ngx_cycle_t變量,并初始化它的各個成員。
2、調用core類型的create_conf,實際只有一個ngx_core_module_create_conf函數----初始化ngx_core_conf_t結構(存放core_module支持的指令),保存在ngx_cycle->conf_ctx數組中。可以說,此時,ngx_cycle->conf_ctx數組中只有一個ngx_core_conf_t結構。
3、初始化ngx_conf_t結構。
4、ngx_conf_parse 解析配置文件,把結果保存在模塊對應的ngx_conf里面。
5、調用core類型的init_conf,實際只有一個ngx_core_module_init_conf函數(初始化對應的ngx_core_conf_t函數)。為什么要init,都已經解析配置文件了,應該在這之前初始化呀--如果值為-1,表明沒有設置,初始化默認值!
6、ngx_open_listening_sockets:遍歷listening數組并打開所有偵聽sockets(socket()->setsockopt()->bind()->listen())。
7、調用所有模塊的init_module(實際上只有ngx_event_core_module模塊定義了該callback,即只有ngx_event_module_init()被調用)。
ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename):
函數的作用就是循環不停的從配置文件中讀取指令,然后進行指令處理,直到結束
1、先分析ngx_core_module的指令及其對應的set函數。
{ ngx_string("daemon"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_core_conf_t, daemon),
//計算daemon成員在ngx_core_conf_t結構體里面的偏移
NULL },
ngx_conf_set_flag_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf):里面代碼很清楚,根據配置指令,設置模塊conf結構的成員變量。
2、分析ngx_events_modules的指令及其對應的set函數。只有一條指令:
{ ngx_string("events"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_events_block,
0,
0,
NULL },
1、ngx_events_module編號為3,于是ngx_cycle->conf_ctx指向的void*數組第3號槽位指向一個void*指針,這個指針又指向一個void*數組(個數==事件類型模塊的個數,Linux平臺編譯之后,只有兩個ngx_epoll_module事件模型ngx_event_core_module和ngx_epoll_module)。
2、調用event類型模塊的上下文ngx_event_module_t 的create_conf鉤子,為void*數組指定槽位創建相應的conf結構。
3、更改當前cf環境(NGX_EVENT_MODULE,NGX_EVENT_CONF)解析events{ 塊里面的指令。里面的set函數都是根據配置文件設置ngx_event_conf_t結構體里面的成員變量。
3、分析ngx_http_module的指令及其對應的set函數。只有一條指令,如下:
{ ngx_string("http"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_http_block,
0,
0,
NULL },
ngx_init_cycle創建了core module的config,那么http module相關的config在那里創建呢?http module相關的config是在ngx_http_block中創建(ngx_http_conf_ctx_t)的,在ngx_http_block中會創建,初始化,合并config(未完全看懂),以及整個http handler phase的初始化(還未看)等等。
新聞熱點
疑難解答