ngx_lua now has (basic) subrequest support

Peter Leonov gojpeg at gmail.com
Thu Jun 24 11:35:02 MSD 2010


Hi, Yichun!

On 24.06.2010, at 8:54, agentzh <agentzh at gmail.com> wrote:

> 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.
Congratulations! Subrequests are proven to be the darkest side of  
nginx (after if of course) ;)

>
> 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'
>    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
Wonderful, guys!
JS module definitly has something to… learn out from Lua module now ;)

>
> 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!
One more thing. AFAIK, location.capture uses a co-routine to emulate  
sync interface to async subrequest. This is awesome :)

And what about parallel sub-requests as SSI module does? It gonna be  
cool to see Lua as an SSI mini-language replacement (PHP did you say?).

>
> We'd make the first public release of ngx_lua when its implementation
> and API become solid enough ;)
>
> Enjoy!
> -agentzh

Very best regards,
Peter.


More information about the nginx-devel mailing list