The highly modular design is the basis of nginx architecture. All modules are represented by the ngx_module_t struct. This struct defines seven internal callback methods, which are responsible for the initialization and exit of the module. A commands member is an array containing the ngx_command_t struct. ngx_command_t is used to parse configuration items. CTX is a void pointer, which makes the module highly flexible.
CTX can point to the common interfaces of different types of modules. The official nginx has five modules, and their relations with CTX interfaces are as follows:
- Core Module: the interface is ngx_core_module_t, And the type macro is ngx_core_module.
- HTTP module: the interface is ngx_http_modult_t, And the type macro is ngx_http_module.
- Event module: the interface is ngx_event_module_t, And the type macro is ngx_event_module.
- Mail module: the interface is ngx_mail_module_t, And the type macro is ngx_mail_module.
- Configuration Module: the interface is null, And the type macro is ngx_conf_module.
Note: The configuration module has only one specific module named ngx_conf_module. Its CTX context pointer is null and does not point to any struct. The role of the configuration module is to parse the nginx. conf configuration file.
The core module contains six modules:
- Ngx_core_module
- Ngx_errlog_module
- Ngx_events_module
- Ngx_openssl_module
- Ngx_http_module
- Ngx_mail_module
Their common interfaces are as follows:
Typedef struct {ngx_str_t name; // Core Module name // before parsing the configuration item, the Framework calls void * (* create_conf) (ngx_cycle_t * cycle) to create the data structure for storing the configuration item ); // After the configuration item is parsed, the Framework calls it and initializes the char * (* init_conf) (ngx_cycle_t * cycle, void * conf) of the module according to the parsed configuration item;} ngx_core_module_t;
Nginx framework management is closely related to core modules and configuration modules, but it is not directly related to the event module, HTTP module, or mail module. The Framework calls six core modules, and these six core modules manage other types of non-core modules. The relationships between the five main modules are as follows:
In general, the core module and configuration module are the basis of other modules, and the event module is the basis of the two application layer modules: the HTTP module and the mail module. Modules in the bold Section are the basis of other modules in this type of module.
Reference: Understanding nginx P256-P263.