每个 nginx 模块,都是一个 ngx_module_t 类型的变量。根据 ngx_module_t 的 type,所有 nginx 模块可以分为 5 种类型:
typectx 指向的数据结构 commands 指向的数据结构具体模块 NGX_CORE_MODULEngx_core_module_tngx_command_tngx_core_module、ngx_errlog_module、ngx_events_module、ngx_openssl_module、ngx_http_module、ngx_mail_moduleNGX_EVENT_MODULEngx_event_module_tngx_command_tngx_event_core_module、ngx_select_module 等 NGX_HTTP_MODULEngx_http_module_tngx_command_tngx_http_core_module、ngx_http_log_module 等 NGX_MAIL_MODULEngx_mail_module_tngx_command_tngx_mail_core_module、ngx_mail_proxy_module 等 NGX_CONF_MODULENULLngx_command_tngx_conf_module 基本数据结构的定义
- struct ngx_module_s {
- ngx_uint_t ctx_index; // ctx_index表示当前模块在一类模块(type相同)中的序号 ngx_uint_t index; // index表示当前模块在所有模块中的序号 char *name; ngx_uint_t spare0; ngx_uint_t spare1; ngx_uint_t version; const char *signature; void *ctx; ngx_command_t *commands; ngx_uint_t type; ngx_int_t (*init_master)(ngx_log_t *log); ngx_int_t (*init_module)(ngx_cycle_t *cycle); ngx_int_t (*init_process)(ngx_cycle_t *cycle); ngx_int_t (*init_thread)(ngx_cycle_t *cycle); void (*exit_thread)(ngx_cycle_t *cycle); void (*exit_process)(ngx_cycle_t *cycle); void (*exit_master)(ngx_cycle_t *cycle); uintptr_t spare_hook0; uintptr_t spare_hook1; uintptr_t spare_hook2; uintptr_t spare_hook3; uintptr_t spare_hook4; uintptr_t spare_hook5; uintptr_t spare_hook6; uintptr_t spare_hook7;};// 核心模块的ctx指向的数据结构 typedef struct { ngx_str_t name; void *(*create_conf)(ngx_cycle_t *cycle); char *(*init_conf)(ngx_cycle_t *cycle, void *conf);} ngx_core_module_t;// 事件模块的ctx指向的数据结构 typedef struct { ngx_str_t *name; void *(*create_conf)(ngx_cycle_t *cycle); char *(*init_conf)(ngx_cycle_t *cycle, void *conf); ngx_event_actions_t actions;} ngx_event_module_t;// http模块的ctx指向的数据结构typedef struct { ngx_int_t (*preconfiguration)(ngx_conf_t *cf); ngx_int_t (*postconfiguration)(ngx_conf_t *cf); void *(*create_main_conf)(ngx_conf_t *cf); char *(*init_main_conf)(ngx_conf_t *cf, void *conf); void *(*create_srv_conf)(ngx_conf_t *cf); char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf); void *(*create_loc_conf)(ngx_conf_t *cf); char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);} ngx_http_module_t;// mail模块的ctx指向的数据结构typedef struct { ngx_mail_protocol_t *protocol; void *(*create_main_conf)(ngx_conf_t *cf); char *(*init_main_conf)(ngx_conf_t *cf, void *conf); void *(*create_srv_conf)(ngx_conf_t *cf); char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);} ngx_mail_module_t;struct ngx_command_s { ngx_str_t name; // 命令名 ngx_uint_t type; // 命令类型 char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_uint_t conf; ngx_uint_t offset; void *post;};
6 个核心模块的定义
- // 定义核心模块ngx_core_modulestatic ngx_command_t ngx_core_commands[] = {...};static ngx_core_module_t ngx_core_module_ctx = { ngx_string("core"), ngx_core_module_create_conf, ngx_core_module_init_conf};ngx_module_t ngx_core_module = { NGX_MODULE_V1, &ngx_core_module_ctx, /* module context */ ngx_core_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};// 定义核心模块ngx_errlog_modulestatic ngx_command_t ngx_errlog_commands[] = {...};static ngx_core_module_t ngx_errlog_module_ctx = { ngx_string("errlog"), NULL, NULL};ngx_module_t ngx_errlog_module = { NGX_MODULE_V1, &ngx_errlog_module_ctx, /* module context */ ngx_errlog_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};// 定义核心模块ngx_events_modulestatic ngx_command_t ngx_events_commands[] = {...};static ngx_core_module_t ngx_events_module_ctx = { ngx_string("events"), NULL, ngx_event_init_conf};ngx_module_t ngx_events_module = { NGX_MODULE_V1, &ngx_events_module_ctx, /* module context */ ngx_events_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};// 定义核心模块ngx_openssl_modulestatic ngx_command_t ngx_openssl_commands[] = {...};static ngx_core_module_t ngx_openssl_module_ctx = { ngx_string("openssl"), ngx_openssl_create_conf, NULL};ngx_module_t ngx_openssl_module = { NGX_MODULE_V1, &ngx_openssl_module_ctx, /* module context */ ngx_openssl_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ ngx_openssl_exit, /* exit master */ NGX_MODULE_V1_PADDING};// 定义核心模块ngx_http_modulestatic ngx_command_t ngx_http_commands[] = {...};static ngx_core_module_t ngx_http_module_ctx = { ngx_string("http"), NULL, NULL};ngx_module_t ngx_http_module = { NGX_MODULE_V1, &ngx_http_module_ctx, /* module context */ ngx_http_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};// 定义核心模块ngx_mail_module static ngx_command_t ngx_mail_commands[] = {...};static ngx_core_module_t ngx_mail_module_ctx = { ngx_string("mail"), NULL, NULL};ngx_module_t ngx_mail_module = { NGX_MODULE_V1, &ngx_mail_module_ctx, /* module context */ ngx_mail_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};
1 个配置模块的定义
- // 定义配置模块ngx_conf_module,其中ctx为空static ngx_command_t ngx_conf_commands[] = {...};ngx_module_t ngx_conf_module = { NGX_MODULE_V1, NULL, /* module context */ ngx_conf_commands, /* module directives */ NGX_CONF_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ ngx_conf_flush_files, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};
事件模块的定义
- // 定义事件模块ngx_event_core_modulestatic ngx_command_t ngx_event_core_commands[] = {...};ngx_event_module_t ngx_event_core_module_ctx = { &event_core_name, ngx_event_core_create_conf, /* create configuration */ ngx_event_core_init_conf, /* init configuration */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }};ngx_module_t ngx_event_core_module = { NGX_MODULE_V1, &ngx_event_core_module_ctx, /* module context */ ngx_event_core_commands, /* module directives */ NGX_EVENT_MODULE, /* module type */ NULL, /* init master */ ngx_event_module_init, /* init module */ ngx_event_process_init, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};
http 模块的定义
- // 定义http模块ngx_http_core_modulestatic ngx_command_t ngx_http_core_commands[] = {...}static ngx_http_module_t ngx_http_core_module_ctx = { ngx_http_core_preconfiguration, /* preconfiguration */ ngx_http_core_postconfiguration, /* postconfiguration */ ngx_http_core_create_main_conf, /* create main configuration */ ngx_http_core_init_main_conf, /* init main configuration */ ngx_http_core_create_srv_conf, /* create server configuration */ ngx_http_core_merge_srv_conf, /* merge server configuration */ ngx_http_core_create_loc_conf, /* create location configuration */ ngx_http_core_merge_loc_conf /* merge location configuration */};ngx_module_t ngx_http_core_module = { NGX_MODULE_V1, &ngx_http_core_module_ctx, /* module context */ ngx_http_core_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};
mail 模块的定义
- // 定义mail模块ngx_mail_core_modulestatic ngx_command_t ngx_mail_core_commands[] = {...};static ngx_mail_module_t ngx_mail_core_module_ctx = { NULL, /* protocol */ ngx_mail_core_create_main_conf, /* create main configuration */ NULL, /* init main configuration */ ngx_mail_core_create_srv_conf, /* create server configuration */ ngx_mail_core_merge_srv_conf /* merge server configuration */};ngx_module_t ngx_mail_core_module = { NGX_MODULE_V1, &ngx_mail_core_module_ctx, /* module context */ ngx_mail_core_commands, /* module directives */ NGX_MAIL_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};
图解
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: