static char * ngx_kqueue_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_kqueue_conf_t *kcf = conf; ngx_conf_init_uint_value(kcf->changes, 512); ngx_conf_init_uint_value(kcf->events, 512); return NGX_CONF_OK; }
static char * ngx_epoll_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_epoll_conf_t *epcf = conf; ngx_conf_init_uint_value(epcf->events, 512); ngx_conf_init_uint_value(epcf->aio_requests, 32); return NGX_CONF_OK; }
static char * ngx_devpoll_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_devpoll_conf_t *dpcf = conf; ngx_conf_init_uint_value(dpcf->changes, 32); ngx_conf_init_uint_value(dpcf->events, 32); return NGX_CONF_OK; }
static char * ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_rtsig_conf_t *rtscf = conf; /* LinuxThreads use the first 3 RT signals */ ngx_conf_init_uint_value(rtscf->signo, SIGRTMIN + 10); ngx_conf_init_uint_value(rtscf->overflow_events, 16); ngx_conf_init_uint_value(rtscf->overflow_test, 32); ngx_conf_init_uint_value(rtscf->overflow_threshold, 10); return NGX_CONF_OK; }
static char * ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_eventport_conf_t *epcf = conf; ngx_conf_init_uint_value(epcf->events, 32); return NGX_CONF_OK; }
static char * ngx_stream_core_init_main_conf(ngx_conf_t *cf, void *conf) { ngx_stream_core_main_conf_t *cmcf = conf; ngx_conf_init_uint_value(cmcf->variables_hash_max_size, 1024); ngx_conf_init_uint_value(cmcf->variables_hash_bucket_size, 64); cmcf->variables_hash_bucket_size = ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size); if (cmcf->ncaptures) { cmcf->ncaptures = (cmcf->ncaptures + 1) * 3; } return NGX_CONF_OK; }
static char * ngx_pool_context_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_pool_context_conf_t *pcf = conf; ngx_conf_init_uint_value(pcf->size, cycle->connection_n); ngx_pool_context_hash_size = pcf->size; ngx_pool_context_hash = ngx_pcalloc(cycle->pool, sizeof(ngx_pool_context_node_t *) * ngx_pool_context_hash_size); if (ngx_pool_context_hash == NULL) { return NGX_CONF_ERROR; } return NGX_CONF_OK; }
static ngx_int_t ngx_http_upstream_init_keepalive(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) { ngx_uint_t i; ngx_http_upstream_keepalive_srv_conf_t *kcf; ngx_http_upstream_keepalive_cache_t *cached; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "init keepalive"); kcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_keepalive_module); ngx_conf_init_msec_value(kcf->timeout, 60000); ngx_conf_init_uint_value(kcf->requests, 100); if (kcf->original_init_upstream(cf, us) != NGX_OK) { return NGX_ERROR; } kcf->original_init_peer = us->peer.init; us->peer.init = ngx_http_upstream_init_keepalive_peer; /* allocate cache items and add to free queue */ cached = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_keepalive_cache_t) * kcf->max_cached); if (cached == NULL) { return NGX_ERROR; } ngx_queue_init(&kcf->cache); ngx_queue_init(&kcf->free); for (i = 0; i < kcf->max_cached; i++) { ngx_queue_insert_head(&kcf->free, &cached[i].queue); cached[i].conf = kcf; } return NGX_OK; }
static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) int fd; #endif #if (NGX_HAVE_RTSIG) ngx_uint_t rtsig; ngx_core_conf_t *ccf; #endif ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); if (fd != -1) { (void) close(fd); module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { module = &ngx_epoll_module; } #endif #if (NGX_HAVE_RTSIG) if (module == NULL) { module = &ngx_rtsig_module; rtsig = 1; } else { rtsig = 0; } #endif #if (NGX_HAVE_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif #if (NGX_HAVE_SELECT) if (module == NULL) { module = &ngx_select_module; } #endif if (module == NULL) { for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = ngx_modules[i]->ctx; if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) { continue; } module = ngx_modules[i]; break; } } if (module == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); cycle->connection_n = ecf->connections; ngx_conf_init_uint_value(ecf->use, module->ctx_index); event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); ngx_conf_init_value(ecf->multi_accept, 0); ngx_conf_init_value(ecf->accept_mutex, 1); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); #if (NGX_HAVE_RTSIG) if (!rtsig) { return NGX_CONF_OK; } if (ecf->accept_mutex) { return NGX_CONF_OK; } ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); if (ccf->worker_processes == 0) { return NGX_CONF_OK; } ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "the \"rtsig\" method requires \"accept_mutex\" to be on"); return NGX_CONF_ERROR; #else return NGX_CONF_OK; #endif }
// 所有模块配置解析完毕后,对配置进行初始化 // 如果有的指令没有写,就要给正确的默认值 // 模块默认使用epoll // 默认不接受多个请求,也就是一次只accept一个连接 // 默认使用负载均衡锁 static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) int fd; #endif // rtsig在nginx 1.9.x已经删除 ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; // 测试epoll是否可用 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); // epoll调用可用,那么模块默认使用epoll if (fd != -1) { (void) close(fd); // epoll调用可用,那么模块默认使用epoll module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { // epoll调用可用,那么模块默认使用epoll module = &ngx_epoll_module; } #endif // rtsig在nginx 1.9.x已经删除 #if (NGX_HAVE_DEVPOLL) && !(NGX_TEST_BUILD_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif // 如果epoll不可用,那么默认使用select #if (NGX_HAVE_SELECT) if (module == NULL) { module = &ngx_select_module; } #endif // 还没有决定默认的事件模型 if (module == NULL) { // 遍历所有的事件模块 for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = cycle->modules[i]->ctx; // 跳过event_core模块 if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) { continue; } // 使用数组里的第一个事件模块 module = cycle->modules[i]; break; } } // 最后还没有决定默认的事件模型,出错 if (module == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } // nginx每个进程可使用的连接数量,即cycle里的连接池大小 ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); // 如果没有使用worker_connections指令,在这里设置 cycle->connection_n = ecf->connections; // 决定使用的事件模型,之前的module只作为默认值,如果已经使用了use则无效 ngx_conf_init_uint_value(ecf->use, module->ctx_index); // 初始化使用的事件模块的名字 event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); // 默认不接受多个请求,也就是一次只accept一个连接 ngx_conf_init_value(ecf->multi_accept, 0); // 默认使用负载均衡锁 ngx_conf_init_value(ecf->accept_mutex, 1); // 默认负载均衡锁的等待时间是500毫秒 ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); return NGX_CONF_OK; }
static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) int fd; #endif ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); if (fd != -1) { (void) close(fd); module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { module = &ngx_epoll_module; } #endif #if (NGX_HAVE_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif #if (NGX_HAVE_SELECT) if (module == NULL) { module = &ngx_select_module; } #endif //ngx_event_core_module后的第一个NGX_EVENT_MODULE也就是ngx_epoll_module默认作为第一个event模块 if (module == NULL) { for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = ngx_modules[i]->ctx; if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) //不能为ngx_event_core_module { continue; } module = ngx_modules[i]; break; } } if (module == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); cycle->connection_n = ecf->connections; ngx_conf_init_uint_value(ecf->use, module->ctx_index); event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); ngx_conf_init_value(ecf->multi_accept, 0); ngx_conf_init_value(ecf->accept_mutex, 1); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); return NGX_CONF_OK; }