How nginx's "location if" works (Was Re: Setting cache parameter via if directives)

agentzh agentzh at
Fri Feb 18 14:54:51 MSK 2011

On Fri, Feb 18, 2011 at 7:19 PM, Alexander Kunz <akunz at> wrote:
> Hello,
> that sound really interested. Please can you give me a hint how you use Lua
> as applicaton language? How do you merge for example a Headline from
> mysql/redis into your html code?

Our own applications are typical RIAs, that is, from the point of view
of MVC, the View and Controller are completely running on the client
side, i.e., in our user's web browser, kinda like Gmail :) We've been
using a pure-client-side templating system and compiler called
Jemplate [1].

The basic model looks like this:

1. nginx just serves static files like html, js, css, jpg for the web
app backbone that loads into the user's web browser and runs.

2. The client side JavaScript code issues (cross-site) AJAX requests
to our web service servers which also runs another set of nginx
instances which rely on ngx_drizzle, ngx_lua, and etc to efficiently
emit JSON formatted data.

3. The client side JS code put the data with the (compiled) templates
and get the final HTML fragments that are ready to be put into a node
in the HTML DOM (or XML for the flash components). Then the users see
page regions updated.

Maybe we can call it Service-Oriented Applications? Oh well...

And yeah, RIAs hate most search engine crawlers. Some of our module
users are combining ngx_lua with ngx_ctpp2 [2] for efficient server
side templating where ctpp2 [3] is a templating engine written in C++.
But I've never tried ngx_ctpp2 myself (yet) and I may roll out my own
implementation of a server-side template engine for Perl's TT2
templating language in pure C some time in the future ;)

> Is it possible to use something like
> templates? Or replace a token like ##Headline## in static html pages?

Sure, see above. Also, any templating engines for Lua can be used
directly. The basic steps are:

    local data = get_data_from_remote()
    local html = generate_html_from_template("/path/to/my/template")
    ngx.print(html)  -- emit it!

> With content_by_lua i can wonderful output the data, but what about the html
> code?

Why can't you just regard HTML as a special form of "data"? ;)


[2] svn://

More information about the nginx mailing list