nginx源码分析—模块及其初始化

来源:互联网 发布:淘宝详情自动生成鲁班 编辑:程序博客网 时间:2024/05/16 05:26

Content

0. 

1. nginx有哪些模块?

2. nginx如何描述这些模块?

2.1 模块数据结构

2.1.1 ngx_module_t结构

2.1.2 ngx_command_t结构

2.2 模块类图

3. nginx如何组织这些模块?

3.1 全局数组ngx_modules

3.2 模块组织结构图

4. nginx的模块种类

5. nginx如何初始化这些模块?

5.1 静态初始化

5.2 动态初始化

5.2.1 index字段的初始化

5.2.2 ctx_index字段的初始化

5.2.3 其他初始化

6. 小结

  

0. 

 

本文以nginx-1.0.4版本为例,介绍nginx的模块及其初始化。.表示nginx-1.0.4源代码目录,本文为/usr/src/nginx-1.0.4

 

1. nginx有哪些模块?

 

要知道nginx有哪些模块,一个快速的方法就是编译nginx。编译之后,会在源代码根目录下生成objs目录,该目录中包含有ngx_auto_config.hngx_auto_headers.h,以及ngx_modules.c文件,当然,还有Makefile文件等。

 

其中,生成的ngx_modules.c文件中,重新集中申明(使用extern关键字)nginx配置的所有模块,这些模块可通过编译前的configure命令进行配置,即设置哪些模块需要编译,哪些不被编译。如下。

[cpp] view plaincopy
  1. 00001:  
  2. 00002: #include <ngx_config.h>  
  3. 00003: #include <ngx_core.h>  
  4. 00004:  
  5. 00005:  
  6. 00006:  
  7. 00007: extern ngx_module_t ngx_core_module;  
  8. 00008: extern ngx_module_t ngx_errlog_module;  
  9. 00009: extern ngx_module_t ngx_conf_module;  
  10. 00010: extern ngx_module_t ngx_events_module;  
  11. 00011: extern ngx_module_t ngx_event_core_module;  
  12. 00012: extern ngx_module_t ngx_epoll_module;  
  13. 00013: extern ngx_module_t ngx_http_module;  
  14. 00014: extern ngx_module_t ngx_http_core_module;  
  15. 00015: extern ngx_module_t ngx_http_log_module;  
  16. 00016: extern ngx_module_t ngx_http_upstream_module;  
  17. 00017: extern ngx_module_t ngx_http_static_module;  
  18. 00018: extern ngx_module_t ngx_http_autoindex_module;  
  19. 00019: extern ngx_module_t ngx_http_index_module;  
  20. 00020: extern ngx_module_t ngx_http_auth_basic_module;  
  21. 00021: extern ngx_module_t ngx_http_access_module;  
  22. 00022: extern ngx_module_t ngx_http_limit_zone_module;  
  23. 00023: extern ngx_module_t ngx_http_limit_req_module;  
  24. 00024: extern ngx_module_t ngx_http_geo_module;  
  25. 00025: extern ngx_module_t ngx_http_map_module;  
  26. 00026: extern ngx_module_t ngx_http_split_clients_module;  
  27. 00027: extern ngx_module_t ngx_http_referer_module;  
  28. 00028: extern ngx_module_t ngx_http_rewrite_module;  
  29. 00029: extern ngx_module_t ngx_http_proxy_module;  
  30. 00030: extern ngx_module_t ngx_http_fastcgi_module;  
  31. 00031: extern ngx_module_t ngx_http_uwsgi_module;  
  32. 00032: extern ngx_module_t ngx_http_scgi_module;  
  33. 00033: extern ngx_module_t ngx_http_memcached_module;  
  34. 00034: extern ngx_module_t ngx_http_empty_gif_module;  
  35. 00035: extern ngx_module_t ngx_http_browser_module;  
  36. 00036: extern ngx_module_t ngx_http_upstream_ip_hash_module;  
  37. 00037: extern ngx_module_t ngx_http_stub_status_module;  
  38. 00038: extern ngx_module_t ngx_http_write_filter_module;  
  39. 00039: extern ngx_module_t ngx_http_header_filter_module;  
  40. 00040: extern ngx_module_t ngx_http_chunked_filter_module;  
  41. 00041: extern ngx_module_t ngx_http_range_header_filter_module;  
  42. 00042: extern ngx_module_t ngx_http_gzip_filter_module;  
  43. 00043: extern ngx_module_t ngx_http_postpone_filter_module;  
  44. 00044: extern ngx_module_t ngx_http_ssi_filter_module;  
  45. 00045: extern ngx_module_t ngx_http_charset_filter_module;  
  46. 00046: extern ngx_module_t ngx_http_userid_filter_module;  
  47. 00047: extern ngx_module_t ngx_http_headers_filter_module;  
  48. 00048: extern ngx_module_t ngx_http_copy_filter_module;  
  49. 00049: extern ngx_module_t ngx_http_range_body_filter_module;  
  50. 00050: extern ngx_module_t ngx_http_not_modified_filter_module;  
  51. 00051:  

