nginx
将HTTP
请求处理流程分为11
个阶段,绝大部分HTTP模块都会将自己的handler
添加到某个阶段(将handler
加入全局数组phases
),nginx
处理HTTP请求时会逐个调用每个阶段的handler
,其中有4
个阶段不能自定义handler
,11
个阶段代码在/src/http/ngx_http_core_module.h
中:
1 | typedef enum { |
通过gdb
可以看到具体阶段对应的handler
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58gdb /usr/local/nginx16/sbin/nginx
b ngx_http_block
r
n
b 326
c
(gdb) p cmcf->phases
$1 = {{handlers = {elts = 0x7417f0, nelts = 0, size = 8, nalloc = 1, pool = 0x71d2e0}}, {handlers = {elts = 0x7417f8, nelts = 1, size = 8, nalloc = 1, pool = 0x71d2e0}}, {handlers = {elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0}}, {handlers = {elts = 0x741800, nelts = 1, size = 8, nalloc = 1, pool = 0x71d2e0}}, {handlers = {elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0}}, {handlers = { elts = 0x741e98, nelts = 2, size = 8, nalloc = 2, pool = 0x71d2e0}}, {handlers = {elts = 0x741810, nelts = 2, size = 8, nalloc = 2, pool = 0x71d2e0}}, {handlers = {elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0}}, {handlers = {elts = 0x741820, nelts = 2, size = 8, nalloc = 2, pool = 0x71d2e0}}, {handlers = {elts = 0x741830, nelts = 3, size = 8, nalloc = 4, pool = 0x71d2e0}}, {handlers = { elts = 0x741850, nelts = 1, size = 8, nalloc = 1, pool = 0x71d2e0}}}
(gdb) p cmcf->phases[0]
$2 = {handlers = {elts = 0x7417f0, nelts = 0, size = 8, nalloc = 1, pool = 0x71d2e0}}
(gdb) p *(ngx_http_handler_pt*)cmcf->phases[0].handlers.elts
$3 = (ngx_http_handler_pt) 0x0
(gdb) p cmcf->phases[1]
$4 = {handlers = {elts = 0x7417f8, nelts = 1, size = 8, nalloc = 1, pool = 0x71d2e0}}
(gdb) p *(ngx_http_handler_pt*)cmcf->phases[1].handlers.elts
$5 = (ngx_http_handler_pt) 0x4aaeca <ngx_http_rewrite_handler>
(gdb) p cmcf->phases[2]
$6 = {handlers = {elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0}}
(gdb) p cmcf->phases[3]
$7 = {handlers = {elts = 0x741800, nelts = 1, size = 8, nalloc = 1, pool = 0x71d2e0}}
(gdb) p *(ngx_http_handler_pt*)cmcf->phases[3].handlers.elts
$8 = (ngx_http_handler_pt) 0x4aaeca <ngx_http_rewrite_handler>
(gdb) p cmcf->phases[4]
$9 = {handlers = {elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0}}
(gdb) p cmcf->phases[5]
$10 = {handlers = {elts = 0x741e98, nelts = 2, size = 8, nalloc = 2, pool = 0x71d2e0}}
(gdb) p *(ngx_http_handler_pt*)cmcf->phases[5].handlers.elts+0
$11 = (ngx_int_t (*)(ngx_http_request_t *)) 0x4a226e <ngx_http_limit_conn_handler>
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[5].handlers.elts+1)
$13 = (ngx_http_handler_pt) 0x4a343c <ngx_http_limit_req_handler>
(gdb) p cmcf->phases[6]
$14 = {handlers = {elts = 0x741810, nelts = 2, size = 8, nalloc = 2, pool = 0x71d2e0}}
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[6].handlers.elts+1)
$15 = (ngx_http_handler_pt) 0x4a1872 <ngx_http_access_handler>
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[6].handlers.elts)
$16 = (ngx_http_handler_pt) 0x4a0dcc <ngx_http_auth_basic_handler>
(gdb) p cmcf->phases[7]
$17 = {handlers = {elts = 0x0, nelts = 0, size = 0, nalloc = 0, pool = 0x0}}
(gdb) p cmcf->phases[8]
$18 = {handlers = {elts = 0x741820, nelts = 2, size = 8, nalloc = 2, pool = 0x71d2e0}}
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[8].handlers.elts)
$19 = (ngx_http_handler_pt) 0x49fbc0 <ngx_http_mirror_handler>
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[8].handlers.elts+1)
$20 = (ngx_http_handler_pt) 0x4a00b5 <ngx_http_try_files_handler>
(gdb) p cmcf->phases[9]
$21 = {handlers = {elts = 0x741830, nelts = 3, size = 8, nalloc = 4, pool = 0x71d2e0}}
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[9].handlers.elts)
$22 = (ngx_http_handler_pt) 0x49bcff <ngx_http_static_handler>
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[9].handlers.elts+1)
$23 = (ngx_http_handler_pt) 0x49c685 <ngx_http_autoindex_handler>
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[9].handlers.elts+2)
$24 = (ngx_http_handler_pt) 0x49ec96 <ngx_http_index_handler>
(gdb) p cmcf->phases[10]
$25 = {handlers = {elts = 0x741850, nelts = 1, size = 8, nalloc = 1, pool = 0x71d2e0}}
(gdb) p *((ngx_http_handler_pt*)cmcf->phases[10].handlers.elts)
$26 = (ngx_http_handler_pt) 0x466ea5 <ngx_http_log_handler>
(gdb) p cmcf->phases[11]
$27 = {handlers = {elts = 0x733288, nelts = 0, size = 0, nalloc = 0, pool = 0x721340}}
11
个阶段注册handler
如下: