一. 編譯運(yùn)行Nginx
詳細(xì)過(guò)程可參考博文:Nginx源碼編譯安裝教程
配置調(diào)試功能
一定要開啟Nginx調(diào)試功能:
修改 ./auto/cc/conf :
ngx_compile_opt="-c" 改為 ngx_compile_opt="-c -g"
安裝Nginx
cd nginx-1.10.3
sudo ./configure --prefix=/usr/local/nginx --with-stream --with-debug
sudo make
sudo make install
更改Nginx程序目錄權(quán)限
必須修改文件夾權(quán)限,要不然VScode將沒有權(quán)限啟動(dòng)Nginx。
Nginx安裝目錄為configure命令中--prefix所設(shè)置的目錄:
sudo ./configure --prefix=/usr/local/nginx --with-stream --with-debug
修改文件夾權(quán)限:
chmod -R 777 /usr/local/nginx
配置nginx.conf
需要關(guān)閉Nginx守護(hù)進(jìn)程運(yùn)行方式,從而保證每次關(guān)閉debugger都能關(guān)閉nginx服務(wù)。
- 切記不可以關(guān)閉master_process,不然master進(jìn)程和worker進(jìn)程將為同一進(jìn)程,并且無(wú)法調(diào)試nginx工作進(jìn)程。
- worker_processesss設(shè)為1,方便調(diào)試。
在/conf/nginx.conf 中修改如下:
daemon off;
#master_process off;
worker_processes 1;
二. 調(diào)試Master進(jìn)程
1. 添加VSCode調(diào)試配置
在Nginx中打開Nginx源碼,并添加調(diào)試配置,如下圖。

image.png
2. 修改launch.json
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// 調(diào)試nginx Master進(jìn)程
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/objs/nginx",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
3. 啟動(dòng)調(diào)試

image.png
三. 調(diào)試Worker進(jìn)程
1. 查看Worker進(jìn)程id
zsbmac% ps -ef | grep nginx
502 80322 80311 0 8:27下午 ttys008 0:00.00 grep nginx
502 80316 80318 0 8:27下午 ttys009 0:00.01 nginx: master process /Users/zsb/Documents/Workspaces/nginx-dev/nginx-1.10.3/objs/nginx
502 80320 80316 0 8:27下午 ttys009 0:00.00 nginx: worker process
2. 編輯launch.json
增加工作進(jìn)程的調(diào)試配置:
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// 監(jiān)聽工作進(jìn)程
{
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/objs/nginx",
"processId": "80320", // 填寫 Worker 進(jìn)程 PID
"MIMode": "lldb"
},
// 啟動(dòng)nginx
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/objs/nginx",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
3. 切換debug模式為attach

image.png
4. 斷點(diǎn)調(diào)試,在解析 Http 請(qǐng)求處打斷點(diǎn),刷新瀏覽器

image.png
參考文獻(xiàn)
本文作者: seawish
版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 CC BY-NC-SA 3.0 許可協(xié)議。轉(zhuǎn)載請(qǐng)注明出處!