Nginx 配置初始化过程

本文详细介绍了Nginx如何解析配置文件,并通过多级指针结构组织配置信息。重点讲解了http模块中server与location配置的复杂合并过程,包括两阶段merge操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx解析配置文件,将解析出来得配置存放在ngx_cycle_sconf_ctx中,conf_ctx是个四级指针,因为保存这些配置需要context,而这些context是有层级关系,最终的配置结构如图:


    http模块的配置有些复杂,由于server的配置还可以出现在http模块中,同时location的配置可以出现在http模块或者server模块中,所以对于http来说也就是最上面的那个ngx_http_ctx_conf_tsrv_confloc_conf是十分有必要的,这两个指针后面的结构体数组保存了在http中的那些server的和location的配置。同样对于每个server来说,不需要单独的main配置了,直接引用main的就可以。每个server必须有自己单独的ngx_http_core_srv_conf_t,来保存当前server块内的配置,这个配置最后会和http的里面的ngx_http_core_srv_conf_tmerge,这个merge是把父server的配置merge到子server配置上面。对于location的配置,在httpserver中都可以配置,那么merge的操作需要首先把httplocation配置merge到每个server配置中,然后每个serverlocation配置再和每个location模块中的配置进行merge,这里location配置需要merge两次。举例ngx_http_core_module模块merge的过程:

    merge过程是按照module一个一个modulemerge,第一步从main配置里面的servers,遍历每个server,把main里面的server配置merge到每个server的配置中,然后把main里面的location配置merge到每个serverlocation的配置中。第二步再次遍历每个serverlocations,把这个serverlocation的配置merge到具体的每个location中。

代码:




  1. static char *
  2. ngx_http_merge_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
  3.     ngx_http_module_t *module, ngx_uint_t ctx_index) //cmcf代表http的main配置
  4. { 
  5.     char *rv; 
  6.     ngx_uint_t s; 
  7.     ngx_http_conf_ctx_t *ctx, saved;
  8.     ngx_http_core_loc_conf_t *clcf;
  9.     ngx_http_core_srv_conf_t **cscfp;

  10.     cscfp = cmcf->servers.elts;             //得到servers数组,cmcf是main层的配置
  11.     ctx = (ngx_http_conf_ctx_t *) cf->ctx; //ctx是main的 ngx_http_conf_ctx_t
  12.     saved = *ctx;
  13.     rv = NGX_CONF_OK;

  14.     for (= 0; s < cmcf->servers.nelts; s++) { //遍历每个server,把main的配置merge到每个server中

  15.         /* merge the server{}s' srv_conf'*/

  16.         ctx->srv_conf = cscfp[s]->ctx->srv_conf; 

  17.         if (module->merge_srv_conf) {           //调用模块的merge server操作
  18.     //save.srv_conf是父server配置,cscf->ctx->srv_conf是当前server的配置,相当于图中的第一步
  19.             rv = module->merge_srv_conf(cf, saved.srv_conf[ctx_index],
  20.                                         cscfp[s]->ctx->srv_conf[ctx_index]); 

  21.             if (rv != NGX_CONF_OK) {
  22.                 goto failed;
  23.             } 
  24.         } 
  25.   //调用模块的merge location操作,把父location配置merge到每个server的location配置相当于图中的第一步
  26.         if (module->merge_loc_conf) {

  27.             /* merge the server{}'s loc_conf */

  28.             ctx->loc_conf = cscfp[s]->ctx->loc_conf;

  29.             rv = module->merge_loc_conf(cf, saved.loc_conf[ctx_index],
  30.                                         cscfp[s]->ctx->loc_conf[ctx_index]);
  31.             if (rv != NGX_CONF_OK) {
  32.                 goto failed;
  33.             } 

  34.             /* merge the locations{}' loc_conf'*/
  35.             clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];

  36. //该merge每个server的location配置到每个location的配置中了,相当于图中的第二步
  37.             rv = ngx_http_merge_locations(cf, clcf->locations,
  38.                                           cscfp[s]->ctx->loc_conf,
  39.                                           module, ctx_index); 

  40.             if (rv != NGX_CONF_OK) {
  41.                 goto failed;
  42.             }
  43.         }
  44.     }

    serverlocationlocationmerge过程

  1. static char *
  2. ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations,
  3.     void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
  4. {
  5.     char *rv;
  6.     ngx_queue_t *q;
  7.     ngx_http_conf_ctx_t *ctx, saved;
  8.     ngx_http_core_loc_conf_t *clcf;
  9.     ngx_http_location_queue_t *lq;

  10.     if (locations == NULL) {
  11.         return NGX_CONF_OK;
  12.     }

  13.     ctx = (ngx_http_conf_ctx_t *) cf->ctx;
  14.     saved = *ctx;

  15.     for (= ngx_queue_head(locations);      //遍历server中的locations队列
  16.          q != ngx_queue_sentinel(locations);
  17.          q = ngx_queue_next(q))
  18.     {
  19.         lq = (ngx_http_location_queue_t *) q;

  20.         clcf = lq->exact ? lq->exact : lq->inclusive; 
  21.         ctx->loc_conf = clcf->loc_conf;

//loc_conf代表server下location配置,clcf->loc_conf代表每个location的配置
  1.         rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
  2.                                     clcf->loc_conf[ctx_index]); 
  3.         if (rv != NGX_CONF_OK) {
  4.             return rv;
  5.         }

  6.         rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf,
  7.                                       module, ctx_index);        //递归嵌套location
  8.         if (rv != NGX_CONF_OK) {
  9.             return rv;
  10.         }
  11.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值