Nginx 源码结构分析

来源:互联网 发布:网络侵权 起诉书 编辑:程序博客网 时间:2024/04/30 22:51

原文出处:http://blog.csdn.net/chenhanzhun/article/details/42742097

Nginx 源码基本结构

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

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ tree -L 1  
  2. .  
  3. ├── core  
  4. ├── event  
  5. ├── http  
  6. ├── mail  
  7. ├── misc  
  8. └── os  
  9.   
  10. 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 服务器最基本的数据结构以及最基本的核心模块(核心模块为其他模块提供了公共调用的基本功能)。首先看下该核心模块的源码结构:

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

event 事件驱动模型结构

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  30. 1 directory, 26 files  

http 模块结构

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

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  36. 1 directory, 32 files  

Nginx 源码的模块化结构

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


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