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

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

static char *
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
  char            *rv;
  ngx_uint_t          mi, m, s;
  ngx_conf_t          pcf;
  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) {
    return NGX_CONF_ERROR;
  }

 // 创建 http 配置结构,保存所有 http 模块的配置信息
  *(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;
    }

 // 重新设定 HTTP 模块序号
    ngx_modules[m]->ctx_index = ngx_http_max_module++;
  }


  /* the http main_conf context, it is the same in the all http contexts */

  ctx->main_conf = ngx_pcalloc(cf->pool,
                 sizeof(void *) * ngx_http_max_module);
  if (ctx->main_conf == NULL) {
    return NGX_CONF_ERROR;
  }


  /*
   * the http null srv_conf context, it is used to merge
   * the server{}s' srv_conf's
   */

  ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
  if (ctx->srv_conf == NULL) {
    return NGX_CONF_ERROR;
  }


  /*
   * the http null loc_conf context, it is used to merge
   * the server{}s' loc_conf's
   */

  ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
  if (ctx->loc_conf == NULL) {
    return NGX_CONF_ERROR;
  }


  /*
   * create the main_conf's, the null srv_conf's, and the null loc_conf's
   * of the all http modules
   */

  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;
    mi = ngx_modules[m]->ctx_index;

    if (module->create_main_conf) {
  // 调用每个模块的 create_main_conf 回调函数
  // 创建自己的 main_conf
  //
  // ngx_http_core_module  ngx_http_core_create_main_conf
  // ngx_http_log_module  ngx_http_log_create_main_conf
  // ngx_http_upstream_module  ngx_http_upstream_create_main_conf
  // ngx_http_map_module  ngx_http_map_create_conf
  // ngx_http_ssi_filter_module ngx_http_ssi_create_main_conf
  // ngx_http_charset_filter_module ngx_http_charset_create_main_conf
      ctx->main_conf[mi] = module->create_main_conf(cf);
      if (ctx->main_conf[mi] == NULL) {
        return NGX_CONF_ERROR;
      }
    }

    if (module->create_srv_conf) {
  // 调用每个模块的 create_srv_conf 回调函数
  // 创建自己的 srv_conf
  //
  // ngx_http_core_module   ngx_http_core_create_srv_conf
  // ngx_http_ssl_module   ngx_http_ssl_create_srv_conf
  // ngx_http_upstream_hash_module ngx_http_upstream_hash_create_conf
  // ngx_http_upstream_least_conn_module ngx_http_upstream_least_conn_create_conf
  // ngx_http_upstream_keepalive_module ngx_http_upstream_keepalive_create_conf
      ctx->srv_conf[mi] = module->create_srv_conf(cf);
      if (ctx->srv_conf[mi] == NULL) {
        return NGX_CONF_ERROR;
      }
    }

    if (module->create_loc_conf) {
  // 调用每个模块的 create_loc_conf 回调函数
  // 创建自己的 loc_conf
  //
  // ngx_http_core_module  ngx_http_core_create_loc_conf
  // ngx_http_log_module  ngx_http_log_create_loc_conf
  // ngx_http_gzip_static_module ngx_http_gzip_static_create_conf
  // ngx_http_autoindex_module ngx_http_autoindex_create_loc_conf
  // ngx_http_index_module  ngx_http_index_create_loc_conf
  // ngx_http_auth_basic_module ngx_http_auth_basic_create_loc_conf
  // ngx_http_access_module  ngx_http_access_create_loc_conf
  // ngx_http_limit_conn_module ngx_http_limit_conn_create_conf
  // ngx_http_limit_req_module ngx_http_limit_req_create_conf
  // ngx_http_referer_module  ngx_http_referer_create_conf
  // ngx_http_rewrite_module  ngx_http_rewrite_create_loc_conf
  // ngx_http_proxy_module  ngx_http_proxy_create_loc_conf
  // ngx_http_fastcgi_module  ngx_http_fastcgi_create_loc_conf
  // ngx_http_uwsgi_module  ngx_http_uwsgi_create_loc_conf
  // ngx_http_scgi_module  ngx_http_scgi_create_loc_conf
  // ngx_http_memcached_module ngx_http_memcached_create_loc_conf
  // ngx_http_browser_module  ngx_http_browser_create_conf
  // ngx_http_gzip_filter_module ngx_http_gzip_create_conf
  // ngx_http_ssi_filter_module ngx_http_ssi_create_loc_conf
  // ngx_http_charset_filter_module ngx_http_charset_create_loc_conf
  // ngx_http_userid_filter_module ngx_http_userid_create_conf
  // ngx_http_headers_filter_module ngx_http_headers_create_conf
  // ngx_http_copy_filter_module ngx_http_copy_filter_create_conf
      ctx->loc_conf[mi] = module->create_loc_conf(cf);
      if (ctx->loc_conf[mi] == NULL) {
        return NGX_CONF_ERROR;
      }
    }
  }

  pcf = *cf;
  cf->ctx = ctx;

  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;

    if (module->preconfiguration) {
  // 调用每个模块的 preconfiguration 回调函数
  //
  // ngx_http_core_module  ngx_http_core_preconfiguration
  // ngx_http_upstream_module  ngx_http_upstream_add_variables
  // ngx_http_ssl_module  ngx_http_ssl_add_variables
  // ngx_http_proxy_module  ngx_http_proxy_add_variables
  // ngx_http_fastcgi_module  ngx_http_fastcgi_add_variables
  // ngx_http_browser_module  ngx_http_browser_add_variable
  // ngx_http_stub_status_module ngx_http_stub_status_add_variables
  // ngx_http_gzip_filter_module ngx_http_gzip_add_variables
  // ngx_http_ssi_filter_module ngx_http_ssi_preconfiguration
  // ngx_http_userid_filter_module ngx_http_userid_add_variables
      if (module->preconfiguration(cf) != NGX_OK) {
        return NGX_CONF_ERROR;
      }
    }
  }

  /* parse inside the http{} block */

  cf->module_type = NGX_HTTP_MODULE;
  cf->cmd_type = NGX_HTTP_MAIN_CONF;
  rv = ngx_conf_parse(cf, NULL);

  if (rv != NGX_CONF_OK) {
    goto failed;
  }

  /*
   * init http{} main_conf's, merge the server{}s' srv_conf's
   * and its location{}s' loc_conf's
   */

  cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
  cscfp = cmcf->servers.elts;

  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;
    mi = ngx_modules[m]->ctx_index;

    /* init http{} main_conf's */

    if (module->init_main_conf) {
  // 调用每个模块的 init_main_conf 回调函数
  // 初始化自己的 main_conf
  //
  // ngx_http_core_module  ngx_http_core_init_main_conf
  // ngx_http_upstream_module ngx_http_upstream_init_main_conf
  // ngx_http_ssi_filter_module ngx_http_ssi_init_main_conf
      rv = module->init_main_conf(cf, ctx->main_conf[mi]);
      if (rv != NGX_CONF_OK) {
        goto failed;
      }
    }

 // 合并同名配置项
    rv = ngx_http_merge_servers(cf, cmcf, module, mi);
    if (rv != NGX_CONF_OK) {
      goto failed;
    }
  }


  /* create location trees */

  for (s = 0; s < cmcf->servers.nelts; s++) {

    clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];

    if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {
      return NGX_CONF_ERROR;
    }

    if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
      return NGX_CONF_ERROR;
    }
  }


  if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }

  if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }


  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;

    if (module->postconfiguration) {
  // 调用每个 HTTP 模块的 postconfiguration 回调函数
  //
  // ngx_http_log_module   ngx_http_log_init
  // ngx_http_static_module  ngx_http_static_init
  // ngx_http_gzip_static_module  ngx_http_gzip_static_init
  // ngx_http_autoindex_module  ngx_http_autoindex_init
  // ngx_http_index_module  ngx_http_index_init
  // ngx_http_auth_basic_module  ngx_http_auth_basic_init
  // ngx_http_access_module  ngx_http_access_init
  // ngx_http_limit_conn_module  ngx_http_limit_conn_init
  // ngx_http_limit_req_module  ngx_http_limit_req_init
  // ngx_http_rewrite_module  ngx_http_rewrite_init
  // ngx_http_ssl_module   ngx_http_ssl_init
  // ngx_http_write_filter_module  ngx_http_write_filter_init
  // ngx_http_header_filter_module ngx_http_header_filter_init
  // ngx_http_chunked_filter_module ngx_http_chunked_filter_init
  // ngx_http_range_body_filter_module ngx_http_range_body_filter_init
  // ngx_http_gzip_filter_module  ngx_http_gzip_filter_init
  // ngx_http_postpone_filter_module ngx_http_postpone_filter_init
  // ngx_http_ssi_filter_module  ngx_http_ssi_filter_init
  // ngx_http_charset_filter_module ngx_http_charset_postconfiguration
  // ngx_http_userid_filter_module ngx_http_userid_init
  // ngx_http_headers_filter_module ngx_http_headers_filter_init
  // ngx_http_copy_filter_module  ngx_http_copy_filter_init
  // ngx_http_range_body_filter_module ngx_http_range_body_filter_init
  // ngx_http_not_modified_filter_module ngx_http_not_modified_filter_init
      if (module->postconfiguration(cf) != NGX_OK) {
        return NGX_CONF_ERROR;
      }
    }
  }

 // 初始化所有的变量
 // 不仅包括HTTP core模块的变量
 // 也包括其他的HTTP模块导出的变量,以及配置文件中使用 set 命令设置的变量
 // 这里的初始化包括初始化hash表,以及初始化数组索引
  if (ngx_http_variables_init_vars(cf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }

  /*
   * http{}'s cf->ctx was needed while the configuration merging
   * and in postconfiguration process
   */

  *cf = pcf;


 // 初始化 phase_engine 结构
  if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }


  /* optimize the lists of ports, addresses and server names */

 // 创建 http 连接,并设置为监听状态
  if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != NGX_OK) {
    return NGX_CONF_ERROR;
  }

  return NGX_CONF_OK;

failed:

  *cf = pcf;

  return rv;
} // }}}