nginx整体结构

http://blog.csdn.net/benbendy1984/article/details/6003779



       零零碎碎的看了nginx的源码,自己感觉还是比较的乱,所以花点时间把它的整体结构给理下,

 


 

 

 

 

      一   模块的结构(ngx_module_t)

 

    

  1. struct ngx_module_s {  
  2.     ngx_uint_t            ctx_index;             //分类索引,在该类别下模块中的索引号,现在系统core、event、http和mail四种模块  
  3.     ngx_uint_t            index;                 //模块的索引,表示该模块在所有模块中的下标  
  4.     ngx_uint_t            spare0;  
  5.     ngx_uint_t            spare1;  
  6.     ngx_uint_t            spare2;  
  7.     ngx_uint_t            spare3;  
  8.     ngx_uint_t            version;  
  9.     void                 *ctx;  
  10.     ngx_command_t        *commands;  
  11.     ngx_uint_t            type;  
  12.     ngx_int_t           (*init_master)(ngx_log_t *log);  
  13.     ngx_int_t           (*init_module)(ngx_cycle_t *cycle);  
  14.     ngx_int_t           (*init_process)(ngx_cycle_t *cycle);  
  15.     ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);  
  16.     void                (*exit_thread)(ngx_cycle_t *cycle);  
  17.     void                (*exit_process)(ngx_cycle_t *cycle);  
  18.     void                (*exit_master)(ngx_cycle_t *cycle);  
  19.     uintptr_t             spare_hook0;  
  20.     uintptr_t             spare_hook1;  
  21.     uintptr_t             spare_hook2;  
  22.     uintptr_t             spare_hook3;  
  23.     uintptr_t             spare_hook4;  
  24.     uintptr_t             spare_hook5;  
  25.     uintptr_t             spare_hook6;  
  26.     uintptr_t             spare_hook7;  
  27. };  

 

      index 是指模块的下标,系统在初始化的是,给了每个模块一个唯一id,就好比每个学生都有一个学号,每个员工都有一个工号,下标从0开始。

 

      ctx   是模块的上下文,不同的模块具有不同的上下文,这个具体在第二部分介绍

 

      command  处理指令,在配置文件中的每条指令,在模块中都有对应的处理,因为每个模块可以有多个指令,所以这里可以存放多个指令处理的指令

 

      其它暂时不介绍。

 

 

      二  模块上下文(ngx_xxx_module_ctx) 

           这里的xxx表示模块的类型(core、 event 、http、email)

 

      name 模块上下文的名字

 

      ngx_xxx_module_create_conf   模块配置的创建函数,这个函数创建的模块配置需要保存在 conf_ctx中的,那么放到那里呢?就是通过模块的index来定位的: cycle->conf_ctx[ngx_modules[i]->index] = rv;  (ngx_cycle.c: ngx_init_cycle()函数)


       ngx_xxx_module_init_conf        模块配置的初始化函数

 

      注意:不是每个模块都有上面的函数的,有些模块(event, http) 没有配置信息,那就不用这两个函数的。

 

        三  模块的配置 conf_ctx 

 

        这里面只是存放了指向各个模块conf的指针,各个模块的指针根据他们的index 有序存放。

 

        系统的配置结果 ngx_conf_t 和cycle 都有成员指向这个结构,这样便于获取模块的配置信息

 

        四  模块指令(ngx_command)

 

  1. struct ngx_command_s {  
  2.     ngx_str_t             name;  
  3.     ngx_uint_t            type;  
  4.     char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);  
  5.     ngx_uint_t            conf;  
  6.     ngx_uint_t            offset;       //set函数需要设置的值在整个结构中的地址   
  7.     void                  *post;  
  8. };  

 

         name 模块指令的名称

 

         type  模块的类型,这个比较的复杂,我还没有看懂

 

         set(cf, cmd ,conf)  设置的函数,所有工作都是在这个函数中完成的,不同模块的不同指令,都有不同的函数

 

         conf  这个是定位到那个被赋值模块的配置,但是不是所有的模块会使用这个变量的,一般的模块直接通过index 就定位到了的,具体是什么样的情况使用这个,还需要深入的看下。

 

         offset  这个是被赋值模块的配置内部的下标。 

 

         post   不知知道

 

         总的来说,模块指令完成类似下面的工作      *(config  +  offset) = cf->args ,对找到模块内的制定位置赋值。

 

 

        五 全局数据(ngx_cycle_t)

 

        这个保存了系统的全局数据,在整个程序执行过程中,都会使用到

 

       

  1. struct ngx_cycle_s {  
  2.     void                  ****conf_ctx;  
  3.     ngx_pool_t               *pool;  
  4.     ngx_log_t                *log;  
  5.     ngx_log_t                 new_log;  
  6.     ngx_connection_t        **files;                  //存放socket和connection之间的关系  
  7.     ngx_connection_t         *free_connections;  
  8.     ngx_uint_t                free_connection_n;  
  9.     ngx_array_t               listening;  
  10.     ngx_array_t               pathes;  
  11.     ngx_list_t                open_files;  
  12.     ngx_list_t                shared_memory;  
  13.     ngx_uint_t                connection_n;  
  14.     ngx_uint_t                files_n;  
  15.     ngx_connection_t         *connections;  
  16.     ngx_event_t              *read_events;  
  17.     ngx_event_t              *write_events;  
  18.     ngx_cycle_t              *old_cycle;  
  19.     ngx_str_t                 conf_file;  
  20.     ngx_str_t                 conf_param;  
  21.     ngx_str_t                 conf_prefix;  
  22.     ngx_str_t                 prefix;  
  23.     ngx_str_t                 lock_file;  
  24.     ngx_str_t                 hostname;  
  25. };  

 

        conf_ctx  指向的就是模块的配置信息

 

        六   指令参数 ngx_conf_t

        这里存放了从配置文件中读取的,需要传给模块配置信息的数据。

       

  1. struct ngx_conf_s {  
  2.     char                 *name;  
  3.     ngx_array_t          *args;                               //指令参数,从文件读入,直接放入这个数组  
  4.     ngx_cycle_t          *cycle;                              //  
  5.     ngx_pool_t           *pool;  
  6.     ngx_pool_t           *temp_pool;  
  7.     ngx_conf_file_t      *conf_file;  
  8.     ngx_log_t            *log;  
  9.     void                 *ctx;  
  10.     ngx_uint_t            module_type;  
  11.     ngx_uint_t            cmd_type;  
  12.     ngx_conf_handler_pt   handler;  
  13.     char                 *handler_conf;  
  14. }; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值