

Turning Nginx into Full-Stack Web Application Platform (By Taobao.com) - Polat
https://github.com/agentzh/ngx_openresty

======
d_r
FWIW, while less known in the West, Taobao.com is one of the world's most
highest-trafficked websites. It's the eBay equivalent in China and has many
more use cases, such as buying plane tickets or new clothes.

------
riobard
For those unfamiliar with the Nginx/Lua setup, here's the introduction:

"Lua-nginx-module integrated Lua into NginX, and providing high-concurrent &
non-blocking request processing ability without forcing developers explicitly
dividing business logics into state machine. Developers can write their
programs in plain-old sequential ways, and lua-nginx-module will automatically
interrupt the program logic at blockable I/O operations, save the context and
delegate these I/O ops to NginX’s event mechanism. When I/O ops are done, lua-
nginx-module will restore the context and restore the normal running of the
program logic. User program itself will feel everything as usual as never
being interrupted.

Lua-nginx-module use one-coroutine-per-request request handling model, i.e.
for each incoming request, lua-nginx-module spawns a coroutine to run user
code to process the request, and the coroutine is destroyed when request
handling process is done. Every coroutine has its own independent global
environment, which inherits shared read-only common data. So any globals
injected by user code won’t affect other request’s processing, and will be
freed when request handling is done. You can imagine that user code is running
in a ‘sandbox’, which shares the same lifecycle with request itself. By this
way, lua-nginx-module can prevent memory-leak caused by the abusing of globals
in user code, and improved service robustness.

Benefit from Lua’s excellant coroutine support, lua-nginx-module can handle
tens of thousands of concurrent requests with very low memory overhead.
According to our tests, the memory overhead for each request in lua-nginx-
module is only 2 KB or even half smaller if LuaJIT is used. So obviously lua-
nginx-module is a good candidate for implementing extensive concurrent
services."

[https://github.com/chaoslawful/lua-nginx-
module/wiki/Introdu...](https://github.com/chaoslawful/lua-nginx-
module/wiki/Introduction)

The ability to do async-IO without changing code flow is a big plus.

~~~
ww520
Be able to write in sequential logic flow and have the underlying
language+system automatically turning it into async calls is a huge win. This
is like having the async benefit of Node.js without dealing with all the
callbacks.

------
justincormack
After a bit of testing I am starting some more serious Nginx+Lua development.

The big advantage is that it is a pretty mature http stack that is tested in
production at scale, unlike some of the newer frameworks (that you probably
run behind nginx anyway). It still has some limitations like the proxy module
only supports http 1.0 and some of the chunked encoding stuff is partial at
this point, but it is generally sufficient.

Luajit is really fast and nice to use (the ffi interface can call C directly)
and coroutines mean that blocking calls can just drop back to the event loop.
You have to write anything blocking as an nginx module though, hence this
collection of modules as a bundle.

Having a real programming language in the context of a web server is nice, you
can either do small things like write complex auth that you might otherwise
have a proxy for, or you can write full content handlers.

Agentzh is pretty helpful on the nginx list with issues too.

~~~
swah
I'm confused - there are some Perl modules and some LuaJIT modules. Is it Lua
you will be programming if you use this?

Also, if you're interested in that combination, have you seen Zed Shaw's Tir?
(<http://sheddingbikes.com/posts/1289384533.html>) Its Lua+ZeroMQ+Mongrel2.

~~~
justincormack
Perl is just the the built process of the bundle. Lua is the programming
language inside Nginx.

Tir looks nice too. It is more targeted at websocket type applications,
although it is general purpose too. Nginx supports more traditional HTTP
models, I dont know of any native websocket support for it, but you can of
course use it to proxy to mongrel/node or whatever. Nginx has the maturity
advantage, and you can use it as a proxy, load balancer, and so on. With the
Lua support and these additional modules that taobao developed you get a full
environment all in one place.

------
mhd
taobao.com use php on their front page, so I guess this is used for web
services internally and/or used for new developments.

The blog of the main developer[1] is quite interesting, too.

I'm going to have a serious look at it. I've always liked lua (something
between tcl and python, language-wise), and even if it doesn't make the cut as
a front end solution (because of missing stuff or simply because I'm going
with a pure javascript frontend), it would seem to be a good replacement for a
non-blocking backend server like node.js.

[1]: <http://agentzh.blogspot.com/>

------
stcredzero
I think a Ngnix/Lua full stack web app environment is a great idea. (In fact,
I think there's a comment of mine with this very idea from about 8 months
back.)

