# Help with chaining of sub-requests

**URL:** <https://www.ruby-forum.com/t/help-with-chaining-of-sub-requests/218939>\
**Category:** NGINX\
**Created:** [April 16, 2012, 11:52pm UTC](https://www.ruby-forum.com/t/help-with-chaining-of-sub-requests/218939 "2012-04-16T23:52:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 16, 2012, 11:52pm UTC](https://www.ruby-forum.com/t/help-with-chaining-of-sub-requests/218939/1 "2012-04-16T23:52:12Z")

</div>

Hi,

I have 3 existing http backend web-services S1, S2 and S3. I am  
trying to use nginx for a use-case where i need to first get a “key”  
from service S1, use this key along with some input data, to query S2,  
and based on S2’s response, query S3. My question is, would  
sub-request chaining be the best method to go about this? I can use  
the upstream method to connect to S1. Am i doing something wrong, if  
I then fire a sub-request (request to S2) from within the content  
handler (process header) method registered for this upstream?. How do  
i then initiate the sub-request to S3 ?

Any pointers or example that i might be able to follow, for something  
like the above?

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 17, 2012, 9:47am UTC](https://www.ruby-forum.com/t/help-with-chaining-of-sub-requests/218939/2 "2012-04-17T09:47:22Z")

</div>

Thank you for the pointers, agentzh!

–Ashish

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 17, 2012, 9:20am UTC](https://www.ruby-forum.com/t/help-with-chaining-of-sub-requests/218939/3 "2012-04-17T09:20:40Z")

</div>

On Tue, Apr 17, 2012 at 5:51 AM, Ashish S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any pointers or example that i might be able to follow, for something  
> like the above?

Regarding chained subrequest implementation, see ngx\_echo module’s  
echo\_subrequest directive for an example:

[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpEchoModule#echo_subrequest)

Also ngx\_lua’s ngx.location.capture() and ngx.location.capture\_multi()  
worth a look too:

[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule#ngx.location.capture)  
[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule#ngx.location.capture_multi)

Regarding non-subrequest upstream communication, ngx\_lua supports the  
cosocket API which implements something similar to (but more general  
than) ngx\_http\_upstream:

[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule#ngx.socket.tcp)

Hope these helps,  
-agentzh
