这是这条线的第 0 章。后面每一章都要在这棵树上指认函数、追调用链,
所以先解决两个问题:树长什么样、怎么在里面不迷路。读完之后你应该能回答:
- nginx 的构建系统是什么?为什么它既不像 autoconf 也不像 meson?
./configure跑完之后,objs/里多了哪几个文件?各是什么?src/下的六个目录各管什么?一个 HTTP 模块该放在哪?- 想知道「
ngx_xxx在哪」,一条命令能不能找到?
本文所有命令与行号基于 /work/ai/cwork/nginx,
版本是 nginx 1.31.11(mainline,HEAD e76b6746c)。
1.1 先建立体量感
这棵树有多大:
| 目录 | .c | .h | 管什么 |
|---|---|---|---|
src/core/ |
43 | 43 | 内存、字符串、容器、配置文件、连接、日志、模块系统 |
src/event/ |
10 | 7 | 事件循环抽象 + modules/ 下 5 种事件后端 |
src/http/ |
18 | 10 | HTTP 协议核心 |
src/http/modules/ |
60 | 3 | 60 个 HTTP 功能模块 |
src/http/v2/ src/http/v3/ |
13 | 7 | HTTP/2 与 HTTP/3 |
src/os/unix/ |
36 | 31 | 系统调用封装 |
src/stream/ |
27 | 6 | TCP 代理 |
src/mail/ |
14 | 5 | 邮件代理 |
| 合计 | 268 | 142 | 约 25.8 万行(含注释) |
和 PostgreSQL 比一比:PostgreSQL 的代码量是这个的十几倍,
但 nginx 的抽象层数少得多 —— 一个请求的完整生命周期,
在src/event/+src/http/+src/core/三层里就能走完。nginx 的难不在「大」,在**「密」**:每一个
ngx_开头的函数,
都要先想清楚它是在 core 层、event 层还是 http 层说话。
版本号在源码的最顶上,src/core/nginx.h:12:
// src/core/nginx.h:12-14
#define nginx_version 1031011
#define NGINX_VERSION "1.31.11"
#define NGINX_VER "nginx/" NGINX_VERSION
1031011 是 1.31.11 的十进制拼法 —— 后面所有 NGX_VERSION 相关的
宏(比如 ngx_feature 之类)都以它为基准。
1.2 构建系统:一堆 shell 脚本
nginx 不用 autoconf,不用 meson,不用 cmake。./configure 本身是一个 shell 脚本,它把 auto/ 目录下的十几个脚本
按顺序 source 一遍。
主流程(configure 开头的十几行):
| 顺序 | 脚本 | 干什么 |
|---|---|---|
| 1 | auto/options |
解析命令行选项(--prefix、--with-cc-opt、…) |
| 2 | auto/init |
定义输出目录与生成文件名 |
| 3 | auto/sources |
列出全部源文件 |
| 4 | auto/have / auto/feature |
探测系统特性,写成 #define |
| 5 | auto/modules |
决定这次编译带哪些模块 |
| 6 | auto/make |
生成 objs/Makefile 与 objs/ngx_modules.c |
auto/init:6-17 定下了所有生成物的名字:
# auto/init:6-17
NGX_MAKEFILE=$NGX_OBJS/Makefile
NGX_MODULES_C=$NGX_OBJS/ngx_modules.c
NGX_AUTO_HEADERS_H=$NGX_OBJS/ngx_auto_headers.h
NGX_AUTO_CONFIG_H=$NGX_OBJS/ngx_auto_config.h
NGX_AUTOTEST=$NGX_OBJS/autotest
NGX_AUTOCONF_ERR=$NGX_OBJS/autoconf.err
# STUBs
NGX_ERR=$NGX_OBJS/autoconf.err
MAKEFILE=$NGX_OBJS/Makefile
注意
auto/init:16-17这两行:NGX_ERR和MAKEFILE指向同一个文件。
也就是说make报错了,去objs/autoconf.err看原因。
1.2.1 编译
cd /work/ai/cwork/nginx
# 最简:默认模块
./configure
make -j$(nproc)
# 常用组合
./configure \
--prefix=/work/ai/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-stream \
--with-stream_ssl_module \
--with-threads \
--with-cc-opt="-O2 -g -Wall"
make -j$(nproc)
make install
选项的完整列表在 auto/options:452 之后(--help 打出来的就是那一段)。
几个要紧的:
--prefix=—— 安装前缀,默认/usr/local/nginx(auto/options:201)。
编译时的 prefix 会被写进二进制,改路径要重新编译,
除非运行期用-p覆盖。--with-http_ssl_module—— 开 HTTPS(auto/options:245)。--without-http_gzip_module—— 关 gzip(auto/options:272)。--with-stream—— 开 TCP 代理子系统(auto/options:342)。--with-cc-opt=—— 额外编译选项(auto/options:382)。
想 gdb 跟源码就写-O0 -g3。
1.2.2 模块是「选」出来的
auto/modules 是个 800 多行的 shell 脚本,里面全是 if [ $XXX = YES ]。
比如 HTTP/2 与 HTTP/3 的开关在 auto/modules:447 和 auto/modules:464:
# auto/modules:447-448
if [ $HTTP_V2 = YES ]; then
have=NGX_HTTP_V2 . auto/have
auto/have 做的事非常朴素 —— 往 objs/ngx_auto_config.h 里追加一个宏:
# auto/have:6-12
cat << END >> $NGX_AUTO_CONFIG_H
#ifndef $have
#define $have 1
#endif
END
于是 src/ 里的代码就能用 #if (NGX_HTTP_V2) 把没编进来的模块整段排除掉。
这就是 nginx 的「按需编译」:一个模块在不在,取决于
auto/modules有没有把它选上、进而取决于objs/ngx_auto_config.h里
有没有那个宏。读源码时必须先确认这一点 —— 你在
src/http/modules/里看到的ngx_http_grpc_module.c,可能压根没被编进你手上的那个二进制。
1.2.3 objs/ 里到底有什么
./configure && make 之后,objs/ 目录里有:
| 文件 | 谁生成 | 是什么 |
|---|---|---|
Makefile |
auto/make |
真正的构建规则 |
ngx_modules.c |
auto/make:512-547 |
本次编译的模块清单 |
ngx_auto_config.h |
auto/have / auto/feature |
探测出来的特性宏 |
ngx_auto_headers.h |
auto/headers |
头文件存在性宏 |
autoconf.err |
各探测脚本 | 报错都往这里写 |
nginx |
链接 | 最终二进制 |
ngx_modules.c 特别值得看一眼 —— 它是这次编译带了哪些模块的权威清单:
// objs/ngx_modules.c(生成的,节选)
extern ngx_module_t ngx_core_module;
extern ngx_module_t ngx_http_static_module;
...
char *ngx_module_names[] = {
"ngx_core_module",
"ngx_http_static_module",
...
};
它的生成逻辑在 auto/make:512-547:把 NGX_MODULES 变量里的模块名
一个一个 extern 出来,最后拼成 ngx_module_t *ngx_modules[] 数组。
1.3 装完之后东西在哪
make install 之后,按 --prefix 铺开:
<prefix>/
├── conf/nginx.conf 默认配置文件
├── html/ 默认站点根(index.html)
├── logs/ access.log / error.log
├── sbin/nginx 二进制
└── client_body_temp/ 客户端请求体临时目录(proxy 缓冲落盘也在这)
临时目录是 proxy 的关键落点:
当响应头大于proxy_buffer_size、或者请求体大于client_body_buffer_size时,nginx 会把数据写到磁盘上的临时文件。
磁盘慢,这条路径就慢 —— 这是「反代为什么有时候特别慢」的第一个现场。
1.4 src/ 六层地图
src/
├── core/ 43 个 .c —— 与协议无关的地基
├── event/ 10 个 .c —— 事件循环抽象 + 5 种后端
├── http/ 18 个 .c —— HTTP 协议核心
│ ├── modules/ 60 个 .c —— 各功能模块
│ ├── v2/ HTTP/2
│ └── v3/ HTTP/3
├── os/unix/ 36 个 .c —— 系统调用封装
├── stream/ 27 个 .c —— TCP 代理(复用 event + core)
└── mail/ 14 个 .c —— 邮件代理(复用 event + core)
各层的职责边界,画的时候按这张图:
| 层 | 回答什么 | 关键结构体 |
|---|---|---|
core/ |
内存从哪来、配置怎么读、连接怎么记、日志怎么写 | ngx_pool_t(src/core/ngx_palloc.h:57)、ngx_cycle_t(src/core/ngx_cycle.h:39)、ngx_connection_t(src/core/ngx_connection.h:125) |
event/ |
「有事件了,去处理」这件事本身 | ngx_event_t(src/event/ngx_event.h:19) |
http/ |
HTTP 语义:请求、响应、虚拟主机、location | ngx_http_request_t(src/http/ngx_http_request.h:386) |
os/unix/ |
「这个系统上怎么干活」 | — |
最关键的一条边界:core/ 不知道 HTTP,http/ 不知道 epoll。http/ 只认 ngx_event_t,epoll 是 event/ 内部的事。
这就是为什么 nginx 能把 HTTP/3 也塞进同一套事件循环里。
1.5 三条读源码的入口路径
拿到一棵树,先要决定「从哪进」。nginx 有三条正路:
路径一:从模块进(查配置行为)
想搞清 proxy_buffer_size 到底干了什么 —— 从指令名反查:
grep -rn "proxy_buffer_size" src/→ 落在src/http/modules/ngx_http_proxy_module.c的ngx_command_t数组里;- 那条
ngx_command_t的set函数指针,就是这条指令的处理器; ngx_command_t定义在src/core/ngx_conf_file.h:77:
// src/core/ngx_conf_file.h:77-84
struct ngx_command_s {
ngx_str_t name;
ngx_uint_t type;
char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
ngx_uint_t conf;
ngx_uint_t offset;
void *post;
};
type 说的是「这条指令吃几个参数」—— NGX_CONF_TAKE2 就是两个,NGX_CONF_BLOCK 表示后面跟一个 {} 块。conf + offset 说的是「结果写到配置结构的哪个字段」。
路径二:从模块清单进(看这次编译带了什么)
ngx_modules[] 这个数组在 src/core/ngx_module.h:278 声明:
// src/core/ngx_module.h:278-278
extern ngx_module_t *ngx_modules[];
它的内容是 objs/ngx_modules.c 生成的 —— 编译期决定的。
每个模块的结构体(src/core/ngx_module.h:223)长这样:
// src/core/ngx_module.h:223-241
struct ngx_module_s {
ngx_uint_t ctx_index;
ngx_uint_t index;
char *name;
ngx_uint_t spare0;
ngx_uint_t spare1;
ngx_uint_t version;
const char *signature;
void *ctx;
ngx_command_t *commands;
ngx_uint_t type;
ngx_int_t (*init_master)(ngx_log_t *log);
ngx_int_t (*init_module)(ngx_cycle_t *cycle);
四个生命周期钩子一目了然:
| 钩子 | 在哪个进程跑 | 干什么 |
|---|---|---|
init_master |
master | master 启动时跑一次 |
init_module |
master | 每次加载配置后跑(reload 也跑) |
init_process |
worker | 每个 worker 起来时跑一次 |
exit_process |
worker | worker 退出时跑 |
init_module在 reload 时也会跑 —— 所以「模块的初始化只做一次」
是错的假设。写动态模块时这里最容易出 bug。
路径三:从请求进(追一次请求)
想搞清「一个请求到底走了哪些函数」,从 ngx_http_request_t 出发
(src/http/ngx_http_request.h:386),顺着 r->main 的调用链往下追。
这一条是第 05 章之后的主线。
1.6 定位工具
cd /work/ai/cwork/nginx
# 1. 按函数名找定义(先搜分号,确定是定义不是调用)
grep -rn "^ngx_http_upstream_init(" src/
# 2. 按指令名找它注册在哪个模块
grep -rn "proxy_buffer_size" src/http/modules/ngx_http_proxy_module.c
# 3. 按结构体找所有用到它的地方
grep -rn "ngx_http_request_t" src/http/ | wc -l
# 4. 看某个文件里所有函数定义(ctags 装了就用 ctags)
grep -n "^ngx_[a-z_]*(" src/http/ngx_http_upstream.c
ctags / cscope 在这个树上非常好用:
ctags -R -f tags src/ # 生成 tags
cscope -R -f cscope.out src/ # 生成 cscope 数据库
再配一个跳转脚本(~/.vimrc):
set tags+=/work/ai/cwork/nginx/tags
一个阅读纪律:nginx 的函数名统一
ngx_前缀 + 模块名 + 动作,
比如ngx_http_upstream_init、ngx_event_accept、ngx_slab_alloc。
看到ngx_http_开头就知道在 HTTP 层,看到ngx_event_开头就知道在事件层。
命名就是分层 —— 这是这棵树最友好的地方,认准了就不会迷路。
1.7 小结
- 构建系统是
auto/下一堆 shell 脚本,跑完生成objs/Makefile、objs/ngx_modules.c、objs/ngx_auto_config.h。 - 模块是选出来的:一个模块在不在,取决于
auto/modules有没有选它。 src/分六层,core/event/http/os/stream/mail,
层与层之间只认结构体,不认实现。- 三条入口:指令 → 模块(查行为)、模块清单(查这次编了什么)、
请求结构体(追流程)。 ngx_前缀就是分层 —— 认它,别迷路。
下一篇讲 master 与 worker 进程模型:这两个进程分别干什么、
为什么 worker 数等于核数、reload 的时候旧 worker 怎么优雅退场。
参考
- 源码树:
/work/ai/cwork/nginx(nginx 1.31.11,HEADe76b6746c) - 官方文档:Building nginx 与
Command-line parameters - 构建脚本入口:
auto/options、auto/init、auto/sources、auto/modules