English

调试 nginx 源码

5/5/2025
16

Nginx(发音为 “engine-x”)是一款高性能、轻量级的 Web 服务器、反向代理服务器、负载均衡器 和 邮件代理服务器,由俄罗斯工程师 Igor Sysoev 开发,于 2004 年首次发布,目前由 Nginx Inc. 维护并开源(基于 BSD 许可证)

作为一个合格的Web 开发者,怎么能不研究一下nginx 的源码,本文分别介绍使用Clion 和 vscode 调试 nginx 源码的方法

首先 clone nginx 代码到本地, 然后用clion 打开项目

1. 使用 Custom Build Application

选择 Edit Configurations , 在 target 选择框后 点击 Configure Custom Build Targets. 


添加一个 RunNginx target 如下

Build 填写 build 这是 执行 configure 后生成的 makefile target, 可以在makefile 中找到这个target,其作用是贬义词 nginx 可执行文件

调试 nginx 源码
调试 nginx 源码

添加好了 RunNginx target 后, 在Executable 中 选择生产的 nginx 可执行文件。

先手动执行一次命令:然后就可以在objs 目录下面看到 nginx 可以执行文件。

Bash
# 配置 
./auto/configure --with-debug --prefix=./build 

# 编译
make

在 Program arguments 中输入上图所示的 参数, 
-p 指定 nginx 安装目录, 这里可以选择 nginx 源码的目录

-c 指定 配置文件地址, 这里可以选择 源码中的 nginx.conf 文件

添加工作目录配置

调试 nginx 源码

保存配置
在源码下面创建 logs 目录 用于存放运行中的日志文件

接着 修改 nginx.conf 文件, 添加 daemon off , 因为nginx 默认是以守护进程的方式运行的,这里需要关闭 守护进程模式。 添加日志 配置, 

调试 nginx 源码


勾选 run with root privileges, 如果配置了 低于1024 端口的http server。

调试 nginx 源码

 如果不想以root 权限启动的话, 需要修改一下 配置文件中的 80 端口为 一个大于1024 的端口号。

调试 nginx 源码

最后点击 调试, 可以看到 进入 debug 状态了。

调试 nginx 源码

2. 使用 gdbserver 和 clion Remote debug

首先与上一部一样 configure 并build 项目


如果没有安装的话 先安装gdbserver

Bash
sudo apt install gdbserver

启动gdbserver

Plaintext
sudo gdbserver localhost:2333  ./objs/nginx -p /home/hayden/code/git/nginx -c /home/hayden/code/git/nginx/conf/nginx.conf 

配置Clion Remote Debug

调试 nginx 源码

主要就是 rarget remote 项 填写的 地址 与 启动 gdbserver 的时候一致

path mapping 由于是在本地 直接 填 2 个 / 就行了。

3. 调试 fork 的进程

1.右键点击 main thread 然后勾选 follow Child On Fork 选项。

调试 nginx 源码


2.在fork 新的进程之前 输入一下命令到 GDB 控制台

调试 nginx 源码

set follow-fork-mode child

set detach-on-fork off

3.在项目下面创建 gdb init 脚本

关于 gdb init 文件 参考 文档


调试 nginx 源码
Bash
# ~/.gdbinit

set follow-fork-mode child
set detach-on-fork off

使用vscode 调试 nginx 源码

1. 连接 gdbserver 调试

2. 启动 gdb 调试

2个配置如下

JSON
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gdbserver Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/objs/nginx",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerServerAddress": "localhost:2333",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Nginx Debug Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/objs/nginx",
            "args": ["-p", "/home/hayden/code/git/nginx", "-c", "/home/hayden/code/git/nginx/conf/nginx.conf"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                },
                {
                    "description": "set follow-fork-mode child",
                    "text": "-gdb-set follow-fork-mode child",
                    "ignoreFailures": false
                },
                {
                    "description": "set detach-on-fork off",
                    "text": "-gdb-set detach-on-fork off",
                    "ignoreFailures": false
                }
            ]
        }


    ]
}
调试 nginx 源码



Update:
since the .gitignore file in nginx project had ignored /tmp directory so its better to using this tmp directory to store the build files.
so the build command will be like this:

Bash
./auto/configure --with-debug --prefix=./tmp 

make install -j5

then using the vscode config like this:

JSON
{
            "name": "Nginx Debug Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/tmp/sbin/nginx",
            "args": ["-p", "${workspaceFolder}/tmp", "-c", "${workspaceFolder}/tmp/conf/nginx.conf"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                },
                {
                    "description": "set follow-fork-mode child",
                    "text": "-gdb-set follow-fork-mode child",
                    "ignoreFailures": false
                },
                {
                    "description": "set detach-on-fork off",
                    "text": "-gdb-set detach-on-fork off",
                    "ignoreFailures": false
                }
            ]
        }