Hi Jason
  • About
  • Keywords
    • keywords - 2021/01
    • Keywords - 2020/08
    • Keywords - 2020/07
  • Note
    • 2021
    • 2020
      • 伊拉克域名.IQ被美国删除的背后以及早期的根域名管理
      • 美国如果把根域名服务器封了,中国会从网络上消失?
      • The Technical Data Guidance
    • 缓慢收藏, 小心整理
      • 泰戈尔诗句节选
    • 金刚经 原文 | 抄经
  • Read
    • 符号
    • 段永平投资问答录
      • 符号的设计
      • 符号形式探寻
      • 作为符号的设计(上篇)
      • 作为符号的设计(下篇)
      • 符号化设计之符号形式探寻
    • Dark Mode
      • Dark Mode的设计要点
      • 一篇吃透 Dark Mode ,搞定“暗黑/深色”适配
    • Apple
      • Apple “无缝”设计之历程
      • Apple 那些“无关紧要”的设计改进
      • HomePod 的设计
      • 解决方案、设计、好设计,Apple UI 设计中的 Tuning
      • Apple 的 Logo 设计
      • J 的艺术,R 的艺术
      • 从圆角到圆角
      • Apple 颜色设计的历程
      • 欲望、逻辑和习惯
      • 反“建筑学”的 Apple Park 将刺激建筑的发展
      • 螺钉,还是胶水?
      • 关于苹果设计的书籍和文章推荐
      • 正面冲撞习惯
      • 从 iOS 7 的电话图标到 polyoxybenzyl…
      • Affordance(可供性)和设计
      • 美的感知力
      • 2010: A Design Odyssey
      • iPad,从 niche 到 mass
  • Source
    • Nginx
      • 前言
      • Nginx入门
      • Nginx 配置文件
      • Nginx 内存池管理
      • Nginx 基本数据结构
      • Nginx 数组结构 ngx_array_t
      • Nginx 链表结构 ngx_list_t
      • Nginx 队列双向链表结构 ngx_queue_t
      • Nginx 哈希表结构 ngx_hash_t
      • Nginx 红黑树结构 ngx_rbtree_t
      • Nginx 模块开发
      • Nginx 启动初始化过程
      • Nginx 配置解析
      • Nginx 中的 upstream 与 subrequest 机制
      • Nginx 源码结构分析
      • Nginx 事件模块
      • Nginx 的 epoll 事件驱动模块
      • Nginx 定时器事件
      • Nginx 事件驱动模块连接处理
      • Nginx 中 HTTP 模块初始化
      • Nginx 中处理 HTTP 请求
      • Untitled
      • Untitled
    • Part 1
      • curl
  • Google Dev
    • 重要概念
      • Google 搜索的工作方式
      • 什么是展示次数、排名和点击次数?
      • 关于我们的统计信息和数据
    • Search Console帮助
      • 指南概览
      • 网站站长指南
      • 常规指南
        • 搜索引擎优化 (SEO) 新手指南
        • 使用 HTTPS 确保网站安全
        • 保持简单的网址结构
        • 向 Google 说明您的出站链接的用意
        • 将网站标记为面向儿童的内容
        • 浏览器兼容性
        • 避免创建重复内容
        • 确保链接可供抓取
        • 借助 Google 搜索进行网站测试的最佳做法
      • 专门面向内容的指南
        • 与 Google 搜索中的 AMP 网页相关的准则
        • AJAX 增强网站
        • 图片和视频
          • Google 图片最佳做法
          • 图片站点地图
          • Google 图片中的图片权限元数据
          • 视频最佳做法
          • 视频 Sitemap 及其替代方案
          • 有关富媒体文件的最佳做法
        • 播客
        • Google 移动
          • 在功能手机上进行移动浏览
          • Web Light:在搜索结果中提供更快速且更精简的移动版网页
          • Google 搜索中的 Web Light 网页对广告网络的支持
          • Google 探索和您的网站
          • 实用资源:面向适合在移动设备上显示的网页的开发者
          • 将移动网络结算费用明确告知用户
          • 将 Android 应用与网站相关联
      • 质量指南
    • Google Cloud CDN
      • 使用拖管实例组设置 Cloud CDN
      • 使用后端存储分区设置 Cloud CDN
      • 使用缓存键
      • 查看日志
  • Guidebook
    • Color Guide
    • Material.io
  • Navigation
    • Google
    • Social & Study
    • Working Tools
