VS CODE简直是太强大了,不仅仅是前端开发利器,用来调试HotSpot、Nginx也是棒棒的!最关键还免费!
最近刚好在研究Nginx,下面来讲讲如何用VS CODE 调试Nginx。
一:编译运行Nginx
- 修改 /auto/cc/conf 文件,将ngx_compile_opt="-c" 修改为 ngx_compile_opt="-c -g"
- 执行 sudo ./auto/configure --prefix=nginx工程目录 ,如果遇到错误 "the HTTP rewrite module requires the PCRE library",就执行下brew install pcre
- 执行 sudo make
- 执行 ./objs/nginx,打开浏览器访问下 127.0.0.1,没问题的话就可以看到Nginx的欢迎界面了。

二:调试Master进程
Nginx运行模式分为Master进程和Worker进程,让我们先来调试下Master进程吧
- 关掉Nginx的守护进程运行方式,在/conf/nginx.conf 中添加一行:
daemon off;
2. 添加VSCODE调试配置
创建并修改launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/objs/nginx",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
3. 启动调试,爽歪歪!

三:调试 Worker 进程
- 查看 Worker 进程pid

2. 编辑 launch.json,添加 Attach配置
{
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/objs/nginx",
"processId": "806{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/objs/nginx",
"processId": "80636", // 填写 Worker 进程 PID
"MIMode": "lldb"
},
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/objs/nginx",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
3. 切换 debug 模式为 attach

4. 开始 debug,在解析 Http 请求处打断点,刷新浏览器,再次爽歪歪!

是不是很方便,接下来,就尽情的跟随源码旅行吧!