Over a million developers have joined DZone.

Supercharging NGINX With Lua: Part I

DZone's Guide to

Supercharging NGINX With Lua: Part I

Sometimes, we need more complex workflows invoked on a per-request basis. A NGINX LUA module exists that allows us to inject our own Lua handlers into the traffic flow.

· Performance Zone ·
Free Resource

Built by operators for operators, the Sensu monitoring event pipeline empowers businesses to automate their monitoring workflows and gain deep visibility into their multi-cloud environments. Get started for free today.

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 in its handling of branching logic.

Sometimes, it is necessary to have more complex workflows invoked on a per-request basis, however. Luckily, a NGINX Lua module exists that allows 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! There is a well-maintained project provided by OpenResty that bundles it all up for you. Follow the installation instructions here.

Ubuntu Install Cheatsheet

Because you cannot currently download a packaged version of OpenResty NGINX for Ubuntu, you need to compile it from the source. Here is a little helper to get you started!

sudo su  

# 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 II 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.

Download our guide to mitigating alert fatigue, with real-world tips on automating remediation and triage from an IT veteran.

lua ,nginx ,performance ,request handling ,traffic orchestration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}