Powered by GitBook
On this page
  • Nginx 源码基本结构
  • core 核心模块结构
  • event 事件驱动模型结构
  • http 模块结构
  • Nginx 源码的模块化结构

Was this helpful?

  1. Source
  2. Nginx

Nginx 源码结构分析

Nginx 源码基本结构

学习 Nginx 的构架之前,对 Nginx 源码结构进行简单的分析,可以了解 Nginx 模块结构以及模块之间的关系。充分理解Nginx 的基本构架。解压源码到相应的文件后,我们可以看到有一个存放源码的目录文件src,该目录文件存储Nginx 所有的源代码。首先,我们通过命令查看源码的组织结构:

$ tree -L 1
.
├── core
├── event
├── http
├── mail
├── misc
└── os

6 directories, 0 files

输出结果显示有 6 个目录文件,以下是这些目录文件的功能:

  • core :Nginx的核心源代码,包括常用数据结构的以及Nginx 内核实现的核心代码;

  • event:Nginx事件驱动模型,以及定时器的实现相关代码;

  • http :Nginx 实现http 服务器相关的代码;

  • mail :Nginx 实现邮件代理服务器相关的代码;

  • misc :辅助代码,测试C++头 的兼容性,以及对Google_PerfTools 的支持;

  • os :不同体系统结构所提供的系统函数的封装,提供对外统一的系统调用接口;

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

core 核心模块结构

core 目录中的源码定义了 Nginx 服务器最基本的数据结构以及最基本的核心模块(核心模块为其他模块提供了公共调用的基本功能)。首先看下该核心模块的源码结构:


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

├── ngx_conf_file.c
├── ngx_conf_file.h
├── ngx_config.h

├── ngx_connection.c
├── ngx_connection.h

├── ngx_core.h
├── ngx_cpuinfo.c

├── ngx_crc32.c
├── ngx_crc32.h
├── ngx_crc.h

├── ngx_cycle.c
├── ngx_cycle.h

├── ngx_file.c
├── ngx_file.h

├── ngx_inet.c
├── ngx_inet.h

├── ngx_log.c
├── ngx_log.h
├── ngx_syslog.c
├── ngx_syslog.h

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

├── 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 等事件驱动模型;

.
├── modules
│   ├── ngx_aio_module.c           
│   ├── ngx_devpoll_module.c       
│   ├── ngx_epoll_module.c         
│   ├── ngx_eventport_module.c     
│   ├── ngx_kqueue_module.c        
│   ├── ngx_poll_module.c          
│   ├── ngx_rtsig_module.c         
│   ├── ngx_select_module.c        
│   └── ngx_win32_select_module.c  
├── 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 源码划分为以下几种功能,如下图所示:

  • 核心模块功能:为其他模块提供一些基本功能:字符串处理、时间管理、文件读写等功能;

  • 配置解析:主要包括文件语法检查、配置参数解析、参数初始化等功能;

  • 内存管理:内存池管理、共享内存的分配、缓冲区管理等功能;

  • 事件驱动:进程创建与管理、信号接收与处理、所有事件驱动模型的实现、高级 IO 等功能;

  • 日志管理:错误日志的生成与管理、任务日志的生成与管理等功能;

  • HTTP 服务:提供 Web 服务,包括客户度连接管理、客户端请求处理、虚拟主机管理、服务器组管理等功能;

  • Mail 服务:与 HTTP 服务类似,但是增加了邮件协议的实现;

PreviousNginx 中的 upstream 与 subrequest 机制NextNginx 事件模块

Last updated 4 years ago

Was this helpful?