NGX source code analysis module and initialization

Content

0 sequence

1 what nginx module?

2 nginx to describe these modules?

The 2.1 module data structure

2.1.1 ngx_module_t structure

2.1.2 ngx_command_t structure

The 2.2 module class diagram

3 nginx how to organize these modules?

3.1 global array ngx_modules

The 3.2 module structure diagram

Module type 4 nginx

5 nginx how to initialize the module?

5.1 static initialization

5.2 dynamic initialization

5.2.1 index field initialization

5.2.2 ctx_index field initialization

Other 5.2.3 initialization

6 Summary

0 sequence

Based on the version of nginx-1.0.4 as an example, and introduced the nginx module initialization. C. nginx-1.0.4 source code directory, this is/usr/src/nginx-1.0.4.

1 what nginx module?

You know what nginx module, a fast method is to compile nginx. After compilation, generates the objs directory in the source code under the root directory, the directory contains ngx_auto_config.h and ngx_auto_headers.h, and the ngx_modules.c file, of course, there are Makefile files.

Among them, the generated ngx_modules.c file, re focus the statement (using the extern keyword) nginx configuration of all the modules, these modules can be configured by the compiler before the configure command set, which modules need to compile, which isn't compiled. The following.

[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:

Obviously, these modules is carried out using extern statement here, to indicate that other modules can access, and on its definition and initialization of ngx_module_t structure in the corresponding.C file. For example, the ngx_core_module module is defined in the./src/core/nginx.c file and static initialization. In fact, ngx_core_module is a global structure, similar to other modules. The following.

[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 to describe these modules?

The 2.1 module data structure

2.1.1 ngx_module_t structure

The data structure of modular architecture of nginx for the ngx_module_t, therefore, here, we first analyze the structure, defined in the./src/core/ngx_conf_file.h file. The following is the content, // after adding annotations.

[cpp] view plaincopy

  1. 00107: #define NGX_MODULE_V1 0, 0, 0, 0, 0, 0, 1 //This macro is used to initialize fields 7 ago
  2. 00108: #define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0 //This macro is used to initialize the final 8 fields
  3. 00109:
  4. 00110: struct ngx_module_s{
  5. 00111: ngx_uint_t ctx_index; //Classification module counter
  6. 00112: ngx_uint_t index; //Module counter
  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; //Edition
  14. 00120:
  15. 00121: void *ctx; //The module context, each kind of module has different contexts
  16. 00122: ngx_command_t *commands; //The module command set, points to a ngx_command_t structure array
  17. 00123: ngx_uint_t type; //Species in this module, a core/event/http/mail
  18. 00124: //The following are some of the callback function
  19. 00125: ngx_uint_t (*init_master)(ngx_log_t *log); //The initialization of master
  20. 00126:
  21. 00127: ngx_uint_t (*init_module)(ngx_cycle_t *cycle); //Initialization module
  22. 00128:
  23. 00129: ngx_uint_t (*init_process)(ngx_cycle_t *cycle); //The initialization process
  24. 00130: ngx_uint_t (*init_thread)(ngx_cycle_t *cycle); //Initialization thread
  25. 00131: void (*exit_thread)(ngx_cycle_t *cycle); //Exit thread
  26. 00132: void (*exit_process)(ngx_cycle_t *cycle); //Out of work process
  27. 00133:
  28. 00134: void (*exit_master)(ngx_cycle_t *cycle); //Exit master
  29. 00135:
  30. 00136: uintptr_t spare_hook0; //These fields are seemingly useless
  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: };

Among them, init_master, init_module, init_process, init_thread, exit_thread, exit_process, exit_masterIn the initializationmaster, Initialization module, initialization process, initialization thread, exit, exit, exit process threadmasterTo be called.

2.1.2 ngx_command_t structure

Module command set the commands to a ngx_command_t array structure, defined in the./src/core/ngx_conf_file.h file. The following is the content, // after adding annotations.

[cpp] view plaincopy

  1. 00077: struct ngx_command_s {
  2. 00078: ngx_str_t name; //The command name
  3. 00079: ngx_uint_t type; //Command 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 } //Air command

The 2.2 module class diagram

Nginx is open source and high performance web server C language development, the code callback is used extensively, such as module structure in ngx_module_t init_master. In fact, we can be a kind of ngx_module_t as C++, the data field which is its properties, and the callback is the kind of operation.

-This is the nginx modularization. Draw a ngx_module_t class diagram as follows.

3 nginx how to organize these modules?

3.1 global array ngx_modules

By the first day, as we know, nginx has dozens of module, then, is how to organize these modules?

-Stored in a global pointer array in ngx_modules[], each element of the array is a pointer to a global ngx_module_t object. The following. Please define the reference in the./objs/ngx_modules.c file.

[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:

The 3.2 module structure diagram

A total of 44 modules, the organization chart of these modules are shown below, because many modules, the map was drawn part is an important module of representative.

Module type 4 nginx

In the initialization of the global array ngx_modules, i.e. for each module of the static initialization. The type field of the module is initialized by the following 4 macros are.

(1) File/src/core/ngx_conf_file.h

[cpp] view plaincopy

  1. #define NGX_CORE_MODULE 0x45524F43 /* "CORE" */
  2. #define NGX_CONF_MODULE 0x464E4F43 /* "CONF" */

(2) File/src/event/ngx_event.h

[cpp] view plaincopy

  1. #define NGX_EVENT_MODULE 0x544E5645 /* "EVNT" */
  2. #define NGX_EVENT_MODULE 0x544E5645 /* "EVNT" */

(3) File/src/http/ngx_http_config.h

[cpp] view plaincopy

  1. #define NGX_HTTP_MODULE 0x50545448 /* "HTTP" */

The module type macros, defined as a sixteen hexadecimal number, corresponding to the sixteen hexadecimal number is the type of ASCII code. Therefore, nginx has 4 types of modules, respectively"CORE","CONF","EVNT","HTTP".

In fact, if in the configure stage, the use of " --with-mail" parameter, mail module will be compiled in, its corresponding macros are as follows.

[cpp] view plaincopy

  1. #define NGX_MAIL_MODULE 0x4C49414D /* "MAIL" */

Therefore, strictly speaking, nginx has 5 types of module, "CORE","CONF","EVNT","HTTP","MAIL".

5 nginx how to initialize the module?

5.1 static initialization

The compile time completed initialize the data members. Let mname be a module name, its static initialization process is as follows.

(1) With 7 former macro NGX_MODULE_V1 initialization field

(2) With the address of the global ngx_mname_module_ctx object to initialize the CTX pointer

(3) With the global array ngx_mname_commands[] to initialize the commands pointer

(4) Using the macro NGX_CORE_MODULE to initialize the type field

(5) Initialize init_master callback

(6) Using the macro NGX_MODULE_V1_PADDING initializes the final 8 fields

Thus, the definition of the module (the global structure of the object), the ctx_index and index are initialized to 0. Therefore, the static initialization module (initialize the data members) are really just module context, module and module type initialization command set.

5.2 dynamic initialization

Nginx operation (starting) early, the module initialization.

5.2.1 index field initialization

The index field of each module is initialized in the main function, as follows.

[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: }

Visible, the for-loop implementation, each module of the index value is in the ngx_modules[] array subscript values, and the global variable ngx_max_module the module number, for this example is, ngx_max_module=44.

5.2.2 ctx_index field initialization

(1) "EVNT"Types of module

[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"Types of module

[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"Types of module

[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. ...

Other 5.2.3 initialization

Initialization of other, will be in the nginx startup and starting the process of introduction.

6 Summary

This paper describes the modules and the initialization of nginx, including all modules organization, and the module static initialization and dynamic initialization.

Posted by Albert at March 15, 2014 - 6:07 PM