nginx的优秀除了体现在程序结构以及代码风格上,nginx的源码组织也同样简洁明了,目录结构层次结构清晰,值得我们去学习。nginx的源码目录与nginx的模块化以及功能的划分是紧密结合,这也使得我们可以很方便地找到相关功能的代码。

下面是nginx源码的目录结构:

  1. .
  2. ├── auto 自动检测系统环境以及编译相关的脚本
  3. ├── cc 关于编译器相关的编译选项的检测脚本
  4. ├── lib nginx编译所需要的一些库的检测脚本
  5. ├── os 与平台相关的一些系统参数与系统调用相关的检测
  6. └── types 与数据类型相关的一些辅助脚本
  7. ├── conf 存放默认配置文件,在make install后,会拷贝到安装目录中去
  8. ├── contrib 存放一些实用工具,如geo配置生成工具(geo2nginx.pl
  9. ├── html 存放默认的网页文件,在make install后,会拷贝到安装目录中去
  10. ├── man nginxman手册
  11. └── src 存放nginx的源代码
  12. ├── core nginx的核心源代码,包括常用数据结构的定义,以及nginx初始化运行的核心代码如main函数
  13. ├── event 对系统事件处理机制的封装,以及定时器的实现相关代码
  14. └── modules 不同事件处理方式的模块化,如selectpollepollkqueue
  15. ├── http nginx作为http服务器相关的代码
  16. └── modules 包含http的各种功能模块
  17. ├── mail nginx作为邮件代理服务器相关的代码
  18. ├── misc 一些辅助代码,测试c++头的兼容性,以及对google_perftools的支持
  19. └── 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 服务器最基本的数据结构以及最基本的核心模块(核心模块为其他模块提供了公共调用的基本功能)。首先看下该核心模块的源码结构:

  1. 实现对各模块的整体控制,是 Nginx 程序 main 函数
  2. ├── nginx.c
  3. ├── nginx.h
  4. 以下是基本数据结构及其操作
  5. ├── ngx_array.c
  6. ├── ngx_array.h
  7. ├── ngx_hash.c
  8. ├── ngx_hash.h
  9. ├── ngx_list.c
  10. ├── ngx_list.h
  11. ├── ngx_queue.c
  12. ├── ngx_queue.h
  13. ├── ngx_radix_tree.c
  14. ├── ngx_radix_tree.h
  15. ├── ngx_rbtree.c
  16. ├── ngx_rbtree.h
  17. ├── ngx_output_chain.c
  18. ├── ngx_buf.c
  19. ├── ngx_buf.h
  20. 整个Nginx 模块构架基本配置管理
  21. ├── ngx_conf_file.c
  22. ├── ngx_conf_file.h
  23. ├── ngx_config.h
  24. 网络连接管理
  25. ├── ngx_connection.c
  26. ├── ngx_connection.h
  27. 定义一些头文件与结构别名
  28. ├── ngx_core.h
  29. ├── ngx_cpuinfo.c
  30. CRC 校验表信息
  31. ├── ngx_crc32.c
  32. ├── ngx_crc32.h
  33. ├── ngx_crc.h
  34. 实现对系统运行过程参数、资源的通用管理
  35. ├── ngx_cycle.c
  36. ├── ngx_cycle.h
  37. 实现文件读写相关的功能
  38. ├── ngx_file.c
  39. ├── ngx_file.h
  40. socket 网络套接字功能
  41. ├── ngx_inet.c
  42. ├── ngx_inet.h
  43. 实现日志输出、管理的相关功能
  44. ├── ngx_log.c
  45. ├── ngx_log.h
  46. ├── ngx_syslog.c
  47. ├── ngx_syslog.h
  48. hash字符串操作
  49. ├── ngx_md5.c
  50. ├── ngx_md5.h
  51. ├── ngx_murmurhash.c
  52. ├── ngx_murmurhash.h
  53. 内存管理相关文件
  54. ├── ngx_open_file_cache.c
  55. ├── ngx_open_file_cache.h
  56. ├── ngx_palloc.c
  57. ├── ngx_palloc.h
  58. ├── ngx_shmtx.c
  59. ├── ngx_shmtx.h
  60. ├── ngx_slab.c
  61. ├── ngx_slab.h
  62. PCRE 上层封装
  63. ├── ngx_parse.c
  64. ├── ngx_parse.h
  65. 反向代理的协议信息
  66. ├── ngx_proxy_protocol.c
  67. ├── ngx_proxy_protocol.h
  68. 实现支持正则表达式
  69. ├── ngx_regex.c
  70. ├── ngx_regex.h
  71. 字符串处理功能
  72. ├── ngx_string.c
  73. ├── ngx_string.h
  74. 时间获取与管理功能
  75. ├── ngx_times.c
  76. └── ngx_times.h
  77. 其他文件
  78. ├── ngx_resolver.c
  79. ├── ngx_resolver.h
  80. ├── ngx_sha1.h
  81. ├── ngx_spinlock.c
  82. ├── ngx_crypt.c
  83. ├── ngx_crypt.h

2、event 事件驱动模型结构

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

  1. .
  2. ├── modules
  3.    ├── ngx_aio_module.c AIO 事件驱动模型
  4.    ├── ngx_devpoll_module.c dev/poll 事件驱动模型
  5.    ├── ngx_epoll_module.c epoll 事件驱动模型
  6.    ├── ngx_eventport_module.c 事件驱动模型端口
  7.    ├── ngx_kqueue_module.c kqueue 事件驱动模型
  8.    ├── ngx_poll_module.c poll 事件驱动模型
  9.    ├── ngx_rtsig_module.c rtsing 事件驱动模型
  10.    ├── ngx_select_module.c Linux 平台下的 select 事件驱动模型
  11.    └── ngx_win32_select_module.c Win32 平台下的 select 事件驱动模型
  12. ├── ngx_event_accept.c
  13. ├── ngx_event_busy_lock.c
  14. ├── ngx_event_busy_lock.h
  15. ├── ngx_event.c
  16. ├── ngx_event_connect.c
  17. ├── ngx_event_connect.h
  18. ├── ngx_event.h
  19. ├── ngx_event_mutex.c
  20. ├── ngx_event_openssl.c
  21. ├── ngx_event_openssl.h
  22. ├── ngx_event_openssl_stapling.c
  23. ├── ngx_event_pipe.c
  24. ├── ngx_event_pipe.h
  25. ├── ngx_event_posted.c
  26. ├── ngx_event_posted.h
  27. ├── ngx_event_timer.c
  28. └── ngx_event_timer.h
  29. 1 directory, 26 files

3、http 模块结构

http 目录和 event 目录一样,通用包含了模块实现源码的 module 目录文件以及一些结构定义、初始化、网络连接建立、管理、关闭,以及数据报解析、服务器组管理等功能的源码文件。module 目录文件实现了HTTP 模块的功能。

  1. .
  2. ├── modules
  3. ├── ngx_http_busy_lock.c
  4. ├── ngx_http_busy_lock.h
  5. ├── ngx_http.c
  6. ├── ngx_http_cache.h
  7. ├── ngx_http_config.h
  8. ├── ngx_http_copy_filter_module.c
  9. ├── ngx_http_core_module.c
  10. ├── ngx_http_core_module.h
  11. ├── ngx_http_file_cache.c
  12. ├── ngx_http.h
  13. ├── ngx_http_header_filter_module.c
  14. ├── ngx_http_parse.c
  15. ├── ngx_http_parse_time.c
  16. ├── ngx_http_postpone_filter_module.c
  17. ├── ngx_http_request_body.c
  18. ├── ngx_http_request.c
  19. ├── ngx_http_request.h
  20. ├── ngx_http_script.c
  21. ├── ngx_http_script.h
  22. ├── ngx_http_spdy.c
  23. ├── ngx_http_spdy_filter_module.c
  24. ├── ngx_http_spdy.h
  25. ├── ngx_http_spdy_module.c
  26. ├── ngx_http_spdy_module.h
  27. ├── ngx_http_special_response.c
  28. ├── ngx_http_upstream.c
  29. ├── ngx_http_upstream.h
  30. ├── ngx_http_upstream_round_robin.c
  31. ├── ngx_http_upstream_round_robin.h
  32. ├── ngx_http_variables.c
  33. ├── ngx_http_variables.h
  34. └── ngx_http_write_filter_module.c
  35. 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. [51nod1538]一道难题

    先观察一下题目给出的式子:对所有满足$\begin{align*}\sum\limits_{i=1}^na_ib_i=m\end{align*}$的$b_{1\cdots n}$,计算$\begin{ ...

  2. 使用jQuery操作dom(追加和删除样式-鼠标移入移出)练习

    1.实现鼠标移入则添加背景色,鼠标移出则删除背景色 <!DOCTYPE html> <html> <head> <title>test1.html< ...

  3. ntp流量放大攻击分析

    最近,听说挂在网络上的设备进行时间同步成功率低,YS需要架设自己的NTP服务器,这玩意第一时间能让人想到NTP流量放大攻击,这也是一种比较古老的攻击方式,检测了一下发现所使用的OS默认已经进行了加固, ...

  4. 激活office2010出现“Failed to inject memory”错误

    使用Office 2010 Toolkit 2.2.3激活office2010的时候,出现Failed to inject memory!错误,原因是前期使用KM激活过office 2010,然后默认 ...

  5. insert语句太长,有StringBuilder优化一下

    private void btnSave_Click(object sender, RoutedEventArgs e) { if (IsInsert) { //假设日历控件没有选日期,那帮它赋一个当 ...

  6. Android Http POST文件上传之-----RFC1867协议

    RFC1867协议介绍            RFC1867协议主要是在HTTP协议的基础上为INPUT标签添加了file属性.同一时候限定了Form的method必须为POST,ENCTYPE必须为 ...

  7. 自建Saltstack的repo软件源仓库

    因为Saltstack自己的repo源是在国外,在国内服务器yum安装Saltstack的时候下载软件包就非常慢,很多情况下还经常下载失败,其实软件包总大小只有10M左右,如果这样安装多台minion ...

  8. 为小团队协作和个人任务管理而生的Team应用

    今天主要给大家讲讲个人用户如何操作附个下载地址:https://coding.net/s/7a64f0d3-0fc3-4618-a490-d29651dae06b1.可直接在主页添加任务 任务被放在默 ...

  9. Gitlab安装部署及基础操作

      环境说明 系统版本 CentOS 7.2 x86_64(较新版本的gitlab集成了更多功能,顺利运行起来的硬件要求较高,这里给了3G内存) 软件版本 gitlab-ce-10.8.4 GitLa ...

  10. observer pattern 之我见

    所谓模式,更多的是一种想法,完全没必要拘泥于代码细节.观察者模式更多体现了两个独立的类利用接口完成一件本应该很复杂的事情 --------------------------------------- ...