Nginx学习(6)—http配置模型

来源:互联网 发布:陕汽重卡软件 编辑:程序博客网 时间:2024/05/01 13:11

HTTP配置模型

ngx_http_conf_ctx_t结构体

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. typedef struct {  
  2.     /* 三个指针数组,数组中每个元素分别指向由create_main/srv/loc_conf方法产生的配置结构体 */  
  3.     void        **main_conf;  
  4.     void        **srv_conf;  
  5.     void        **loc_conf;  
  6. } ngx_http_conf_ctx_t;  
  7.   
  8. static char *  
  9. ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)  
  10. {  
  11.     ...  
  12.     /* HTTP模块创建ngx_http_conf_ctx_t结构体 */  
  13.     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));  
  14.     if (ctx == NULL) {  
  15.         return NGX_CONF_ERROR;  
  16.     }  
  17.   
  18.     http_ctx = cf->ctx;  
  19.     ctx->main_conf = http_ctx->main_conf;  
  20.   
  21.     /* the server{}'s srv_conf */  
  22.     /* 生成一个srv指针数组存储所有srv配置 */  
  23.     ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);  
  24.     if (ctx->srv_conf == NULL) {  
  25.         return NGX_CONF_ERROR;  
  26.     }  
  27.   
  28.     /* the server{}'s loc_conf */  
  29.     /* 生成一个loc指针数组存储所有srv里的loc配置 */  
  30.     ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);  
  31.     if (ctx->loc_conf == NULL) {  
  32.         return NGX_CONF_ERROR;  
  33.     }  
  34.   
  35.     /* 遍历所有HTTP模块 */  
  36.     for (i = 0; ngx_modules[i]; i++) {  
  37.         if (ngx_modules[i]->type != NGX_HTTP_MODULE) {  
  38.             continue;  
  39.         }  
  40.   
  41.         module = ngx_modules[i]->ctx;  
  42.   
  43.         /* 如果遍历的该模块实现了srv/loc方法,则调用,并将返回的地址存储到srv/loc指针数组中 */  
  44.         if (module->create_srv_conf) {  
  45.             mconf = module->create_srv_conf(cf);  
  46.             if (mconf == NULL) {  
  47.                 return NGX_CONF_ERROR;  
  48.             }  
  49.   
  50.             ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf;  
  51.         }  
  52.   
  53.         if (module->create_loc_conf) {  
  54.             mconf = module->create_loc_conf(cf);  
  55.             if (mconf == NULL) {  
  56.                 return NGX_CONF_ERROR;  
  57.             }  
  58.   
  59.             ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;  
  60.         }  
  61.     }  
  62.     ...  
  63. }  

解析HTTP配置流程

画下书中流程,梳理梳理。


HTTP配置模型的内存布局

还是直接画上书中的解释图,一看就明白的东西。这种内存布局设计对生成的ngx_http_XXX_conf_t配置结构体的存储以及对配置文件的解析做了相当好的解释。




如何合并配置项

还是直接画下解析完http配置后的配置合并流程图。


总结

几个图说的清清楚楚。另外,本章还有error日志以及请求上下文两节。error日志就是打印信息函数的实现以及error的级别的介绍。所谓的请求上下文也是再熟悉不过了。之前所在实习公司的全新系统架构中,用的就是全异步的请求机制,对请求上下文这东西,是相当的感性了。然后,现在专注书中设计及业务流程上的学习,之后再此基础上开始做源码的分析。


0 0
原创粉丝点击