您的位置:首页 > 运维架构 > Nginx

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块将无法工作。

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配置项解析。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx源码分析