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!


Originally published at blog.cloud66.com on December 15, 2016.

Show your support

Clapping shows how much you appreciated Cloud 66’s story.