很显然,这些模块均是在此处用extern进行申明,以表明其他模块可以访问,而对其本身的定义和初始化ngx_module_t结构在其对应的.c文件中进行。例如,ngx_core_module模块便是在./src/core/nginx.c文件中定义并进行静态初始化。实际上,ngx_core_module是一个全局的结构体对象,其他模块类同。如下。

[cpp] view plaincopy
  1. ngx_module_t  ngx_core_module = {  
  2.     NGX_MODULE_V1,  
  3.     &ngx_core_module_ctx,                  /* module context */  
  4.     ngx_core_commands,                     /* module directives */  
  5.     NGX_CORE_MODULE,                       /* module type */  
  6.     NULL,                                  /* init master */  
  7.     NULL,                                  /* init module */  
  8.     NULL,                                  /* init process */  
  9.     NULL,                                  /* init thread */  
  10.     NULL,                                  /* exit thread */  
  11.     NULL,                                  /* exit process */  
  12.     NULL,                                  /* exit master */  
  13.     NGX_MODULE_V1_PADDING  
  14. };  

2. nginx如何描述这些模块?

 

2.1 模块数据结构

 

2.1.1 ngx_module_t结构

 

nginx的模块化架构最基本的数据结构为ngx_module_t,因此,此处,我们先分析这个结构,在./src/core/ngx_conf_file.h文件中定义。如下,//后的内容为笔者加入的注释。

[cpp] view plaincopy
  1. 00107: #define NGX_MODULE_V1    0, 0, 0, 0, 0, 0, 1         //该宏用来初始化前7个字段  
  2. 00108: #define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0 //该宏用来初始化最后8个字段  
  3. 00109:  
  4. 00110: struct ngx_module_s{  
  5. 00111:    ngx_uint_t    ctx_index;  //分类模块计数器  
  6. 00112:    ngx_uint_t    index;      //模块计数器  
  7. 00113:  
  8. 00114:    ngx_uint_t    spare0;  
  9. 00115:    ngx_uint_t    spare1;  
  10. 00116:    ngx_uint_t    spare2;  
  11. 00117:    ngx_uint_t    spare3;  
  12. 00118:  
  13. 00119:    ngx_uint_t    version;    //版本  
  14. 00120:  
  15. 00121:    void          *ctx;       //该模块的上下文,每个种类的模块有不同的上下文  
  16. 00122:    ngx_command_t *commands;  //该模块的命令集,指向一个ngx_command_t结构数组  
  17. 00123:    ngx_uint_t    type;       //该模块的种类,为core/event/http/mail中的一种  
  18. 00124:    //以下是一些callback函数  
  19. 00125:    ngx_uint_t    (*init_master)(ngx_log_t *log);      //初始化master  
  20. 00126:  
  21. 00127:    ngx_uint_t    (*init_module)(ngx_cycle_t *cycle);  //初始化模块  
  22. 00128:  
  23. 00129:    ngx_uint_t    (*init_process)(ngx_cycle_t *cycle); //初始化工作进程  
  24. 00130:    ngx_uint_t    (*init_thread)(ngx_cycle_t *cycle);  //初始化线程  
  25. 00131:    void          (*exit_thread)(ngx_cycle_t *cycle);  //退出线程  
  26. 00132:    void          (*exit_process)(ngx_cycle_t *cycle); //退出工作进程  
  27. 00133:  
  28. 00134:    void          (*exit_master)(ngx_cycle_t *cycle);  //退出master  
  29. 00135:  
  30. 00136:    uintptr_t     spare_hook0;  //这些字段貌似没用过  
  31. 00137:    uintptr_t     spare_hook1;  
  32. 00138:    uintptr_t     spare_hook2;  
  33. 00139:    uintptr_t     spare_hook3;  
  34. 00140:    uintptr_t     spare_hook4;  
  35. 00141:    uintptr_t     spare_hook5;  
  36. 00142:    uintptr_t     spare_hook6;  
  37. 00143:    uintptr_t     spare_hook7;  
  38. 00144: };  

