nginx源码学习之一

基于1.0.0

 

function main在core/nginx文件中。

 1 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
 2         ngx_process = NGX_PROCESS_MASTER;
 3 }
 4 
 5 if (ngx_process == NGX_PROCESS_SINGLE) {
 6     ngx_single_process_cycle(cycle);
 7 
 8 } else {
 9     ngx_master_process_cycle(cycle);
10 }
11 
12 #define NGX_PROCESS_SINGLE     0

若 master_process 开关关闭,main进入ngx_single_process_cycle,其中ngx_process为全局变量,默认为0。

在此之前,nginx做了一些基础工作,更重要的基础工作由core/ngx_cycle.c:ngx_init_cycle完成。

ngx_modules数组来自于编译生成的obj/ngx_modules.c文件,该数组定义了将要编译到目标二进制文件里的module。

 

nginx由模块组成,一个模块对外暴露两个结构体:ngx_command_t和ngx_module_t。

 1 struct ngx_command_s {
 2     ngx_str_t             name;
 3     ngx_uint_t            type;
 4     char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 5     ngx_uint_t            conf;
 6     ngx_uint_t            offset;
 7     void                 *post;
 8 };
 9 
10 
11 struct ngx_module_s {
12     ngx_uint_t            ctx_index;
13     ngx_uint_t            index;
14 
15     ngx_uint_t            spare0;
16     ngx_uint_t            spare1;
17     ngx_uint_t            spare2;
18     ngx_uint_t            spare3;
19 
20     ngx_uint_t            version;
21 
22     void                 *ctx;
23     ngx_command_t        *commands;
24     ngx_uint_t            type;
25 
26     ngx_int_t           (*init_master)(ngx_log_t *log);
27 
28     ngx_int_t           (*init_module)(ngx_cycle_t *cycle);
29 
30     ngx_int_t           (*init_process)(ngx_cycle_t *cycle);
31     ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);
32     void                (*exit_thread)(ngx_cycle_t *cycle);
33     void                (*exit_process)(ngx_cycle_t *cycle);
34 
35     void                (*exit_master)(ngx_cycle_t *cycle);
36 
37     uintptr_t             spare_hook0;
38     uintptr_t             spare_hook1;
39     uintptr_t             spare_hook2;
40     uintptr_t             spare_hook3;
41     uintptr_t             spare_hook4;
42     uintptr_t             spare_hook5;
43     uintptr_t             spare_hook6;
44     uintptr_t             spare_hook7;
45 };

 



posted @ 2018-07-14 11:03  li-wenbo  阅读(169)  评论(0编辑  收藏  举报