主页 > 人工智能  > 

Ubuntu下nginx-1.24.0源码分析-ngx_modules

Ubuntu下nginx-1.24.0源码分析-ngx_modules
ngx_modules

声明在 src\core\ngx_module.h

extern ngx_module_t *ngx_modules[];

定义在 objs/ngx_modules.c

#include <ngx_config.h> #include <ngx_core.h> 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 架构中的角色:


成员详解 1. ngx_uint_t ctx_index; 作用:模块上下文索引,用于在模块类型(如 HTTP、事件模块)中标识模块的上下文位置。背景: 不同模块类型(如 NGX_HTTP_MODULE)的上下文结构(ctx)可能不同,ctx_index 用于在模块类型内部唯一标识模块。例如,HTTP 模块的 ctx 指向 ngx_http_module_t 类型,ctx_index 是该模块在 HTTP 模块列表中的索引。 设计意图:支持模块类型的多态性,通过索引快速定位模块的上下文。
2. ngx_uint_t index; 作用:模块的全局唯一索引,标识模块在全局模块列表中的位置。背景: 所有模块(核心、事件、HTTP 等)在全局模块数组 ngx_modules 中按加载顺序排列,index 是模块在此数组中的位置。 设计意图:通过索引快速访问模块,避免遍历整个模块列表。
3. char *name; 作用:模块名称(字符串),用于日志和调试。背景:例如 "http_gzip_module"、"core_module"。设计意图:提供可读性,方便开发和调试。
4. ngx_uint_t spare0; 和 ngx_uint_t spare1; 作用:保留字段,未被使用。背景:早期版本可能用于其他目的,当前代码中已弃用。设计意图:为未来扩展预留空间,保持结构体对齐。
5. ngx_uint_t version; 作用:模块版本号,用于兼容性检查。背景:通常设置为 NGX_MODULE_V1,表示模块的版本与 Nginx 核心兼容。设计意图:确保模块与 Nginx 核心版本匹配,避免因版本差异导致的崩溃。
6. const char *signature; 作用:模块签名,验证模块的合法性。背景:通常设置为 NGX_MODULE_SIGNATURE,是一个预定义的字符串(如 "nginx")。设计意图:防止加载不兼容或恶意模块,确保模块由可信源编译。
7. void *ctx; 作用:指向模块特定上下文的指针,不同模块类型有不同的上下文结构。背景: 例如,HTTP 模块的 ctx 指向 ngx_http_module_t,包含处理请求的回调函数。事件模块的 ctx 指向 ngx_event_module_t,包含事件处理方法。 设计意图:实现模块多态性,通过统一接口调用不同模块的功能。
8. ngx_command_t *commands; 作用:指向模块支持的配置指令数组,用于解析配置文件。背景: ngx_command_t 结构体定义了指令名称、类型、解析函数等。例如,worker_processes 指令由核心模块处理。 设计意图:将配置解析逻辑封装到模块中,实现模块化配置管理。
9. ngx_uint_t type; 作用:模块类型,标识模块所属的类别。背景: 常见类型包括 NGX_CORE_MODULE(核心模块)、NGX_HTTP_MODULE(HTTP 模块)、NGX_EVENT_MODULE(事件模块)等。 设计意图:根据类型对模块分类,便于在不同阶段调用对应模块的功能。
10. 生命周期函数(函数指针)

以下函数指针定义了模块在不同阶段的回调逻辑:

init_master:

作用:在主进程启动时调用。背景:通常用于全局初始化(如日志模块)。设计意图:确保主进程的全局资源正确初始化。

init_module:

作用:在模块加载到运行周期(cycle)时调用。背景:所有模块的 init_module 在配置解析完成后被调用。设计意图:初始化模块的全局状态(如内存池、共享数据)。

init_process:

作用:在工作进程启动时调用。背景:用于进程级初始化(如事件模块初始化套接字)。设计意图:隔离进程资源,避免多进程竞争。

init_thread 和 exit_thread:

作用:线程级初始化和清理(Nginx 默认不启用多线程,通常为 NULL)。背景:为未来支持多线程预留。

exit_process:

作用:工作进程退出时调用,释放进程级资源。

exit_master:

作用:主进程退出时调用,释放全局资源。
11. 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 高效、可扩展的关键。

标签:

Ubuntu下nginx-1.24.0源码分析-ngx_modules由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Ubuntu下nginx-1.24.0源码分析-ngx_modules