server{}块解析
2017-01-13 15:21
85 查看
server{}块解析过程:
第一部分,同样是server块内解析,包括创建server级配置存储结构、解析server{}内配置项。
与http块解析不同的有两点:一,server的ngx_http_conf_ctx_t->main_conf不是重新创建的,而是直接指向http块的ngx_http_conf_ctx_t->main_conf,因为server块内并不存在http main配置,其http main配置复用http块的;二,需要将server块加入http的servers存储结构cmcf->servers。
第二部分,为没有listen配置的server设置默认listen,否则server块将无法工作。
ngx_http_add_listen函数说明见listen配置项解析。
第一部分,同样是server块内解析,包括创建server级配置存储结构、解析server{}内配置项。
与http块解析不同的有两点:一,server的ngx_http_conf_ctx_t->main_conf不是重新创建的,而是直接指向http块的ngx_http_conf_ctx_t->main_conf,因为server块内并不存在http main配置,其http main配置复用http块的;二,需要将server块加入http的servers存储结构cmcf->servers。
第二部分,为没有listen配置的server设置默认listen,否则server块将无法工作。
static char * ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) { char *rv; void *mconf; ngx_uint_t i; ngx_conf_t pcf; ngx_http_module_t *module; struct sockaddr_in *sin; ngx_http_conf_ctx_t *ctx, *http_ctx; ngx_http_listen_opt_t lsopt; ngx_http_core_srv_conf_t *cscf, **cscfp; ngx_http_core_main_conf_t *cmcf; ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); if (ctx == NULL) { return NGX_CONF_ERROR; } /* server块配置存储结构的main_conf指向http块的main_conf */ http_ctx = cf->ctx; ctx->main_conf = http_ctx->main_conf; /* the server{}'s srv_conf */ ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); if (ctx->srv_conf == NULL) { return NGX_CONF_ERROR; } /* the server{}'s loc_conf */ ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); if (ctx->loc_conf == NULL) { return NGX_CONF_ERROR; } for (i = 0; cf->cycle->modules[i]; i++) { if (cf->cycle->modules[i]->type != NGX_HTTP_MODULE) { continue; } module = cf-& 4000 gt;cycle->modules[i]->ctx; if (module->create_srv_conf) { mconf = module->create_srv_conf(cf); if (mconf == NULL) { return NGX_CONF_ERROR; } ctx->srv_conf[cf->cycle->modules[i]->ctx_index] = mconf; } if (module->create_loc_conf) { mconf = module->create_loc_conf(cf); if (mconf == NULL) { return NGX_CONF_ERROR; } ctx->loc_conf[cf->cycle->modules[i]->ctx_index] = mconf; } } /* the server configuration context */ /*将server块加入cmcf->servers,cmcf->servers存储着所有server块*/ cscf = ctx->srv_conf[ngx_http_core_module.ctx_index]; cscf->ctx = ctx; cmcf = ctx->main_conf[ngx_http_core_module.ctx_index]; cscfp = ngx_array_push(&cmcf->servers); if (cscfp == NULL) { return NGX_CONF_ERROR; } *cscfp = cscf; /* parse inside server{} */ pcf = *cf; cf->ctx = ctx; cf->cmd_type = NGX_HTTP_SRV_CONF; rv = ngx_conf_parse(cf, NULL); *cf = pcf; /*如果没有listen配置项,设置默认listen*/ if (rv == NGX_CONF_OK && !cscf->listen) { ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t)); sin = &lsopt.u.sockaddr_in; sin->sin_family = AF_INET; #if (NGX_WIN32) sin->sin_port = htons(80); #else sin->sin_port = htons((getuid() == 0) ? 80 : 8000); #endif sin->sin_addr.s_addr = INADDR_ANY; lsopt.socklen = sizeof(struct sockaddr_in); lsopt.backlog = NGX_LISTEN_BACKLOG; lsopt.rcvbuf = -1; lsopt.sndbuf = -1; #if (NGX_HAVE_SETFIB) lsopt.setfib = -1; #endif #if (NGX_HAVE_TCP_FASTOPEN) lsopt.fastopen = -1; #endif lsopt.wildcard = 1; (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr, NGX_SOCKADDR_STRLEN, 1); if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) { return NGX_CONF_ERROR; } } return rv; }
ngx_http_add_listen函数说明见listen配置项解析。
相关文章推荐
- Tomcat的配置文件Server.xml解析
- 解析python中的wsgiserver
- tomcat解析(九)StandardServer.initialize.start
- Darwin Streaming Server中RTSP会话流程解析(Darwin流媒体服务器解析1)
- Server.MapPath() 解析
- Python模块解析之SocketServer(三)——模块思想
- Darwin Streaming Server中RTSP请求的处理过程(Darwin流媒体服务器解析2)
- php $_SERVER详细参数解析
- ZooKeeper源码解析(四):client如何和server连接
- iOS解析Server端返回JSON数据
- MS SQL SERVER日志解析和浏览
- ServerSAN解析(四):FusionStorage存储与计算分合灵活部署
- android源码解析之(九)-->SystemServer进程启动流程
- Tomcat 8(八)Tomcat解析server.xml的工具—Digester
- 关于Java中的ServerSocket类与构造服务器的解析
- Server.MapPath() 解析
- Hadoop RPC源码解析——Server类(一)
- 注册中心 Eureka 源码解析 —— Eureka-Server 启动(一)之 ServerConfig
- Kafka源码解析(一)core.kafka.server.KafkaServer
- php全局变量$_SERVER解析