nginx的内部实现上用四个模块上下文结构把所有模块从实现上分开为四种,不同的模块上下文支持不同的模块钩子。如果抛开实现方式,仅仅从功能逻辑上来 区分,大致可以把所有模块分为三类,第一类是nginx的内核模块;第二类是http模块;第三类是mail模块。内核模块包含核心类模块、event类 模块和没有模块上下文结构的conf模块;http模块是所有的http类模块;mail模块是所有的mail类模块。由此可见,按照功能分类基本上与按 照模块上下文分类一致。
核心类模块,即上下文为ngx_core_module_t结构的模块有以下几个:
ngx_module_t ngx_core_module;
ngx_module_t ngx_errlog_module;
ngx_module_t ngx_events_module;
ngx_module_t ngx_http_module;
ngx_module_t ngx_mail_module;
ngx_module_t ngx_openssl_module;
ngx_module_t ngx_google_perftools_module;
ngx_module_t ngx_conf_module;(没有模块上下文,这里把它归类到核心类,便于后面分析)
ngx_events_module,ngx_http_module,ngx_mail_module 这三个模块主要是实现了对配置文件中events {...},http {...},mail {...}/imap {...}配置域的解析钩子,它们嵌套调用event类模块、http类模块和mail类模块实现的指令解析钩子解析{}中的具体指令,它们没有实现模块 上下文的create_conf和init_conf钩子(也不需要,因为它们只是一个中转而已,不做实际的事情)。
ngx_core_module 实现了配置文件中全局域的大部分指令的解析钩子,它们也实现模块上下文的create_conf钩子和init_conf钩子,创建和初始化 ngx_core_conf_t配置结构,这个结构可以通过cycle->conf_ctx[module.index]引用到。
ngx_errlog_module实现了配置文件中全局域的error_log指令的解析钩子,没有实现模块上下文的create_conf和init_conf钩子。
ngx_conf_module实现了全局域的include指令的解析钩子,没有实现模块上下文的create_conf和init_conf钩子。
ngx_openssl_module 实现了全局域的ssl_engine指令的解析钩子,实现了模块上下文的create_conf钩子,创建ngx_openssl_conf_t配置结 构,这个结构可以通过cycle->conf_ctx[module.index]引用到,也实现了模块的exit_master钩子。
事件类模块,即模块上下文为ngx_event_module_t结构的模块有以下几个:
ngx_module_t ngx_event_core_module;
ngx_module_t ngx_select_module;
ngx_module_t ngx_poll_module;
ngx_module_t ngx_eventport_module;
ngx_module_t ngx_aio_module;
ngx_module_t ngx_epoll_module;
ngx_module_t ngx_kqueue_module;
ngx_module_t ngx_devpoll_module;
ngx_module_t ngx_rtsig_module;
ngx_event_core_module 实现了配置文件events域的 worker_connections,connections,use,multi_accept,accept_mutex,accept_mutex_delay 和debug_connection指令的解析钩子,也实现了模块上下文的create_conf和init_conf钩子,创建和初始化 ngx_event_conf_t配置结构,这个结构可以通过cycle->conf_ctx[module.index]引用到,另外还实现了模 块的init_module和init_process钩子。
ngx_select_module实现了模块上下文的init_conf 初始化配置钩子和actions.add、actions.del、actions.enable、actions.disable、 actions.process_events、actions.init、actions.done这几个事件处理钩子。
ngx_poll_module 实现了模块上下文的init_conf初始化配置钩子和actions.add、actions.del、actions.enable、 actions.disable、actions.process_events、actions.init、actions.done这几个事件处理钩 子。
ngx_eventport_module实现了配置文件events域的eventport_events指令的解析钩子,实现了 模块上下文的create_conf和init_conf钩子,创建和初始化ngx_eventport_conf_t配置结构体,这个结构可以通过 cycle->conf_ctx[module.index]引用到,也实现了actions.add、actions.del、 actions.enable、actions.disable、actions.process_events、actions.init、 actions.done这几个事件处理钩子。
ngx_aio_module实现了actions.add、actions.del、actions.del_conn、actions.process_events、actions.init、actions.done这几个事件处理钩子。
ngx_epoll_module 实现了配置文件events域的epoll_events指令的解析钩子,实现了模块上下文的create_conf和init_conf钩子,创建和初 始化ngx_epoll_conf_t配置结构,这个结构可以通过cycle->conf_ctx[module.index]引用到,也实现了 actions.add、actions.del、actions.enable、actions.disable、actions.add_conn、 actions.del_conn、actions.process_events、actions.init、actions.done这几个事件处理 钩子。
ngx_kqueue_module实现了配置文件events域的kqueue_changes和kqueue_events指 令的解析钩子,实现了模块上下文的create_conf和init_conf钩子,创建和初始化ngx_kqueue_conf_t配置结构,这个结构 可以通过cycle->conf_ctx[module.index]引用到,也实现了actions.add、actions.del、 actions.enable、actions.disable、actions.process_changes、 actions.process_events、actions.init、actions.done这几个事件处理钩子。
ngx_devpoll_module 实现了配置文件events域的devpoll_changes和devpoll_events指令的解析钩子,实现了模块上下文的 create_conf和init_conf钩子,创建和初始化ngx_devpoll_conf_t配置结构,这个结构可以通过 cycle->conf_ctx[module.index]引用到,也实现了actions.add、actions.del、 actions.enable、actions.disable、actions.process_events、actions.init、 actions.done这几个事件处理钩子。
ngx_rtsig_module实现了配置文件events域的rtsig_前缀的几个 指令的解析钩子,实现了模块上下文的create_conf和init_conf钩子,创建和初始化ngx_rtsig_conf_t配置结构,这个结构 可以通过cycle->conf_ctx[module.index]引用到,也实现了actions.add_conn、 actions.del_conn、actions.process_events、actions.init
nginx源码分析(15)-模块分析(1)
最新推荐文章于 2025-06-05 17:24:23 发布
本文详细剖析了nginx的模块结构,尤其是内核模块、http模块和mail模块,以及核心类模块如ngx_core_module、ngx_events_module等。文章重点介绍了事件处理机制,包括ngx_event_core_module和ngx_epoll_module,并详细阐述了事件处理的构建和运行阶段,如ngx_init_cycle中的配置解析和ngx_event_process_init中的初始化过程。通过分析,读者将能深入理解nginx的事件模型和配置解析流程。

最低0.47元/天 解锁文章
1182





