Chinaunix首页 | 论坛 | 博客
  • 博客访问: 744361
  • 博文数量: 239
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1045
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-22 18:25
文章分类

全部博文(239)

文章存档

2019年(9)

2018年(64)

2017年(2)

2016年(26)

2015年(30)

2014年(41)

2013年(65)

2012年(2)

分类: C/C++

2013-09-26 13:31:46

原文地址:nginx配置初始化过程 作者:djjsindy

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

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

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

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

  24.         if (module->merge_loc_conf) { //调用模块的merge location操作,把父location配置merge到每个server的location配置相当于图中的第一步

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

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

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

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

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

    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 (q = 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;

  22.         rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
  23.                                     clcf->loc_conf[ctx_index]); //loc_conf代表server下location配置,clcf->loc_conf代表每个location的配置
  24.         if (rv != NGX_CONF_OK) {
  25.             return rv;
  26.         }

  27.         rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf,
  28.                                       module, ctx_index);        //递归嵌套location
  29.         if (rv != NGX_CONF_OK) {
  30.             return rv;
  31.         }
  32.     }



阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~