nginx源碼學習之一
阿新 • • 發佈:2018-07-14
har dex sin 開關 spa mman cti 基礎 commands
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 structngx_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 };
nginx源碼學習之一