2014-01-25

permalink

This sample demonstrates usage of LuaRocks with OpenResty. It's been tested on Linux and Mac OS X, with the standard Lua interpreter or with LuaJIT.

LuaRocks is a deployment and management system for Lua modules. LuaRocks allows one to install Lua modules as self-contained packages called "rocks", which also contain version dependency information.

We assume that you have installed OpenResty into the default location, i.e., /usr/local/openresty. You can adjust the paths in this sample according to the actual installation prefix of your OpenResty installation. If you haven't installed OpenResty yet, check out the Download and Installation pages.

Install LuaRocks
First of all, let's install LuaRocks:

Download the LuaRocks tarball from http://www.luarocks.org/en/Download. As of this writing, the latest version is 2.1.2, but we'll use 2.0.13 for compatibility throughout this sample.

Install the Lua MD5 library with LuaRocks
In this sample, we'll use the Lua MD5 library to serve as an example, so let's install it with LuaRocks:

Configuring our OpenResty application
Let's change the current directory to /usr/local/openresty/nginx/:

Next, edit the conf/nginx.conf file to the following contents with your favorite text editor (like vim or emacs):

Finally, create the following two Lua module files conf/foo.lua

and conf/bar.lua

Start the Nginx server
Now we start the Nginx server with our app:

If you have already started the Nginx server, then stop it before starting it:

Test our app
Now we can test our app via the curl utility or any HTTP compliant clients like a web browser:

we could get the following outputs at the first run:

then at the second run:

The output changed because LuaNginxModule by default caches already loaded Lua modules and those outputing code run at Lua module loading time will no longer be run.

Now let's do some benchmark:

On my Thinkpad T400 laptop (Core2Duo T9600 CPU), it yields

Note that the throughput is achieved by a single nginx worker process. While doing such benchmark on your own, just be careful about error log level settings in your nginx.conf and not to run out of dynamic port range on your local machine, or it'll be significantly slow after a short of period of time.

Known issues
Pior to OpenResty 1.0.4.2rc10, it's known that turning lua_code_cache on will cause LuaRocks atop LuaNginxModule to throw out the following exception in error.log:

If you're using any version of OpenResty before 1.0.4.2rc10, please consider upgrading.

Show more