Chinaunix首页 | 论坛 | 博客
  • 博客访问: 282133
  • 博文数量: 134
  • 博客积分: 667
  • 博客等级: 上士
  • 技术积分: 770
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 15:19
文章分类

全部博文(134)

文章存档

2012年(134)

分类: LINUX

2012-06-12 15:03:24

源码的src/core目录下实现了不少精巧的数据结构,最重要的有:内存池ngx_pool_t、缓冲区ngx_buf_t、缓冲区链 ngx_chain_t、字符串ngx_str_t、数组ngx_array_t、链表ngx_list_t、队列ngx_queue_t、基于hash 的关联数组ngx_hash_t、红黑树ngx_rbtree_t、radix树ngx_radix_tree_t等,这些数据结构频繁出现在源码中,而且这些结构之间也是彼此配合,联系紧密,很难孤立某一个出来。每个数据结构都提供了一批相关的操作接口,一般设置数据的接口(add)只是从内存池中分配空间并返回指向结构体的指针,然后利用这个指针再去设置成员,这使得此类接口的实现保持干净简洁。这些数据结构的实现分析是有一定难度的,不过我觉得不了解实现也并不影响理解整体的架构,只要知道有这么些重要的数据结构和基本的功用就足以满足需要了,我会在搞清楚整体架构之后再详细分析一下这些精致的玩意。

nginx的模块化架构和实现方式是源码分析系列中最关键的一个部分。模块化是nginx的骨架,事件处理是nginx的心脏,而每个具体模块构成了nginx的血肉,摸清骨架,才能游刃有余。

先高屋建瓴的快速浏览一下nginx的模块化架构,后面再详细分析每一个重要数据结构和实现方式。

nginx 的模块在源码中对应着是ngx_module_t结构的变量,有一个全局的ngx_module_t指针数组,这个指针数组包含了当前编译版本支持的所有模块,这个指针数组的定义是在自动脚本生成的objs/ngx_modules.c文件中,下面是在我的机器上编译的nginx版本(0.8.9)的模块声明:

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_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_static_module;
extern ngx_module_t ngx_http_autoindex_module;
extern ngx_module_t ngx_http_index_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_zone_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_referer_module;
extern ngx_module_t ngx_http_rewrite_module;
extern ngx_module_t ngx_http_proxy_module;
extern ngx_module_t ngx_http_fastcgi_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_ip_hash_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_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_charset_filter_module;
extern ngx_module_t ngx_http_ssi_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;

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,
&ngx_http_upstream_module,
&ngx_http_static_module,
&ngx_http_autoindex_module,
&ngx_http_index_module,
&ngx_http_auth_basic_module,
&ngx_http_access_module,
&ngx_http_limit_zone_module,
&ngx_http_limit_req_module,
&ngx_http_geo_module,
&ngx_http_map_module,
&ngx_http_referer_module,
&ngx_http_rewrite_module,
&ngx_http_proxy_module,
&ngx_http_fastcgi_module,
&ngx_http_memcached_module,
&ngx_http_empty_gif_module,
&ngx_http_browser_module,
&ngx_http_upstream_ip_hash_module,
&ngx_http_write_filter_module,
&ngx_http_header_filter_module,
&ngx_http_chunked_filter_module,
&ngx_http_range_header_filter_module,
&ngx_http_gzip_filter_module,
&ngx_http_postpone_filter_module,
&ngx_http_charset_filter_module,
&ngx_http_ssi_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,
NULL
};

这里只有每个模块变量的声明(注意extern声明符),而每个模块变量的定义包含在各自模块实现的文件中,比如ngx_core_module定义在src/core/nginx.c中:

ngx_module_t ngx_core_module = {
NGX_MODULE_V1,
&ngx_core_module_ctx,
ngx_core_commands,
NGX_CORE_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};

提问:这么多的模块,在nginx中究竟是如何使用的呢,模块的功能是如何体现的呢?

这 就要先说一下nginx启动的过程了,nginx是一个master主进程+多个worker子进程的工作模式 (详细会在分析事件处理的时候解释),nginx主进程启动的过程中会按照初始化master、初始化模块、初始化工作进程、(初始化线程、退出线程)、退出工作进程、退出master顺序进行,而在这些子过程内部和子过程之间,又会有读取配置、创建配置、初始化配置、合并配置、http解析、http过滤、http输出、http代理等过程,在这些过程开始前后、过程中、结束前后等时机,nginx调用合适的模块接口完成特定的任务。

所谓的合适模块接口,是各个模块通过一些方式注册到系统内的回调函数,这些回调函数都要符合一定的接口规范,比如上面那个ngx_core_module变量的定义初始化中,那些赋值为NULL的都是函数指针,如果要注册一个回调函数,就可以按照函数指针指定的接口编写函数,然后通过赋值给这些函数指针来注册回调函数,不同的接口被nginx调用的时机是不同的,比如init_master是在初始化master的时候被调用。

到这里,应该对nginx的模块化架构有了一些感性认识了,接下来就进入具体实现方式的分析,会稍微复杂一些,最好能够结合源码加深一下理解
阅读(1553) | 评论(0) | 转发(0) |
0

上一篇:nginx 自动脚本

下一篇:nginx的模块化架构2

给主人留下些什么吧!~~