ngx_lua now has (basic) subrequest support

agentzh agentzh at gmail.com
Thu Jun 24 08:54:32 MSD 2010


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'
   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



More information about the nginx mailing list