nginx调试方法

来源:互联网 发布:天心天思网络差评 编辑:程序博客网 时间:2024/05/16 12:28

2015-05-02 wcdj


摘要:调试nginx用到的工具有:secureCRT, curl, wget, gdb, cgdb, strace, pstack, addr2line等。

首先是下载源码,可以通过curl或wget来完成。
curl -O http://nginx.org/download/nginx-1.2.0.tar.gz
wget http://nginx.org/download/nginx-1.2.0.tar.gz

编译带调试和禁止优化的nginx,编译后可以通过./nginx -V命令查看
./configure --prefix='/data/home/gerryyang/LAMP/nginx/install/nginx-1.2.0' --with-debug --without-http_rewrite_module --with-cc-opt='-ggdb3 -O0'
为了查看nginx中定义的宏,需要使用-ggdb3选项
使用-g和-ggdb3编译后的大小区别
~/LAMP/nginx/install/nginx-1.2.0/sbin$ls -rtlh
total 18M
-rwxrwxr-x 1 gerryyang gerryyang 2.6M May 2 18:08 nginx.old
-rwxrwxr-x 1 gerryyang gerryyang 15M May 2 19:06 nginx

通过刷新所有源文件的时间戳,间接达到重新编译出一个新的nginx,这样做可以修改configure生成的Makefile文件
find . -name "*.c" | xargs touch

修改nginx配置,设置nginx只启动一个worker进程
worker_processes 1;

调试nginx启动和后续过程,最简单的方法是,将master进程和worker进程逻辑全部合在一个进程里,修改nginx配置
daemon off;
master_process off;
fork场景的调试方法,让gdb跟踪fork之后的子进程
set follow-fork-mode child

先使用curl测试nginx是否可以正常服务
curl -v "http://10.206.131.144:9001/"
* About to connect() to 10.206.131.144 port 9001 (#0)*   Trying 10.206.131.144... connected* Connected to 10.206.131.144 (10.206.131.144) port 9001 (#0)> GET / HTTP/1.1> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.1.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2> Host: 10.206.131.144:9001> Accept: */*> < HTTP/1.1 200 OK< Server: nginx/1.2.0< Date: Sat, 02 May 2015 10:21:54 GMT< Content-Type: text/html< Content-Length: 151< Last-Modified: Sat, 02 May 2015 09:19:03 GMT< Connection: keep-alive< Accept-Ranges: bytes< <html><head><title>Welcome to nginx!</title></head><body bgcolor="white" text="black"><center><h1>Welcome to nginx!</h1></center></body></html>* Connection #0 to host 10.206.131.144 left intact* Closing connection #0


使用gdb调试nginx启动的方法
~/LAMP/nginx/install/nginx-1.2.0/sbin$gdb --args ./nginx -c conf/nginx.conf
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-50.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /data/home/gerryyang/LAMP/nginx/install/nginx-1.2.0/sbin/nginx...done.
(gdb) r
Starting program: /data/home/gerryyang/LAMP/nginx/install/nginx-1.2.0/sbin/nginx -c conf/nginx.conf
[Thread debugging using libthread_db enabled]


使用strace调试的方法
strace -s1024 -p30946
Process 30946 attached - interrupt to quit
epoll_wait(3, 

Refer

[1] http://daniel.haxx.se/docs/curl-vs-wget.html



1 0
原创粉丝点击