Ubuntu 下 nginx-1.24.0 源码分析 - ngx_preinit_modules 函数
ngx_preinit_modules声明在 src\core\ngx_module.h
ngx_int_t ngx_preinit_modules(void)
;
实现在 src\core\ngx_module.c
ngx_int_t
ngx_preinit_modules(void)
{
ngx_uint_ti;
for (i = 0; ngx_modules; i++) {
ngx_modules->index = i;
ngx_modules->name = ngx_module_names;
}
ngx_modules_n = i;
ngx_max_module = ngx_modules_n + NGX_MAX_DYNAMIC_MODULES;
return NGX_OK;
}
在 Nginx 中,模块(module)是其核心架构的基础。
Nginx 的功能险些完全由模块实现,包罗核心模块、HTTP 模块、事件模块等。
为了管理这些模块,Nginx 在启动时会对所有静态模块进行初始化,并为动态模块预留空间。
ngx_preinit_modules 函数的主要作用是对静态模块进行预初始化,
设置每个模块的根本信息(如索引值和名称),
并盘算模块的最大数目。
这是 Nginx 启动过程中非常重要的一步。
函数界说与参数
ngx_int_t ngx_preinit_modules(void)
[*]返回值:ngx_int_t 类型,表示操作结果。成功返回 NGX_OK,失败返回 NGX_ERROR。
[*]参数:无参数。
代码分析
1. 遍历 ngx_modules 数组
ngx_uint_ti;
for (i = 0; ngx_modules; i++) {
ngx_modules->index = i;
ngx_modules->name = ngx_module_names;
}
[*]作用:
[*]遍历全局数组 ngx_modules,对每个模块进行初始化。
[*]设置模块的索引值(index)和名称(name)。
[*]逻辑:
[*]ngx_modules 是一个全局数组,包含了所有的静态模块。
[*]ngx_modules 是指向第 i 个模块的指针。
[*]ngx_modules->index = i:为模块分配一个唯一的索引值。
[*]ngx_modules->name = ngx_module_names:将模块名称从 ngx_module_names 数组中赋值给模块。
[*]意图:
[*]索引值用于标识模块,方便后续查找和管理。
[*]名称用于调试和日记记载,便于开发者定位题目。
ngx_modules、ngx_modules
界说在 objs\ngx_modules.c
#include <ngx_config.h>
#include <ngx_core.h>
extern ngx_module_tngx_core_module;
extern ngx_module_tngx_errlog_module;
extern ngx_module_tngx_conf_module;
extern ngx_module_tngx_openssl_module;
extern ngx_module_tngx_regex_module;
extern ngx_module_tngx_events_module;
extern ngx_module_tngx_event_core_module;
extern ngx_module_tngx_epoll_module;
extern ngx_module_tngx_http_module;
extern ngx_module_tngx_http_core_module;
extern ngx_module_tngx_http_log_module;
extern ngx_module_tngx_http_upstream_module;
extern ngx_module_tngx_http_v2_module;
extern ngx_module_tngx_http_static_module;
extern ngx_module_tngx_http_gzip_static_module;
extern ngx_module_tngx_http_autoindex_module;
extern ngx_module_tngx_http_index_module;
extern ngx_module_tngx_http_mirror_module;
extern ngx_module_tngx_http_try_files_module;
extern ngx_module_tngx_http_auth_basic_module;
extern ngx_module_tngx_http_access_module;
extern ngx_module_tngx_http_limit_conn_module;
extern ngx_module_tngx_http_limit_req_module;
extern ngx_module_tngx_http_geo_module;
extern ngx_module_tngx_http_map_module;
extern ngx_module_tngx_http_split_clients_module;
extern ngx_module_tngx_http_referer_module;
extern ngx_module_tngx_http_rewrite_module;
extern ngx_module_tngx_http_ssl_module;
extern ngx_module_tngx_http_proxy_module;
extern ngx_module_tngx_http_fastcgi_module;
extern ngx_module_tngx_http_uwsgi_module;
extern ngx_module_tngx_http_scgi_module;
extern ngx_module_tngx_http_grpc_module;
extern ngx_module_tngx_http_memcached_module;
extern ngx_module_tngx_http_empty_gif_module;
extern ngx_module_tngx_http_browser_module;
extern ngx_module_tngx_http_upstream_hash_module;
extern ngx_module_tngx_http_upstream_ip_hash_module;
extern ngx_module_tngx_http_upstream_least_conn_module;
extern ngx_module_tngx_http_upstream_random_module;
extern ngx_module_tngx_http_upstream_keepalive_module;
extern ngx_module_tngx_http_upstream_zone_module;
extern ngx_module_tngx_http_write_filter_module;
extern ngx_module_tngx_http_header_filter_module;
extern ngx_module_tngx_http_chunked_filter_module;
extern ngx_module_tngx_http_v2_filter_module;
extern ngx_module_tngx_http_range_header_filter_module;
extern ngx_module_tngx_http_gzip_filter_module;
extern ngx_module_tngx_http_postpone_filter_module;
extern ngx_module_tngx_http_ssi_filter_module;
extern ngx_module_tngx_http_charset_filter_module;
extern ngx_module_tngx_http_userid_filter_module;
extern ngx_module_tngx_http_headers_filter_module;
extern ngx_module_tngx_http_copy_filter_module;
extern ngx_module_tngx_http_range_body_filter_module;
extern ngx_module_tngx_http_not_modified_filter_module;
extern ngx_module_tngx_stream_module;
extern ngx_module_tngx_stream_core_module;
extern ngx_module_tngx_stream_log_module;
extern ngx_module_tngx_stream_proxy_module;
extern ngx_module_tngx_stream_upstream_module;
extern ngx_module_tngx_stream_write_filter_module;
extern ngx_module_tngx_stream_ssl_module;
extern ngx_module_tngx_stream_limit_conn_module;
extern ngx_module_tngx_stream_access_module;
extern ngx_module_tngx_stream_geo_module;
extern ngx_module_tngx_stream_map_module;
extern ngx_module_tngx_stream_split_clients_module;
extern ngx_module_tngx_stream_return_module;
extern ngx_module_tngx_stream_set_module;
extern ngx_module_tngx_stream_upstream_hash_module;
extern ngx_module_tngx_stream_upstream_least_conn_module;
extern ngx_module_tngx_stream_upstream_random_module;
extern ngx_module_tngx_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;
};
2. 记载静态模块的数目
ngx_modules_n = i;
[*]作用:记载静态模块的数目。
[*]逻辑:
[*]i 是遍历 ngx_modules 数组时的计数器,最终值即为静态模块的数目。
[*]将其赋值给全局变量 ngx_modules_n。
[*]意图:
[*]生存静态模块的数目,供后续利用(如分配内存或盘算最大模块数)。
3. 盘算最大模块数目
ngx_max_module = ngx_modules_n + NGX_MAX_DYNAMIC_MODULES;
[*]作用:盘算体系支持的最大模块数目。
[*]逻辑:
[*]ngx_max_module 是一个全局变量,表示体系允许的最大模块数目。
[*]ngx_modules_n 是静态模块的数目。
[*]NGX_MAX_DYNAMIC_MODULES 是一个常量,表示体系为动态模块预留的空间,默认值为 128。
[*]最大模块数目等于静态模块数目加上动态模块预留空间。
[*]意图:
[*]为动态模块预留足够的空间,确保体系可以加载额外的模块。
[*]防止模块数目超出限定,导致内存分配失败或其他题目。
4. 返回成功状态
return NGX_OK;
[*]作用:返回成功状态。
[*]逻辑:
[*]假如所有操作都顺遂完成,函数返回 NGX_OK。
[*]意图:
[*]告知调用者初始化过程成功完成。
总结
我们可以看到 ngx_preinit_modules 函数的计划具有以下特点:
[*] 模块化:
[*]每个模块都有唯一的索引值和名称,便于管理和调试。
[*]静态模块和动态模块分开处理,保持代码清楚且易于扩展。
[*] 灵活性:
[*]为动态模块预留空间,支持模块的动态加载和卸载。
[*]允许开发者通过设置文件加载额外的功能模块,加强体系的可扩展性。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]