配置:
server {
root /var/www/html;
index main.html main.htm;
listen 127.0.0.1:991;
location / {
add_before_body /before.html;
add_after_body /after.html;
}
}
返回:
curl 127.0.0.1:991
before
main
after
分步:
1,主请求:在ngx_http_addition_body_filter中,调用ngx_http_subrequest,创建第一子请求。
2,主请求:ngx_http_postpone_filter中,将主请求的返回in,加入r→postponed
3,主请求:在ngx_http_addition_body_filter中,调用ngx_http_subrequest,创建第二子请求。
4,主请求:ngx_http_finalize_request,因为主请求下有postponed(第一子请求,in:456,第二子请求),set_writer_handler设置write_event_handler。
5,主请求:ngx_http_run_posted_requests,开始处理子请求,此时里面有第一子请求,第二子请求。
6,第一子请求:ngx_http_postpone_filter中,因为它没有子请求,in直接加到r→main的chain中,等待发送。
7,第一子请求:ngx_http_finalize_request,c→data指针移交给主请求,同时将主请求加入到posted_requests,等到被调用,此时posted_requests中为第二子请求和主请求。
8,第二子请求:ngx_http_postpone_filter中,因为c→data不指向第二子请求,不具备发送能力,将生成的返回加入第二子请求的postponed中。
9,第二子请求:ngx_http_finalize_request,第二子请求下有postponed,调用set_writer_handler设置write_event_handler回调,ngx_http_run_posted_requests开始遍历到主请求,此时为posted_requests仅为主请求。
10,主请求:ngx_http_postpone_filter中,遍历自己的postponed,将自己生成的返回加入chain,将c→data设置为第二子请求,将第二子请求加入posted_requests。
11,主请求:ngx_http_finalize_request,set_writer_handler设置write_event_handler,继续遍历,去第二子请求。
12,第二子请求:ngx_http_postpone_filter中,发送它自己postponed下的返回。
13,第二子请求:ngx_http_finalize_request,c→data此时等于它本身,顺利结束,最后不忘将主请求加回posted_requests。
14,主请求:ngx_http_postpone_filter,ngx_http_finalize_request,发送数据ngx_http_post_action。