nginx的优秀除了体如今程序结构以及代码风格上,nginx的源码组织也一样简洁明了,目录结构层次结构清晰,值得咱们去学习。nginx的源码目录与nginx的模块化以及功能的划分是紧密结合,这也使得咱们能够很方便地找到相关功能的代码。html
下面是nginx源码的目录结构:nginx
.
├── auto 自动检测系统环境以及编译相关的脚本 │ ├── cc 关于编译器相关的编译选项的检测脚本 │ ├── lib nginx编译所须要的一些库的检测脚本 │ ├── os 与平台相关的一些系统参数与系统调用相关的检测 │ └── types 与数据类型相关的一些辅助脚本 ├── conf 存放默认配置文件,在make install后,会拷贝到安装目录中去 ├── contrib 存放一些实用工具,如geo配置生成工具(geo2nginx.pl) ├── html 存放默认的网页文件,在make install后,会拷贝到安装目录中去 ├── man nginx的man手册 └── src 存放nginx的源代码 ├── core nginx的核心源代码,包括经常使用数据结构的定义,以及nginx初始化运行的核心代码如main函数 ├── event 对系统事件处理机制的封装,以及定时器的实现相关代码 │ └── modules 不一样事件处理方式的模块化,如select、poll、epoll、kqueue等 ├── http nginx做为http服务器相关的代码 │ └── modules 包含http的各类功能模块 ├── mail nginx做为邮件代理服务器相关的代码 ├── misc 一些辅助代码,测试c++头的兼容性,以及对google_perftools的支持 └── os 主要是对各类不一样体系统结构所提供的系统函数的封装,对外提供统一的系统调用接口
对于上面src文件夹,输出结果显示有 6 个目录文件,如下是这些目录文件的功能:c++
下面主要针对重要的三个目录进行简单的介绍:core 目录、http 目录、event 目录。正则表达式
一、core 核心模块结构服务器
core 目录中的源码定义了 Nginx 服务器最基本的数据结构以及最基本的核心模块(核心模块为其余模块提供了公共调用的基本功能)。首先看下该核心模块的源码结构:网络
实现对各模块的总体控制,是 Nginx 程序 main 函数 ├── nginx.c ├── nginx.h 如下是基本数据结构及其操做 ├── ngx_array.c ├── ngx_array.h ├── ngx_hash.c ├── ngx_hash.h ├── ngx_list.c ├── ngx_list.h ├── ngx_queue.c ├── ngx_queue.h ├── ngx_radix_tree.c ├── ngx_radix_tree.h ├── ngx_rbtree.c ├── ngx_rbtree.h ├── ngx_output_chain.c ├── ngx_buf.c ├── ngx_buf.h 整个Nginx 模块构架基本配置管理 ├── ngx_conf_file.c ├── ngx_conf_file.h ├── ngx_config.h 网络链接管理 ├── ngx_connection.c ├── ngx_connection.h 定义一些头文件与结构别名 ├── ngx_core.h ├── ngx_cpuinfo.c CRC 校验表信息 ├── ngx_crc32.c ├── ngx_crc32.h ├── ngx_crc.h 实现对系统运行过程参数、资源的通用管理 ├── ngx_cycle.c ├── ngx_cycle.h 实现文件读写相关的功能 ├── ngx_file.c ├── ngx_file.h socket 网络套接字功能 ├── ngx_inet.c ├── ngx_inet.h 实现日志输出、管理的相关功能 ├── ngx_log.c ├── ngx_log.h ├── ngx_syslog.c ├── ngx_syslog.h hash字符串操做 ├── ngx_md5.c ├── ngx_md5.h ├── ngx_murmurhash.c ├── ngx_murmurhash.h 内存管理相关文件 ├── ngx_open_file_cache.c ├── ngx_open_file_cache.h ├── ngx_palloc.c ├── ngx_palloc.h ├── ngx_shmtx.c ├── ngx_shmtx.h ├── ngx_slab.c ├── ngx_slab.h PCRE 上层封装 ├── ngx_parse.c ├── ngx_parse.h 反向代理的协议信息 ├── ngx_proxy_protocol.c ├── ngx_proxy_protocol.h 实现支持正则表达式 ├── ngx_regex.c ├── ngx_regex.h 字符串处理功能 ├── ngx_string.c ├── ngx_string.h 时间获取与管理功能 ├── ngx_times.c └── ngx_times.h 其余文件 ├── ngx_resolver.c ├── ngx_resolver.h ├── ngx_sha1.h ├── ngx_spinlock.c ├── ngx_crypt.c ├── ngx_crypt.h
二、event 事件驱动模型结构数据结构
event 目录里面包含一种子目录 module 以及一些文件,除了 module 子目录,其余文件提供了事件驱动模型相关数据结构的定义、初始化、事件接收、传递、管理功能以及事件驱动模型调用功能。module 子目录里面的源码实现了Nginx 支持的事件驱动模型:AIO、epoll、kqueue、select、/dev/poll、poll 等事件驱动模型;socket
.
├── modules │ ├── ngx_aio_module.c AIO 事件驱动模型 │ ├── ngx_devpoll_module.c dev/poll 事件驱动模型 │ ├── ngx_epoll_module.c epoll 事件驱动模型 │ ├── ngx_eventport_module.c 事件驱动模型端口 │ ├── ngx_kqueue_module.c kqueue 事件驱动模型 │ ├── ngx_poll_module.c poll 事件驱动模型 │ ├── ngx_rtsig_module.c rtsing 事件驱动模型 │ ├── ngx_select_module.c Linux 平台下的 select 事件驱动模型 │ └── ngx_win32_select_module.c Win32 平台下的 select 事件驱动模型 ├── ngx_event_accept.c ├── ngx_event_busy_lock.c ├── ngx_event_busy_lock.h ├── ngx_event.c ├── ngx_event_connect.c ├── ngx_event_connect.h ├── ngx_event.h ├── ngx_event_mutex.c ├── ngx_event_openssl.c ├── ngx_event_openssl.h ├── ngx_event_openssl_stapling.c ├── ngx_event_pipe.c ├── ngx_event_pipe.h ├── ngx_event_posted.c ├── ngx_event_posted.h ├── ngx_event_timer.c └── ngx_event_timer.h 1 directory, 26 files
三、http 模块结构模块化
http 目录和 event 目录同样,通用包含了模块实现源码的 module 目录文件以及一些结构定义、初始化、网络链接创建、管理、关闭,以及数据报解析、服务器组管理等功能的源码文件。module 目录文件实现了HTTP 模块的功能。函数
.
├── modules ├── ngx_http_busy_lock.c ├── ngx_http_busy_lock.h ├── ngx_http.c ├── ngx_http_cache.h ├── ngx_http_config.h ├── ngx_http_copy_filter_module.c ├── ngx_http_core_module.c ├── ngx_http_core_module.h ├── ngx_http_file_cache.c ├── ngx_http.h ├── ngx_http_header_filter_module.c ├── ngx_http_parse.c ├── ngx_http_parse_time.c ├── ngx_http_postpone_filter_module.c ├── ngx_http_request_body.c ├── ngx_http_request.c ├── ngx_http_request.h ├── ngx_http_script.c ├── ngx_http_script.h ├── ngx_http_spdy.c ├── ngx_http_spdy_filter_module.c ├── ngx_http_spdy.h ├── ngx_http_spdy_module.c ├── ngx_http_spdy_module.h ├── ngx_http_special_response.c ├── ngx_http_upstream.c ├── ngx_http_upstream.h ├── ngx_http_upstream_round_robin.c ├── ngx_http_upstream_round_robin.h ├── ngx_http_variables.c ├── ngx_http_variables.h └── ngx_http_write_filter_module.c 1 directory, 32 files
四、Nginx 源码的模块化结构
根据各模块的功能,可把 Nginx 源码划分为如下几种功能,以下图所示:
本文参考自:
https://www.kancloud.cn/digest/understandingnginx/202599
http://tengine.taobao.org/book/chapter_09.html