staticchar*ngx_http_block(ngx_conf_t*cf,ngx_command_t*cmd,void*conf){char*rv;ngx_uint_tmi,m,s;ngx_conf_tpcf;ngx_http_module_t*module;ngx_http_conf_ctx_t*ctx;ngx_http_core_loc_conf_t*clcf;ngx_http_core_srv_conf_t**cscfp;ngx_http_core_main_conf_t*cmcf;/* the main http context */ctx=ngx_pcalloc(cf->pool,sizeof(ngx_http_conf_ctx_t));if(ctx==NULL){returnNGX_CONF_ERROR;}*(ngx_http_conf_ctx_t**)conf=ctx;/* 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++;}....for(m=0;ngx_modules[m];m++){if(ngx_modules[m]->type!=NGX_HTTP_MODULE){continue;}module=ngx_modules[m]->ctx;if(module->postconfiguration){if(module->postconfiguration(cf)!=NGX_OK){//调用模块的init函数returnNGX_CONF_ERROR;}}}....if(ngx_http_init_phase_handlers(cf,cmcf)!=NGX_OK){//关键returnNGX_CONF_ERROR;}....}staticngx_int_tngx_http_init_phase_handlers(ngx_conf_t*cf,ngx_http_core_main_conf_t*cmcf){ngx_int_tj;ngx_uint_ti,n;ngx_uint_tfind_config_index,use_rewrite,use_access;ngx_http_handler_pt*h;ngx_http_phase_handler_t*ph;ngx_http_phase_handler_ptchecker;cmcf->phase_engine.server_rewrite_index=(ngx_uint_t)-1;cmcf->phase_engine.location_rewrite_index=(ngx_uint_t)-1;find_config_index=0;use_rewrite=cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts?1:0;use_access=cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts?1:0;n=use_rewrite+use_access+cmcf->try_files+1/* find config phase */;for(i=0;i<NGX_HTTP_LOG_PHASE;i++){n+=cmcf->phases[i].handlers.nelts;}ph=ngx_pcalloc(cf->pool,n*sizeof(ngx_http_phase_handler_t)+sizeof(void*));if(ph==NULL){returnNGX_ERROR;}cmcf->phase_engine.handlers=ph;n=0;for(i=0;i<NGX_HTTP_LOG_PHASE;i++){h=cmcf->phases[i].handlers.elts;switch(i){caseNGX_HTTP_SERVER_REWRITE_PHASE:
if(cmcf->phase_engine.server_rewrite_index==(ngx_uint_t)-1){cmcf->phase_engine.server_rewrite_index=n;}checker=ngx_http_core_rewrite_phase;break;/* 其他的一些处理 */...}n+=cmcf->phases[i].handlers.nelts;/* 从后面开始... */for(j=cmcf->phases[i].handlers.nelts-1;j>=0;j--){ph->checker=checker;ph->handler=h[j];ph->next=n;ph++;}}returnNGX_OK;}