nginx的调试

Feb 09, 2010

查看nginx运行状况,看log文档无疑是最方便,想实现nginx的调试,你只需要在./configure的时候添加--with-debug选项,然后在你的配置文档中添加

error_log  logs/error.log debug;
如下是我的配置文件:

#user  nobody; 
worker_processes  1; 

error_log  logs/error.log debug; 

master_process  off; 
daemon off; 

events { 
    worker_connections  1024; 
} 


http { 
    include       mime.types; 
    default_type  application/octet-stream; 

    access_log  logs/access.log ;   

    sendfile        on; 
    #tcp_nopush     on; 

    #keepalive_timeout  0; 
    keepalive_timeout  65; 
    #for error 502 
    large_client_header_buffers 4 32k; 
    proxy_next_upstream error timeout invalid_header http_500 http_503; 
    upstream wurmbai{ 
        server  127.0.0.1:80; 
        server  127.0.0.1:3128; 
        hash $request_uri; 
        hash_again 2; 
    } 

    server { 
        listen       80; 
        server_name  localhost; 

        location / { 
            root   html; 
            index  index.html index.htmi; 
            proxy_pass  http://wurmbai; 
        } 
    } 

}

请原谅我配置文件的丑陋,没有什么特别的选项,普通到不能普通的配置。如上的配置文件,当我在访问本地服务器时,error.log内容如下:

2010/02/09 16:52:01 [debug] 12715#0: *1 http cleanup add: 09B1714C 
2010/02/09 16:52:01 [debug] 12715#0: *1 http script var: "/" 
2010/02/09 16:52:01 [debug] 12715#0: *1 upstream_hash: hashing "/" 
2010/02/09 16:52:01 [debug] 12715#0: *1 upstream_hash: get upstream request hash peer try 3 
2010/02/09 16:52:01 [debug] 12715#0: *1 upstream_hash: chose peer 1 w/ hash 31187 
2010/02/09 16:52:01 [debug] 12715#0: *1 socket 9 
2010/02/09 16:52:01 [debug] 12715#0: *1 epoll add connection: fd:9 ev:80000005 
2010/02/09 16:52:01 [debug] 12715#0: *1 connect to 127.0.0.1:3128, fd:9 #2 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream connect: -2 
2010/02/09 16:52:01 [debug] 12715#0: *1 event timer add: 9: 60000:2985196537 
2010/02/09 16:52:01 [debug] 12715#0: timer delta: 0 
2010/02/09 16:52:01 [debug] 12715#0: posted events 00000000 
2010/02/09 16:52:01 [debug] 12715#0: worker cycle 
2010/02/09 16:52:01 [debug] 12715#0: epoll timer: 60000 
2010/02/09 16:52:01 [debug] 12715#0: epoll: fd:8 ev:0004 d:09B22954 
2010/02/09 16:52:01 [debug] 12715#0: *1 http run request: "/?" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream check client, write event:1, "/" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream recv(): -1 (11: Resource temporarily unavailable) 
2010/02/09 16:52:01 [debug] 12715#0: epoll: fd:9 ev:0004 d:09B229B0 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream request: "/?" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream send request handler 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream send request 
2010/02/09 16:52:01 [debug] 12715#0: *1 chain writer buf fl:1 s:352 
2010/02/09 16:52:01 [debug] 12715#0: *1 chain writer in: 09B17188 
2010/02/09 16:52:01 [debug] 12715#0: *1 writev: 352 
2010/02/09 16:52:01 [debug] 12715#0: *1 chain writer out: 00000000 
2010/02/09 16:52:01 [debug] 12715#0: *1 event timer del: 9: 2985196537 
2010/02/09 16:52:01 [debug] 12715#0: *1 event timer add: 9: 60000:2985196538 
2010/02/09 16:52:01 [debug] 12715#0: timer delta: 1 
2010/02/09 16:52:01 [debug] 12715#0: posted events 00000000 
2010/02/09 16:52:01 [debug] 12715#0: worker cycle 
2010/02/09 16:52:01 [debug] 12715#0: epoll timer: 60000 
2010/02/09 16:52:01 [debug] 12715#0: epoll: fd:9 ev:0005 d:09B229B0 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream request: "/?" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http upstream process header 
2010/02/09 16:52:01 [debug] 12715#0: *1 malloc: 09B177F8:4096 
2010/02/09 16:52:01 [debug] 12715#0: *1 recv: fd:9 387 of 4096 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy status 200 "200 OK" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Server: Apache/2.2.11 (Ubuntu)" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Last-Modified: Thu, 28 Jan 2010 11:47:57 GMT" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "ETag: "8ee7b-2d-47e38170f7540"-gzip" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Vary: Accept-Encoding" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Content-Encoding: gzip" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Content-Type: text/html" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Content-Length: 56" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Date: Tue, 09 Feb 2010 08:52:01 GMT" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "X-Varnish: 1680796254" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Age: 0" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Via: 1.1 varnish" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header: "Connection: close" 
2010/02/09 16:52:01 [debug] 12715#0: *1 http proxy header done 
2010/02/09 16:52:01 [debug] 12715#0: *1 HTTP/1.1 200 OK 
Server: nginx/0.7.63 
Date: Tue, 09 Feb 2010 08:52:01 GMT 
Content-Type: text/html 
Connection: keep-alive 
Last-Modified: Thu, 28 Jan 2010 11:47:57 GMT 
ETag: "8ee7b-2d-47e38170f7540"-gzip 
Vary: Accept-Encoding 
Content-Encoding: gzip 
Content-Length: 56 
X-Varnish: 1680796254 
Age: 0 
Via: 1.1 varnish

上面是error.log关于此次访问信息的一部分,通过上面的内容我们可以很清楚的看到nginx通过url hash选择了127.0.0.1:3128这个upstream(也就是varnish),而且我们还能清楚的看到整个nginx执行的各个细节和http请求的头部,结合源代码看error.log对我的帮助真的很大。不错!

Comments

  1. If suffer we must let's suffer on the heights.

    Avatar

Get In Touch With Us ...