nginx子请求与addtion filter模块分析

配置示例:

server {

root /var/www/html;

index  main.html main.htm;

listen 127.0.0.1:999;

location / {

add_before_body /before.html;

add_after_body  /after.html;

}

}

返回:

curl 127.0.0.1:999

before

main

after

 

流程分析:

主请求:在ngx_http_addition_body_filter中,调用ngx_http_subrequest,创建第一子请求。

主请求:ngx_http_postpone_filter中,将主请求的返回in,加入r→postponed。

主请求:在ngx_http_addition_body_filter中,调用ngx_http_subrequest,创建第二子请求。

主请求:ngx_http_finalize_request,因为主请求下有postponed,set_writer_handler设置write_event_handler。

主请求:ngx_http_run_posted_requests,开始处理子请求,此时里面有第一子请求,第二子请求。

第一子请求:ngx_http_postpone_filter中因为它没有子请求,in直接加到r→main的chain中,等待发送。

第一子请求:ngx_http_finalize_request,c→data指针移交给主请求,同时将主请求加入到posted_requests,等到被调用时,posted_requests中为第二子请求和主请求。

第二子请求:ngx_http_postpone_filter中,因为c→data不指向第二子请求,不具备发送能力,将生成的返回加入第二子请求的postponed中。

第二子请求:ngx_http_finalize_request,第二子请求下有postponed,调用set_writer_handler设置write_event_handler回调,ngx_http_run_posted_requests开始遍历到主请求,此时为posted_requests仅为主请求。

主请求:ngx_http_postpone_filter中,遍历自己的postponed,将自己生成的返回加入chain,将c→data设置为第二子请求,将第二子请求加入posted_requests。

主请求:ngx_http_finalize_request,set_writer_handler设置write_event_handler,继续遍历到第二子请求。

第二子请求:ngx_http_postpone_filter中,发送它自己postponed下的返回。

第二子请求:ngx_http_finalize_request,c→data此时等于它本身,顺利结束,最后不忘将主请求加回posted_requests。

主请求:ngx_http_postpone_filter,ngx_http_finalize_request,发送数据ngx_http_post_action。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值