其中,init_master, init_module, init_process, init_thread, exit_thread, exit_process, exit_master分别在初始化master、初始化模块、初始化工作进程、初始化线程、退出线程、退出工作进程、退出master时被调用。

 

2.1.2 ngx_command_t结构

 

模块的命令集commands指向一个ngx_command_t结构数组,在./src/core/ngx_conf_file.h文件中定义。如下,//后的内容为笔者加入的注释。

[cpp] view plaincopy
  1. 00077: struct ngx_command_s {  
  2. 00078:    ngx_str_t     name;  //命令名  
  3. 00079:    ngx_uint_t    type;  //命令类型  
  4. 00080:    char          *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);  
  5. 00081:    ngx_uint_t    conf;  
  6. 00082:    ngx_uint_t    offset;  
  7. 00083:    void          *post;  
  8. 00084: };  
  9. 00085:  
  10. 00086: #define ngx_null_command { ngx_null_string, 0, NULL, 0, 0, NULL }  //空命令  

2.2 模块类图

 

nginxC语言开发的开源高性能web server,其代码中大量使用了callback方式,例如模块结构ngx_module_t中的init_master等。实际上,我们可以将ngx_module_t看作C++的一个类,其中的数据字段便是其属性,而那些callback便是该类的操作。

 

——这应该就是nginx的模块化思想。画出的ngx_module_t的类图如下。

3. nginx如何组织这些模块?

 

3.1 全局数组ngx_modules

 

由第1节,我们知道,nginx拥有几十个模块,那么,这些模块是如何组织的呢?

 

——保存在一个全局指针数组ngx_modules[]中,数组的每一个元素均为一个全局ngx_module_t对象的指针。如下。请参考./objs/ngx_modules.c文件中的定义。

[cpp] view plaincopy
  1. 00052: ngx_module_t *ngx_modules[] = {  
  2. 00053:    &ngx_core_module,  
  3. 00054:    &ngx_errlog_module,  
  4. 00055:    &ngx_conf_module,  
  5. 00056:    &ngx_events_module,  
  6. 00057:    &ngx_event_core_module,  
  7. 00058:    &ngx_epoll_module,  
  8. 00059:    &ngx_http_module,  
  9. 00060:    &ngx_http_core_module,  
  10. 00061:    &ngx_http_log_module,  
  11. 00062:    &ngx_http_upstream_module,  
  12. 00063:    &ngx_http_static_module,  
  13. 00064:    &ngx_http_autoindex_module,  
  14. 00065:    &ngx_http_index_module,  
  15. 00066:    &ngx_http_auth_basic_module,  
  16. 00067:    &ngx_http_access_module,  
  17. 00068:    &ngx_http_limit_zone_module,  
  18. 00069:    &ngx_http_limit_req_module,  
  19. 00070:    &ngx_http_geo_module,  
  20. 00071:    &ngx_http_map_module,  
  21. 00072:    &ngx_http_split_clients_module,  
  22. 00073:    &ngx_http_referer_module,  
  23. 00074:    &ngx_http_rewrite_module,  
  24. 00075:    &ngx_http_proxy_module,  
  25. 00076:    &ngx_http_fastcgi_module,  
  26. 00077:    &ngx_http_uwsgi_module,  
  27. 00078:    &ngx_http_scgi_module,  
  28. 00079:    &ngx_http_memcached_module,  
  29. 00080:    &ngx_http_empty_gif_module,  
  30. 00081:    &ngx_http_browser_module,  
  31. 00082:    &ngx_http_upstream_ip_hash_module,  
  32. 00083:    &ngx_http_stub_status_module,  
  33. 00084:    &ngx_http_write_filter_module,  
  34. 00085:    &ngx_http_header_filter_module,  
  35. 00086:    &ngx_http_chunked_filter_module,  
  36. 00087:    &ngx_http_range_header_filter_module,  
  37. 00088:    &ngx_http_gzip_filter_module,  
  38. 00089:    &ngx_http_postpone_filter_module,  
  39. 00090:    &ngx_http_ssi_filter_module,  
  40. 00091:    &ngx_http_charset_filter_module,  
  41. 00092:    &ngx_http_userid_filter_module,  
  42. 00093:    &ngx_http_headers_filter_module,  
  43. 00094:    &ngx_http_copy_filter_module,  
  44. 00095:    &ngx_http_range_body_filter_module,  
  45. 00096:    &ngx_http_not_modified_filter_module,  
  46. 00097:    NULL  
  47. 00098: };  
  48. 00099:  

