Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1485549
  • 博文数量: 228
  • 博客积分: 1698
  • 博客等级: 上尉
  • 技术积分: 3241
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-24 21:49
个人简介

Linux

文章分类

全部博文(228)

文章存档

2017年(1)

2016年(43)

2015年(102)

2014年(44)

2013年(5)

2012年(30)

2011年(3)

分类: LINUX

2015-10-19 21:20:14

介绍一下Nginx的一些基本数据结构,理解这些结构,对后续代码分析有很大帮助。

ngx_http_conf_ctx_t
用于存储各模块的配置信息,包括main/server/location,是三个数组,大小由ngx_http_max_module指定ngx_module_t中的ctx_index用于指定对应模块的
main/server/location的配置结构在下面数组中的下标。

点击(此处)折叠或打开
  1. typedef struct {
  2.     void **main_conf; // 所有模块的main配置数组
  3.     void **srv_conf;  // 所有模块的server配置数组
  4.     void **loc_conf;  // 所有模块的location配置数组
  5. } ngx_http_conf_ctx_t
下面宏用于从数组中获取对应的配置信息,其中r为ngx_http_request_t类型,module为对应的模块, cf为ngx_conf_t类型

点击(此处)折叠或打开

  1. #define ngx_http_get_module_main_conf(r, module) (r)->main_conf[module.ctx_index]
  2. #define ngx_http_get_module_srv_conf(r, module) (r)->srv_conf[module.ctx_index]
  3. #define ngx_http_get_module_loc_conf(r, module) (r)->loc_conf[module.ctx_index]

点击(此处)折叠或打开

  1. #define ngx_http_conf_get_module_main_conf(cf, module)
    1. ((ngx_http_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
  2. #define ngx_http_conf_get_module_srv_conf(cf, module)  
    1. ((ngx_http_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
  3. #define ngx_http_conf_get_module_loc_conf(cf, module)  
    1. ((ngx_http_conf_ctx_t *) cf->ctx)->loc_conf[module.ctx_index]

ngx_module_t

描述模块应包含的基本属性,是模块化架构基本的数据结构。
点击(此处)折叠或打开
  1. typedef struct ngx_module_s ngx_module_t;
  2. struct ngx_module_s {
  3.     ngx_uint_t ctx_index;      // 当前模块的各配置ngx_http_conf_ctx_t各数组的下标
  4.     ngx_uint_t index;          // 记录当前模块在ngx_modules数组中的索引,在main函数中赋值

  5.     ngx_uint_t spare0;         // 预留,目前尚未使用
  6.     ngx_uint_t spare1;         
  7.     ngx_uint_t spare2;
  8.     ngx_uint_t spare3;

  9.     ngx_uint_t version;        // 模块的版本信息,在各模块的ngx_module_t结构中进行了定义,常用NGX_MODULE_V1初始化,
  10.                                // #define NGX_MODULE_V1          0, 0, 0, 0, 0, 0, 1

  11.     void *ctx;                 // 模块的上下文,不同类型模块有不同上下文,目前有core/event/http/mail模块,因此实现了四种结构体
  12.                                // 包括:ngx_core_module_t/ngx_http_module_t/ngx_mail_module_t/ngx_event_module_t类型的ctx

  13.     ngx_command_t *commands;   // 模块对应的指令数组,每个指令对应数组中的一个成员

  14.     ngx_uint_t type;           // 指定模块的类型,NGX_CORE_MODULE/NGX_HTTP_MODULE/NGX_MAIL_MODULE/NGX_EVENT_MODULE/NGX_CONF_MODULE
  15.                                // 这里多了一个CONF的类型,但配置模块本身没有定义上下文

  16.     ngx_int_t (*init_master)(ngx_log_t *log);        // master初始化时调用,但目前尚未使用

  17.     ngx_int_t (*init_module)(ngx_cycle_t *cycle);    // master解析完配置,创建worker进程前,在ngx_init_cycle中遍历ngx_modules调用,只调用一次

  18.     ngx_int_t (*init_process)(ngx_cycle_t *cycle);   // ngx_worker_process_cycle中调用,每个worker初始化时调用
  19.     ngx_int_t (*init_thread)(ngx_cycle_t *cycle);    // 目前尚未使用,线程初始化时调用
  20.     void (*exit_thread)(ngx_cycle_t *cycle);         // 目前尚未使用,线程退出时调用
  21.     void (*exit_process)(ngx_cycle_t *cycle);        // worker进程退出时,遍历ngx_modules调用

  22.     void (*exit_master)(ngx_cycle_t *cycle);         // master进程退出时,遍历ngx_modules调用

  23.     uintptr_t spare_hook0;                           // 预留成员,目前尚未使用
  24.     uintptr_t spare_hook1;
  25.     uintptr_t spare_hook2;
  26.     uintptr_t spare_hook3;
  27.     uintptr_t spare_hook4;
  28.     uintptr_t spare_hook5;
  29.     uintptr_t spare_hook6;
  30.     uintptr_t spare_hook7;
  31. }
main函数中,index的初始化,ngx_max_module记录总的模块数量,下面ngx_modules数组在ngx_modules.c文件中定义,该文件由configure生成。

点击(此处)折叠或打开

  1. ngx_max_module = 0;
  2. for (i = 0; ngx_modules[i]; i++) {
  3.     ngx_modules[i]->index = ngx_max_module++;
  4. }
阅读(1768) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~