Supercharging NGINX with LUA (Part 1)

NGINX is a really really fantastic piece of software. It is supremely flexible and handles all kinds of traffic orchestration workloads. One place that NGINX has received commentary however is it's handling of branching logic.

Sometimes it is necessary to have more complex workflows invoked on a per-request basis however. Luckily an NGINX LUA module exists that allow us to simply inject our own LUA handlers into the traffic flow. LUA is a great fit as an embedded language because of its size, simplicity and JIT-compiled speed.

Getting LUA for NGINX

Unfortunately, adding the LUA module to NGINX on your own requires compilation of the module, which itself requires some tricky dependencies:

The good news is that you don't need to compile it yourself! Their is a well-maintained project provided by OpenResty which bundles it all up for you. Follow the installation instructions here.

Ubuntu Install Cheatsheet

Because you can not currently download a packaged version of OpenResty NGINX for Ubuntu, you need to compile it from source; here is a little helper to get you started!

sudo su  
version=1.11.2.2

# install dependencies
apt-get install libreadline-dev libncurses5-dev libpcre3-dev   libssl-dev perl make build-essential

# download & untar
curl -s -o openresty-$version.tar.gz https://openresty.org/download/openresty-$version.tar.gz  
tar -xvf openresty-$version.tar.gz  
cd openresty-$version

# configure & make
./configure --with-pcre-jit --with-ipv6 --with-stream --with-http_realip_module --with-http_stub_status_module --prefix=/opt/openresty 
make -j8  
make install

# NOTE: installs openresty to: /opt/openresty)
# NOTE: installs nginx to: /opt/nginx/openresty/nginx)
Next Steps

Part 2 of this series will illustrate some examples of how we can now make use of our brand new supercharged NGINX-with-LUA to do some custom request handling!


Part II: For examples of NGINX Lua integration, check out "Supercharging NGINX with Lua (Part 2)".

Part III: For more advanced examples of NGINX Lua, check out "Supercharging NGINX with Lua (Part 3)".


Vic van Gool

Vic is the CTO of Cloud 66. He oversees development, infrastructure and architecture at Cloud 66.

Subscribe and get updates

Have feedback? Please get in touch @cloud66 on Twitter.

Everything you need to build, manage and maintain containers in production on your own servers and any cloud

Try Cloud 66 — 14 Days Free Trial, No credit card required