3.2 模块组织结构图

 

44个模块,这些模块的组织结构图如下所示,因模块较多,图中只画出一部分有代表性的重要模块。 

4. nginx的模块种类

 

在对全局数组ngx_modules进行初始化时,即对每一个模块进行了静态初始化。其中对模块的type字段的初始化是通过以下4个宏进行的。

 

(1) 文件./src/core/ngx_conf_file.h

[cpp] view plaincopy
  1. #define NGX_CORE_MODULE      0x45524F43   /* "CORE" */  
  2. #define NGX_CONF_MODULE      0x464E4F43   /* "CONF" */  

(2) 文件./src/event/ngx_event.h

[cpp] view plaincopy
  1. #define NGX_EVENT_MODULE      0x544E5645  /* "EVNT" */  
  2. #define NGX_EVENT_MODULE      0x544E5645  /* "EVNT" */  

(3) 文件./src/http/ngx_http_config.h

[cpp] view plaincopy
  1. #define NGX_HTTP_MODULE       0x50545448  /* "HTTP" */  

即模块种类宏,定义为一个十六进制的数,这个十六进制的数就是其类型对应的ASCII码。因此,nginx共有4种类型的模块,分别为"CORE","CONF","EVNT","HTTP"

 

实际上,如果在configure阶段,使用了"--with-mail"参数,mail模块将被编译进来,其对应的宏如下。

[cpp] view plaincopy
  1. #define NGX_MAIL_MODULE       0x4C49414D  /* "MAIL" */  

因此,严格来讲,nginx5中类型的模块,"CORE","CONF","EVNT","HTTP","MAIL"

5. nginx如何初始化这些模块?

 

5.1 静态初始化

 

即编译期间完成的数据成员初始化。记mname为某个模块的名字,其静态初始化过程如下。

(1) 用宏NGX_MODULE_V1初始化前7个字段

(2) 用全局对象ngx_mname_module_ctx的地址初始化ctx指针

(3) 用全局数组ngx_mname_commands[]初始化commands指针

(4) 用宏NGX_CORE_MODULE等初始化type字段

(5) 初始化init_mastercallback

(6) 用宏NGX_MODULE_V1_PADDING初始化最后8个字段

 

由此可见,在定义该模块(全局结构对象)时,将其ctx_indexindex均初始化为0。因此,模块的静态初始化(数据成员初始化)实际上只是对模块上下文、模块命令集和模块类型进行初始化。

 

5.2 动态初始化

 

nginx运行(启动)初期,对模块本身的初始化。

 

5.2.1 index字段的初始化

 

对各个模块的index字段的初始化是在main函数中进行的,如下。

[cpp] view plaincopy
  1. 00325:    ngx_max_module = 0;  
  2. 00326:    for (i = 0; ngx_modules[i]; i++) {  
  3. 00327:       ngx_modules[i]->index = ngx_max_module++;  
  4. 00328:    }  

可见,该for-loop执行后,每个模块的index值便是其在ngx_modules[]数组中的下标值,且全局变量ngx_max_module为模块个数,对于本例来讲,ngx_max_module=44

 

5.2.2 ctx_index字段的初始化

 

(1) "EVNT"类型的模块

