声明在 src\core\ngx_module.h
extern ngx_module_t *ngx_modules[];
定义在 objs/ngx_modules.c
#include
#include
extern ngx_module_t ngx_core_module;
extern ngx_module_t ngx_errlog_module;
extern ngx_module_t ngx_conf_module;
extern ngx_module_t ngx_openssl_module;
extern ngx_module_t ngx_regex_module;
extern ngx_module_t ngx_events_module;
extern ngx_module_t ngx_event_core_module;
extern ngx_module_t ngx_epoll_module;
extern ngx_module_t ngx_http_module;
extern ngx_module_t ngx_http_core_module;
extern ngx_module_t ngx_http_log_module;
extern ngx_module_t ngx_http_upstream_module;
extern ngx_module_t ngx_http_v2_module;
extern ngx_module_t ngx_http_static_module;
extern ngx_module_t ngx_http_gzip_static_module;
extern ngx_module_t ngx_http_autoindex_module;
extern ngx_module_t ngx_http_index_module;
extern ngx_module_t ngx_http_mirror_module;
extern ngx_module_t ngx_http_try_files_module;
extern ngx_module_t ngx_http_auth_basic_module;
extern ngx_module_t ngx_http_access_module;
extern ngx_module_t ngx_http_limit_conn_module;
extern ngx_module_t ngx_http_limit_req_module;
extern ngx_module_t ngx_http_geo_module;
extern ngx_module_t ngx_http_map_module;
extern ngx_module_t ngx_http_split_clients_module;
extern ngx_module_t ngx_http_referer_module;
extern ngx_module_t ngx_http_rewrite_module;
extern ngx_module_t ngx_http_ssl_module;
extern ngx_module_t ngx_http_proxy_module;
extern ngx_module_t ngx_http_fastcgi_module;
extern ngx_module_t ngx_http_uwsgi_module;
extern ngx_module_t ngx_http_scgi_module;
extern ngx_module_t ngx_http_grpc_module;
extern ngx_module_t ngx_http_memcached_module;
extern ngx_module_t ngx_http_empty_gif_module;
extern ngx_module_t ngx_http_browser_module;
extern ngx_module_t ngx_http_upstream_hash_module;
extern ngx_module_t ngx_http_upstream_ip_hash_module;
extern ngx_module_t ngx_http_upstream_least_conn_module;
extern ngx_module_t ngx_http_upstream_random_module;
extern ngx_module_t ngx_http_upstream_keepalive_module;
extern ngx_module_t ngx_http_upstream_zone_module;
extern ngx_module_t ngx_http_write_filter_module;
extern ngx_module_t ngx_http_header_filter_module;
extern ngx_module_t ngx_http_chunked_filter_module;
extern ngx_module_t ngx_http_v2_filter_module;
extern ngx_module_t ngx_http_range_header_filter_module;
extern ngx_module_t ngx_http_gzip_filter_module;
extern ngx_module_t ngx_http_postpone_filter_module;
extern ngx_module_t ngx_http_ssi_filter_module;
extern ngx_module_t ngx_http_charset_filter_module;
extern ngx_module_t ngx_http_userid_filter_module;
extern ngx_module_t ngx_http_headers_filter_module;
extern ngx_module_t ngx_http_copy_filter_module;
extern ngx_module_t ngx_http_range_body_filter_module;
extern ngx_module_t ngx_http_not_modified_filter_module;
extern ngx_module_t ngx_stream_module;
extern ngx_module_t ngx_stream_core_module;
extern ngx_module_t ngx_stream_log_module;
extern ngx_module_t ngx_stream_proxy_module;
extern ngx_module_t ngx_stream_upstream_module;
extern ngx_module_t ngx_stream_write_filter_module;
extern ngx_module_t ngx_stream_ssl_module;
extern ngx_module_t ngx_stream_limit_conn_module;
extern ngx_module_t ngx_stream_access_module;
extern ngx_module_t ngx_stream_geo_module;
extern ngx_module_t ngx_stream_map_module;
extern ngx_module_t ngx_stream_split_clients_module;
extern ngx_module_t ngx_stream_return_module;
extern ngx_module_t ngx_stream_set_module;
extern ngx_module_t ngx_stream_upstream_hash_module;
extern ngx_module_t ngx_stream_upstream_least_conn_module;
extern ngx_module_t ngx_stream_upstream_random_module;
extern ngx_module_t ngx_stream_upstream_zone_module;
ngx_module_t *ngx_modules[] = {
&ngx_core_module,
&ngx_errlog_module,
&ngx_conf_module,
&ngx_openssl_module,
&ngx_regex_module,
&ngx_events_module,
&ngx_event_core_module,
&ngx_epoll_module,
&ngx_http_module,
&ngx_http_core_module,
&ngx_http_log_module,
&ngx_http_upstream_module,
&ngx_http_v2_module,
&ngx_http_static_module,
&ngx_http_gzip_static_module,
&ngx_http_autoindex_module,
&ngx_http_index_module,
&ngx_http_mirror_module,
&ngx_http_try_files_module,
&ngx_http_auth_basic_module,
&ngx_http_access_module,
&ngx_http_limit_conn_module,
&ngx_http_limit_req_module,
&ngx_http_geo_module,
&ngx_http_map_module,
&ngx_http_split_clients_module,
&ngx_http_referer_module,
&ngx_http_rewrite_module,
&ngx_http_ssl_module,
&ngx_http_proxy_module,
&ngx_http_fastcgi_module,
&ngx_http_uwsgi_module,
&ngx_http_scgi_module,
&ngx_http_grpc_module,
&ngx_http_memcached_module,
&ngx_http_empty_gif_module,
&ngx_http_browser_module,
&ngx_http_upstream_hash_module,
&ngx_http_upstream_ip_hash_module,
&ngx_http_upstream_least_conn_module,
&ngx_http_upstream_random_module,
&ngx_http_upstream_keepalive_module,
&ngx_http_upstream_zone_module,
&ngx_http_write_filter_module,
&ngx_http_header_filter_module,
&ngx_http_chunked_filter_module,
&ngx_http_v2_filter_module,
&ngx_http_range_header_filter_module,
&ngx_http_gzip_filter_module,
&ngx_http_postpone_filter_module,
&ngx_http_ssi_filter_module,
&ngx_http_charset_filter_module,
&ngx_http_userid_filter_module,
&ngx_http_headers_filter_module,
&ngx_http_copy_filter_module,
&ngx_http_range_body_filter_module,
&ngx_http_not_modified_filter_module,
&ngx_stream_module,
&ngx_stream_core_module,
&ngx_stream_log_module,
&ngx_stream_proxy_module,
&ngx_stream_upstream_module,
&ngx_stream_write_filter_module,
&ngx_stream_ssl_module,
&ngx_stream_limit_conn_module,
&ngx_stream_access_module,
&ngx_stream_geo_module,
&ngx_stream_map_module,
&ngx_stream_split_clients_module,
&ngx_stream_return_module,
&ngx_stream_set_module,
&ngx_stream_upstream_hash_module,
&ngx_stream_upstream_least_conn_module,
&ngx_stream_upstream_random_module,
&ngx_stream_upstream_zone_module,
NULL
};
char *ngx_module_names[] = {
"ngx_core_module",
"ngx_errlog_module",
"ngx_conf_module",
"ngx_openssl_module",
"ngx_regex_module",
"ngx_events_module",
"ngx_event_core_module",
"ngx_epoll_module",
"ngx_http_module",
"ngx_http_core_module",
"ngx_http_log_module",
"ngx_http_upstream_module",
"ngx_http_v2_module",
"ngx_http_static_module",
"ngx_http_gzip_static_module",
"ngx_http_autoindex_module",
"ngx_http_index_module",
"ngx_http_mirror_module",
"ngx_http_try_files_module",
"ngx_http_auth_basic_module",
"ngx_http_access_module",
"ngx_http_limit_conn_module",
"ngx_http_limit_req_module",
"ngx_http_geo_module",
"ngx_http_map_module",
"ngx_http_split_clients_module",
"ngx_http_referer_module",
"ngx_http_rewrite_module",
"ngx_http_ssl_module",
"ngx_http_proxy_module",
"ngx_http_fastcgi_module",
"ngx_http_uwsgi_module",
"ngx_http_scgi_module",
"ngx_http_grpc_module",
"ngx_http_memcached_module",
"ngx_http_empty_gif_module",
"ngx_http_browser_module",
"ngx_http_upstream_hash_module",
"ngx_http_upstream_ip_hash_module",
"ngx_http_upstream_least_conn_module",
"ngx_http_upstream_random_module",
"ngx_http_upstream_keepalive_module",
"ngx_http_upstream_zone_module",
"ngx_http_write_filter_module",
"ngx_http_header_filter_module",
"ngx_http_chunked_filter_module",
"ngx_http_v2_filter_module",
"ngx_http_range_header_filter_module",
"ngx_http_gzip_filter_module",
"ngx_http_postpone_filter_module",
"ngx_http_ssi_filter_module",
"ngx_http_charset_filter_module",
"ngx_http_userid_filter_module",
"ngx_http_headers_filter_module",
"ngx_http_copy_filter_module",
"ngx_http_range_body_filter_module",
"ngx_http_not_modified_filter_module",
"ngx_stream_module",
"ngx_stream_core_module",
"ngx_stream_log_module",
"ngx_stream_proxy_module",
"ngx_stream_upstream_module",
"ngx_stream_write_filter_module",
"ngx_stream_ssl_module",
"ngx_stream_limit_conn_module",
"ngx_stream_access_module",
"ngx_stream_geo_module",
"ngx_stream_map_module",
"ngx_stream_split_clients_module",
"ngx_stream_return_module",
"ngx_stream_set_module",
"ngx_stream_upstream_hash_module",
"ngx_stream_upstream_least_conn_module",
"ngx_stream_upstream_random_module",
"ngx_stream_upstream_zone_module",
NULL
};
ngx_module_t
定义在 src/core/ngx_core.h
typedef struct ngx_module_s ngx_module_t;
ngx_module_s
定义在 src/core/ngx_module.h
struct ngx_module_s {
ngx_uint_t ctx_index;
ngx_uint_t 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;
};
以下是 ngx_module_s
结构体的详细解释,涵盖每个成员的作用、意义及其在 Nginx 架构中的角色:
ngx_uint_t ctx_index;
NGX_HTTP_MODULE
)的上下文结构(ctx
)可能不同,ctx_index
用于在模块类型内部唯一标识模块。ctx
指向 ngx_http_module_t
类型,ctx_index
是该模块在 HTTP 模块列表中的索引。ngx_uint_t index;
ngx_modules
中按加载顺序排列,index
是模块在此数组中的位置。char *name;
"http_gzip_module"
、"core_module"
。ngx_uint_t spare0;
和 ngx_uint_t spare1;
ngx_uint_t version;
NGX_MODULE_V1
,表示模块的版本与 Nginx 核心兼容。const char *signature;
NGX_MODULE_SIGNATURE
,是一个预定义的字符串(如 "nginx"
)。void *ctx;
ctx
指向 ngx_http_module_t
,包含处理请求的回调函数。ctx
指向 ngx_event_module_t
,包含事件处理方法。ngx_command_t *commands;
ngx_command_t
结构体定义了指令名称、类型、解析函数等。worker_processes
指令由核心模块处理。ngx_uint_t type;
NGX_CORE_MODULE
(核心模块)、NGX_HTTP_MODULE
(HTTP 模块)、NGX_EVENT_MODULE
(事件模块)等。以下函数指针定义了模块在不同阶段的回调逻辑:
init_master
:
init_module
:
cycle
)时调用。init_module
在配置解析完成后被调用。init_process
:
init_thread
和 exit_thread
:
NULL
)。exit_process
:
exit_master
:
uintptr_t spare_hook0
到 spare_hook7
模块化架构:
ngx_module_s
结构体,Nginx 将功能划分为独立模块(如 HTTP、事件、日志),每个模块实现特定功能。NGX_CORE_MODULE
)管理全局配置,HTTP 模块处理请求,事件模块管理 I/O 复用。生命周期管理:
init_master
、init_module
、init_process
等函数指针,Nginx 在不同阶段调用模块的初始化和清理逻辑,确保资源正确分配和释放。配置解析:
commands
字段将配置指令与模块绑定,实现配置的模块化解析。兼容性与扩展性:
version
和 signature
确保模块与核心兼容。spare
字段和钩子预留扩展空间,支持未来功能增强。ngx_module_s
是 Nginx 模块化设计的核心数据结构,通过统一的接口管理模块的生命周期、配置解析和功能实现。其设计兼顾灵活性(通过函数指针和上下文多态性)、扩展性(保留字段和钩子)和性能(直接函数调用),是 Nginx 高效、可扩展的关键。