全面了解Nginx中的HTTP协议相关模块配置

2019-10-17 19:25:04王振洲

与 server 块解析函数 ngx_http_core_server 类似,他创建了所有模块的 loc_conf,为了防止内外层具有相同指令,在配置赋值完成后,会通过 merge 函数合并到一起。

然而,与 server 块不同,location 块在 location 后面会通过路径或正则表达式指定 location 配置的应用 uri,因此,在 ngx_http_core_location 函数中调用 PCRE 进行了 location 命令的解析。

解析完成后调用 ngx_http_add_location 将解析结果加入到 locations 链表中。

// ngx_int_t ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
//   ngx_http_core_loc_conf_t *clcf)
// 将 location 配置加入到 locations 配置链表中 {{{
ngx_int_t
ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
  ngx_http_core_loc_conf_t *clcf)
{
  ngx_http_location_queue_t *lq;

  if (*locations == NULL) {
    *locations = ngx_palloc(cf->temp_pool,
                sizeof(ngx_http_location_queue_t));
    if (*locations == NULL) {
      return NGX_ERROR;
    }

    ngx_queue_init(*locations);
  }

  lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
  if (lq == NULL) {
    return NGX_ERROR;
  }

  if (clcf->exact_match
#if (NGX_PCRE)
    || clcf->regex
#endif
    || clcf->named || clcf->noname)
  {
    lq->exact = clcf;
    lq->inclusive = NULL;

  } else {
    lq->exact = NULL;
    lq->inclusive = clcf;
  }

  lq->name = &clcf->name;
  lq->file_name = cf->conf_file->file.name.data;
  lq->line = cf->conf_file->line;

  ngx_queue_init(&lq->list);

  ngx_queue_insert_tail(*locations, &lq->queue);

  return NGX_OK;
} // }}}

配置解析全部完成后的配置结构。