### 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 程序 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 等事件驅動模型;
~~~
.
├── 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 源碼劃分為以下幾種功能,如下圖所示:

- 核心模塊功能:為其他模塊提供一些基本功能:字符串處理、時間管理、文件讀寫等功能;
- 配置解析:主要包括文件語法檢查、配置參數解析、參數初始化等功能;
- 內存管理:內存池管理、共享內存的分配、緩沖區管理等功能;
- 事件驅動:進程創建與管理、信號接收與處理、所有事件驅動模型的實現、高級 IO 等功能;
- 日志管理:錯誤日志的生成與管理、任務日志的生成與管理等功能;
- HTTP 服務:提供 Web 服務,包括客戶度連接管理、客戶端請求處理、虛擬主機管理、服務器組管理等功能;
- Mail 服務:與 HTTP 服務類似,但是增加了郵件協議的實現;
- 前言
- 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 請求
- Nginx 中 upstream 機制的實現
- Nginx 中 upstream 機制的負載均衡