[cpp] view plaincopy
  1. 00877: static char *  
  2. 00878: ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)  
  3. 00879: {  
  4. 00880:    char    *rv;  
  5. 00881:    void    ***ctx;  
  6. 00882:    ngx_uint_t    i;  
  7. 00883:    ngx_conf_t    pcf;  
  8. 00884:    ngx_event_module_t *m;  
  9. 00885:  
  10. 00886:    /* count the number of the event modules and set up their indices */  
  11. 00887:  
  12. 00888:    ngx_event_max_module = 0;  
  13. 00889:    for (i = 0; ngx_modules[i]; i++) {  
  14. 00890:       if (ngx_modules[i]->type ! = NGX_EVENT_MODULE) {  
  15. 00891:          continue;  
  16. 00892:       }  
  17. 00893:  
  18. 00894:       ngx_modules[i]->ctx_index = ngx_event_max_module++;  
  19. 00895:    }  
  20. 00896:  
  21. ...  

(2) "HTTP"类型的模块

[cpp] view plaincopy
  1. 00117: static char *  
  2. 00118: ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)  
  3. 00119: {  
  4. 00120:    char    *rv;  
  5. 00121:    ngx_uint_t    mi, m, s;  
  6. 00122:    ngx_conf_t    pcf;  
  7. 00123:    ngx_http_module_t    *module;  
  8. 00124:    ngx_http_conf_ctx_t    *ctx;  
  9. 00125:    ngx_http_core_loc_conf_t    *clcf;  
  10. 00126:    ngx_http_core_srv_conf_t **cscfp;  
  11. 00127:    ngx_http_core_main_conf_t *cmcf;  
  12. 00128:  
  13. 00129:    /* the main http context */  
  14. 00130:  
  15. 00131:    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));  
  16. 00132:    if (ctx == NULL) {  
  17. 00133:       return NGX_CONF_ERROR;  
  18. 00134:    }  
  19. 00135:  
  20. 00136:    *(ngx_http_conf_ctx_t **) conf = ctx;  
  21. 00137:  
  22. 00138:  
  23. 00139:    /* count the number of the http modules and set up their indices */  
  24. 00140:  
  25. 00141:    ngx_http_max_module = 0;  
  26. 00142:    for (m = 0; ngx_modules[m]; m++) {  
  27. 00143:       if (ngx_modules[m]->type ! = NGX_HTTP_MODULE) {  
  28. 00144:          continue;  
  29. 00145:       }  
  30. 00146:  
  31. 00147:       ngx_modules[m]->ctx_index = ngx_http_max_module++;  
  32. 00148:    }  
  33. 00149:  
  34. ...  

(3) "MAIL"类型的模块

[cpp] view plaincopy
  1. 00072: static char *  
  2. 00073: ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)  
  3. 00074: {  
  4. 00075:    char    *rv;  
  5. 00076:    ngx_uint_t    i, m, mi, s;  
  6. 00077:    ngx_conf_t    pcf;  
  7. 00078:    ngx_array_t    ports;  
  8. 00079:    ngx_mail_listen_t    *listen;  
  9. 00080:    ngx_mail_module_t    *module;  
  10. 00081:    ngx_mail_conf_ctx_t    *ctx;  
  11. 00082:    ngx_mail_core_srv_conf_t **cscfp;  
  12. 00083:    ngx_mail_core_main_conf_t *cmcf;  
  13. 00084:  
  14. 00085:    if (cmd->name.data[0] == 'i') {  
  15. 00086:       ngx_conf_log_error(NGX_LOG_WARN, cf, 0,  
  16. 00087:          "the \"imap\" directive is deprecated, "  
  17. 00088:          "use the \"mail\" directive instead");  
  18. 00089:    }  
  19. 00090:  
  20. 00091:    /* the main mail context */  
  21. 00092:  
  22. 00093:    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t));  
  23. 00094:    if (ctx == NULL) {  
  24. 00095:       return NGX_CONF_ERROR;  
  25. 00096:    }  
  26. 00097:  
  27. 00098:    *(ngx_mail_conf_ctx_t **) conf = ctx;  
  28. 00099:  
  29. 00100:    /* count the number of the http modules and set up their indices */  
  30. 00101:  
  31. 00102:    ngx_mail_max_module = 0;  
  32. 00103:    for (m = 0; ngx_modules[m]; m++) {  
  33. 00104:       if (ngx_modules[m]->type ! = NGX_MAIL_MODULE) {  
  34. 00105:          continue;  
  35. 00106:       }  
  36. 00107:  
  37. 00108:       ngx_modules[m]->ctx_index = ngx_mail_max_module++;  
  38. 00109:    }  
  39. 00110:  
  40. ...  

5.2.3 其他初始化

 

其他的初始化工作,将在nginx启动及其进程启动分析中介绍。

 

6. 小结

 

本文主要讲述了nginx的模块及其初始化,包括所有模块的组织,及模块的静态初始化和部分动态初始化。

0 0
原创粉丝点击