nginx的优秀除了体现在程序结构以及代码风格上,nginx的源码组织也同样简洁明了,目录结构层次结构清晰,值得我们去学习。nginx的源码目录与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 个目录文件,以下是这些目录文件的功能:

  • core  :Nginx的核心源代码,包括常用数据结构的以及Nginx 内核实现的核心代码;
  • event:Nginx事件驱动模型,以及定时器的实现相关代码;
  • http   :Nginx 实现http 服务器相关的代码;
  • mail  :Nginx 实现邮件代理服务器相关的代码;
  • misc :辅助代码,测试C++头 的兼容性,以及对Google_PerfTools 的支持;
  • os     :不同体系统结构所提供的系统函数的封装,提供对外统一的系统调用接口;

下面主要针对重要的三个目录进行简单的介绍:core 目录、http 目录、event 目录。

1、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

2、event 事件驱动模型结构

event 目录里面包含一种子目录 module 以及一些文件,除了 module 子目录,其他文件提供了事件驱动模型相关数据结构的定义、初始化、事件接收、传递、管理功能以及事件驱动模型调用功能。module 子目录里面的源码实现了Nginx 支持的事件驱动模型:AIO、epoll、kqueue、select、/dev/poll、poll 等事件驱动模型;

.
├── 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

3、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

4、Nginx 源码的模块化结构

根据各模块的功能,可把 Nginx 源码划分为以下几种功能,如下图所示:

  • 核心模块功能:为其他模块提供一些基本功能:字符串处理、时间管理、文件读写等功能;
  • 配置解析:主要包括文件语法检查、配置参数解析、参数初始化等功能;
  • 内存管理:内存池管理、共享内存的分配、缓冲区管理等功能;
  • 事件驱动:进程创建与管理、信号接收与处理、所有事件驱动模型的实现、高级 IO 等功能;
  • 日志管理:错误日志的生成与管理、任务日志的生成与管理等功能;
  • HTTP 服务:提供 Web 服务,包括客户度连接管理、客户端请求处理、虚拟主机管理、服务器组管理等功能;
  • Mail 服务:与 HTTP 服务类似,但是增加了邮件协议的实现。

本文参考自:

https://www.kancloud.cn/digest/understandingnginx/202599

http://tengine.taobao.org/book/chapter_09.html

nginx源码学习_源码结构的更多相关文章

  1. 『TensorFlow』SSD源码学习_其一:论文及开源项目文档介绍

    一.论文介绍 读论文系列:Object Detection ECCV2016 SSD 一句话概括:SSD就是关于类别的多尺度RPN网络 基本思路: 基础网络后接多层feature map 多层feat ...

  2. 【 js 基础 】【 源码学习 】源码设计 (持续更新)

    学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析第二部分:undersc ...

  3. 【 js 基础 】【 源码学习 】源码设计 (更新了backbone分析)

    学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析 第二部分:unders ...

  4. nginx源码学习_数据结构(ngx_int_t)

    nginx中关于整型的数据结构位于src/core/ngx_config.h中 结构比较简单,就是一个typedef的操作,具体如下: typedef intptr_t ngx_int_t; type ...

  5. nginx源码学习_数据结构(ngx_pool_t)

    nginx中关于ngx_pool_t的数据结构位于src/core/ngx_palloc.c和src/core/ngx_palloc.h中,该数据结构主要是和内存池相关的,写下这篇博客前参考了网上很多 ...

  6. nginx源码学习_数据结构(ngx_str_t)

    nginx中关于字符串的数据结构位于src/core/ngx_string.c和src/core/ngx_string.h中 先来看一下数据结构: typedef struct { size_t le ...

  7. 『TensorFlow』SSD源码学习_其四:数据介绍及TFR文件生成

    Fork版本项目地址:SSD 一.数据格式介绍 数据文件夹命名为VOC2012,内部有5个子文件夹,如下, 我们的检测任务中使用JPEGImages文件夹和Annotations文件夹. JPEGIm ...

  8. Vue2.x源码学习笔记-源码目录结构整理

    先从github上下载或者clone一个vue分支项目 https://github.com/vuejs/vue 查看下目录结果 先列出一些目录 Vue |— build 打包相关的配置文件,其中最重 ...

  9. Thrift 源码学习一——源码结构

    Thrift 客户端与服务端的交互图 源码结构 传输层 TTransport: TTransport:客户端传输层抽象基础类,read.write.flush.close 等方法 TSocket 与 ...

随机推荐

  1. Easyui treegrid 无法显示树形结构解决办法

    easyui treegrid 中检查了数据结构没有问题的,但就是不展示树形结构, 检查发现原来是 var columnsAll = [ { title: '任务ID', field: 'TaskID ...

  2. Error (10663): Verilog HDL Port Connection error at led_demo.v(6): output or inout port "led" must be connected to a structural net expression

    错误现象:

  3. 【JSP EL】el表达式判断是否为null

    后台程序放入Model中,从前台el表达式取出来非常方便,但是如果需要处理 当数据为null的时候,怎么办,不为null的时候,怎么办:这个怎么处理呢? <span class="us ...

  4. Sublime Text:格式化插件HTML-CSS-JS Prettify

    Sublime Text:插件HTML-CSS-JS Prettify可以格式化HMTL/CSS/JS 1.安装Node.js 2.Sublime中ctrl+shift+p,输入ip: 3.点击Ins ...

  5. [Android Memory] Android内存管理、监测剖析

    转载自:http://blog.csdn.net/anlegor/article/details/23398785 Android内存管理机制: Android内存管理主要有:LowMemory Ki ...

  6. RUEI 13.1.1版本在OEL 5.7上的安装

    准备工作 ntp的工作和同步 /sbin/chkconfig --list | grep ntpd ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off /sb ...

  7. OS中处理机调度模型和调度算法

    OS中处理机调度模型和调度算法 调度层次 1.1. 高级调度(长程调度,作业调度) 功能:依据某种算法.把在外存队列上处于后备队列的那些作业调入内存.以作业为操做对象. 作业:比程序更为广泛的概念,不 ...

  8. 一次@value取值失败的原因

    网上down了一份源码.启动后报错,通过报错信息定位到这个地方: 之前对这个@Value的实现方式我也没了解过,所以乘机对springboot关于这一块的源码研究了一下.可以参考当时我的一篇分析记录& ...

  9. RocketMQ性能压测分析(转)

    原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2093785 一   机器部署 1.1  机器组成 1台nameserver 1台broker  异步刷盘 2 ...

  10. new Thread(new ThreadStart(this.StartServer))

    Thread .new thUdpServer thUdpServer = new Thread(new ThreadStart(this.StartServer))