Ubuntu 下 nginx-1.24.0 源码分析 - ngx_modules

embedded/2025/3/9 21:04:27/

定义在 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_modules 是一个全局数组,类型为 ngx_module_t *[],它的作用是存储 Nginx 中所有模块的指针。

Nginx 是一个高度模块化的 Web 服务器,无论是核心功能(如事件处理、HTTP 处理)还是扩展功能(如反向代理、负载均衡、SSL 支持),都是通过模块实现的。

ngx_modules 提供了一个集中式的注册表,Nginx 在启动时通过遍历这个数组来初始化和配置所有模块。

具体来说:

  1. 模块注册:每个模块(无论是内置模块还是第三方模块)在编译时都会生成一个 ngx_module_t 结构体,描述该模块的元信息(如名称、版本、命令、钩子函数等),而 ngx_modules 数组则将这些模块的指针集中起来。

  2. 动态加载的基础:通过 ngx_modules,Nginx 可以在运行时动态决定哪些模块需要被加载和执行,这为模块的灵活性和可扩展性奠定了基础。

  3. 初始化顺序控制ngx_modules 数组中的顺序通常由编译时的配置决定(例如通过 ./configure 指定),Nginx 会按照这个顺序依次初始化模块,确保依赖关系正确处理。

ngx_modules 是自动生成的代码,这个文件是在编译过程中由配置脚本根据用户的选择生成的 


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;
};

name 字段保存的是模块的名称,是每个模块的唯一标识符

index 是每个模块的唯一标识符,与 name 提供字符串标识不同,index 提供了一个整数形式的标识符,便于快速索引和引用

name 主要用于日志和配置文件,便于人的阅读理解,index 主要用于程序内索引,更加高效


http://www.ppmy.cn/embedded/171313.html

相关文章

基于spring boot使用@Sl4j的日志功能,注解引入后爆红未生效

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 问题描述问题分析解决方案 &#x1f4c3;文章…

C++之序列容器(vector,list,dueqe)

1.大体对比 在软件开发的漫长历程中&#xff0c;数据结构与算法始终占据着核心地位&#xff0c;犹如大厦的基石&#xff0c;稳固支撑着整个程序的运行。在众多编程语言中&#xff0c;数据的存储与管理方式各有千秋&#xff0c;而 C 凭借其丰富且强大的工具集脱颖而出&#xff…

AI与.NET技术实操系列

引言 在当今技术飞速发展的时代&#xff0c;人工智能&#xff08;Artificial Intelligence, AI&#xff09;已成为推动创新和变革的核心力量。从智能助手到自动化决策系统&#xff0c;AI的应用无处不在&#xff0c;深刻影响着我们的生活和工作方式。对于软件开发者而言&#x…

vulnhub靶场渗透之SickOs1.2渗透教程,计划任务提权、chkrootkit提权

vulnhub靶场渗透之SickOs1.2渗透教程&#xff0c;计划任务提权、chkrootkit提权 一、信息收集 1、首先拿到靶场先扫一下ip 2025.3.7 AM 8&#xff1a;36 arp-scan -l 扫描同网段 nmap -sP 192.168.66.24/02、指纹扫描 nmap -sS -sV 192.168.66.130 指纹扫描PORT STATE S…

3.多线程获取音频AI的PCM数据

目录 一RV1126多线程获取音频PCM数据的流程 1.1.初始化AI模块&#xff1a; 1.2.启动AI模块&#xff1a; 1.3.开启多线程获取PCM音频数据&#xff1a; 二代码实战 三ffplay播放pcm数据需要的命令&#xff1a; 一RV1126多线程获取音频PCM数据的流程 RV1126多线程采集AI模块的…

STM32——USART—串口发送

目录 一&#xff1a;USART简介 二&#xff1a;初始化USART 1.开启时钟 2.代码 三&#xff1a;USART发送数据 1.USART发送数据函数 2.获取标志位的状态 3.代码 4.在main.c内调用 5.串口调试 1.串口选择要与设备管理器中的端口保持一致 2.波特率、停止位等要与前面…

在SpringBoot中,使用MultipartFile上传文件

在Spring Boot中&#xff0c;MultipartFile 是处理文件上传的核心类&#xff0c;通常用于接收客户端上传的文件数据。以下是详细的使用说明&#xff1a; 1. 基本概念 MultipartFile 是Spring框架提供的一个接口&#xff0c;封装了上传文件的所有信息&#xff08;如文件内容、文…

Excel 粘贴数据到可见单元格

目录 一. 前提条件二. 添加辅助列三. 排序&#xff0c;并粘贴数据 一. 前提条件 ⏹有如下表格&#xff0c;将名字中带有三的人给过滤出来&#xff0c;然后将地址给粘贴到相应的单元格中 &#x1f613;但是由于显示出来的单元格是被过滤出来的&#xff0c;不符合条件的人名被隐…