# Ngx\_lua now has (basic) subrequest support

**URL:** <https://www.ruby-forum.com/t/ngx-lua-now-has-basic-subrequest-support/190628>\
**Category:** NGINX\
**Created:** [June 24, 2010, 6:55am UTC](https://www.ruby-forum.com/t/ngx-lua-now-has-basic-subrequest-support/190628 "2010-06-24T06:55:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [June 24, 2010, 6:55am UTC](https://www.ruby-forum.com/t/ngx-lua-now-has-basic-subrequest-support/190628/1 "2010-06-24T06:55:34Z")

</div>

Hi, guys!

Last night’s ngx\_lua hackathon has been proven extremely fruitful.  
chaoslawful and I didn’t stop coding until midnight, and successfully  
finished the first draft of the most tricky bit in ngx\_lua, that is,  
transparent non-blocking IO interface (or nginx subrequest interface)  
on the Lua land.

The following test case is now passing:

location /other {  
echo “hello, world”;  
}

# transparent non-blocking I/O in Lua

location /lua {  
content\_by\_lua ’  
local res = ngx.location.capture(“/other”)  
if res.status == 200 then  
ngx.echo(res.body)  
end’;  
}

And on the client side:

$ curl ‘[http://localhost/lua](http://localhost/lua)’  
hello, world

In the /other location, we can actually have drizzle\_pass,  
postgres\_pass, memcached\_pass, proxy\_pass, or any other content  
handler configuration.

Here’s a more amusing example to do “recursive subrequest”:

location /recur {  
content\_by\_lua ’  
local num = tonumber(ngx.var.arg\_num) or 0  
ngx.echo("num is: ", num, “\n”)

```
       if num > 0 then
           res = ngx.location.capture("/recur?num=" .. tostring(num 

```

- 1))  
ngx.echo(“status=”, res.status, " ")  
ngx.echo(“body=”, res.body)  
else  
ngx.echo(“end\n”)  
end  
';  
}

Here’s the output on the client side:

```
$ curl 'http://localhost/recur?num=3'
num is: 3
status=200 body=num is: 2
status=200 body=num is: 1
status=200 body=num is: 0
end

```

You can checkout the git HEAD of ngx\_lua to try out the examples above  
yourself:

```
http://github.com/chaoslawful/lua-nginx-module

```

So…time to replace our PHP code in the business with nginx.conf + Lua  
scripts!

We’d make the first public release of ngx\_lua when its implementation  
and API become solid enough 😉

Enjoy!  
-agentzh
