nginx 源码学习笔记(三)
来源:互联网 发布:ubuntu安装ruby 编辑:程序博客网 时间:2024/05/21 17:42
下面开始真正"nginx之旅",屏住呼吸吧!
首先找好入手点,对于nginx的入手点就是ngx_module_t结构,声明在src/core/ngx_conf_file.h中(我的版本nginx-1.0.13)
#define NGX_MODULE_V1 0, 0, 0, 0, 0, 0, 1#define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0struct ngx_module_s { ngx_uint_t ctx_index; ngx_uint_t index; ngx_uint_t spare0; ngx_uint_t spare1; ngx_uint_t spare2; ngx_uint_t spare3; ngx_uint_t version; 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;};
index: 是一个模块计数器,按照每个模块在ngx_modules[]数组中的声明顺序,从0开始依次给每个模块赋值:
声明顺序在文件objs/ngx_modules.c中。
ngx_module_t *ngx_modules[] = { &ngx_core_module, &ngx_errlog_module, &ngx_conf_module, &ngx_events_module, &ngx_event_core_module, &ngx_epoll_module, &ngx_http_module, &ngx_http_core_module, &ngx_http_log_module,
从src/core/nginx.c中可以找到顺序赋值的代码:
ngx_max_module = 0;for (i = 0; ngx_modules[i]; i++) { ngx_modules[i]->index = ngx_max_module++;}
ctx_index是分类的模块计数器,nginx模块可以分为四种:core、event 、http和mail,每个模块都会各自技术,ctx_index就是每个模块在其所属类组的技术。代码如下
src/event/ngx_event.cfor (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_EVENT_MODULE) { continue; } ngx_modules[i]->ctx_index = ngx_event_max_module++;}src/http/ngx_http.cngx_http_max_module = 0;for (m = 0; ngx_modules[m]; m++) { if (ngx_modules[m]->type != NGX_HTTP_MODULE) { continue; } ngx_modules[m]->ctx_index = ngx_http_max_module++;}src/mail/ngx_mail.cngx_mail_max_module = 0;for (m = 0; ngx_modules[m]; m++) { if (ngx_modules[m]->type != NGX_MAIL_MODULE) { continue; } ngx_modules[m]->ctx_index = ngx_mail_max_module++;}
ctx是模块的上下文,不同种类的模块有不同的上下文,因此实现了四种结构体。(这里非常重要)
commands是模块的指令集。每一个指令在源码中对应着一个ngx_command_t结构变量,
static ngx_command_t ngx_core_commands[] = { { ngx_string("daemon"), NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, 0, offsetof(ngx_core_conf_t, daemon), NULL },.....不全部列举
type就是模块的种类,用于区分前面提到的core event http和mail。
init_master、 init_module、init_process、init_thread、exit_thread、exit_process、 exit_master是函数指针,指向模块实现的自定义回调函数,这些回调函数分别在初始化master、初始化模块、初始化工作进程、初始化线程、退出线程、退出工作进程和退出master的时候被调用,如果模块需要在这些时机做处理,就可以实现对应的函数,并把它赋值给对应的函数指针来注册一个回调函数接口。
其余暂且不详。。
接下来剖析一下ngx_module_t的ctx成员,这个成员的意义是每个模块的上下文,所谓的上下文,也就是这个模块究竟可以做什么,从前面的分析可以知道nginx把所有模块分为四类(core/event/http/mail),对应的,nginx也认为模块的上下文是四种,分别用四个结构体表示:ngx_core_module_t、ngx_event_module_t、ngx_http_module_t、 ngx_mail_module_t。也就是说,如果一个模块属于core分类,那么其上下文就是ngx_core_module_t结构的变量,其他类推。这四个结构体类似于ngx_module_t,也是一些函数指针的集合,每个模块根据自己所属的分类,自定义一些操作函数,通过把这些操作函数赋值为对应分类结构体中的函数指针,这就注册了一个回调函数接口,从而就可以实现更细致的功能了,例如可以为event模块添加事件处理函数,可以为http模块添加过滤函数等。
- nginx 源码学习笔记(三)
- nginx源码学习(三)
- nginx 源码学习笔记(三)——nginx精粹-模块
- nginx 源码学习笔记(三)——nginx精粹-模块
- nginx 源码学习笔记(三)——nginx精粹-模块
- nginx 源码学习笔记(一)
- nginx 源码学习笔记(四)
- nginx 源码学习笔记(二)
- nginx 源码学习笔记(五)
- nginx 源码学习笔记(六)
- nginx 源码学习笔记(七)
- Nginx学习笔记(三)
- nginx源码学习(三)配置解析
- nginx 源码学习笔记(二十二)—— event 模块(三) ——epoll模块
- nginx 源码学习笔记(二十二)—— event 模块(三) ——epoll模块
- RealVNC源码学习笔记 三
- dubbo源码 学习笔记(三)
- Nginx源码学习(三) nginx进程模型
- des加密
- asp.net 滚动条位置保持不变
- DirectFB中的一些结构(Screen,Window, Layer, Surface)及其之间的关系
- Android不让EditText启动时获得焦点
- char *name 与 char name[]的区别(基础知识)
- nginx 源码学习笔记(三)
- 如何查看NC版本
- 监控iPhone的电池状态
- 用Gvim建立IDE编程环境 (Windows篇)
- CX51 用户手册----NOCOND 伪指令
- LC值计算
- 【跟师兄学编程】之用VS2010新建MFC工程
- 面试中的Singleton
- asp.net UpdatePanel的简单用法 详细出处参考:http://www.jb51.net/article/16462.htm