Nginx is a great webserver. But it has no scripting capabilities. To add scripting capabilities in Nginx, one needs to build it from source with the necessary add-ons.
In this article I will showcase how we can build Nginx with Lua inside Docker. There are two ways to do it
1. OpenResty
OpenRestry® is a full-fledged web platform that integrates the standard Nginx core, LuaJIT, many carefully written Lua libraries, lots of high quality 3rd-party Nginx modules
The easiest way to use OpenRestry is to use its docker image
$ docker pull openresty/openresty:alpine-fat
This image comes with most of the stuff loaded. We are not going to go in depth of this, as we are more interested in building Nginx from source ourself
2. Building Nginx from source
Building the package from source has some prerequistes like gcc
, make
and few more things.
Prerequistes
Centos
yum install -y wget unzip gcc make openssl-devel pcre-devel zlib-devel
Ubuntu
apt update
apt install -y wget unzip make libssl-dev libpcre3-dev gcc make zlib1g-dev
Alpine
apk add --no-cache \
ca-certificates \
libressl \
pcre \
zlib \
build-base \
linux-headers \
libressl-dev \
pcre-dev \
wget \
zlib-dev
Downloading the source
We need 4 packages to build Nginx with Lua
- LuaJIT
- Nginx Source Code
- Nginx Devel Kit
- Nginx Lua Module
$ mkdir /tmp/nginx
$ cd /tmp/nginx
$ wget http://nginx.org/download/nginx-1.13.0.tar.gz
$ wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
$ wget -O nginx_devel_kit.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
$ wget -O nginx_lua_module.tar.gz https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
Extract all the tar files
$ tar xvf LuaJIT-2.0.4.tar.gz
$ tar xvf nginx-1.11.10.tar.gz
$ tar xvf nginx_devel_kit.tar.gz
$ tar xvf nginx_lua_module.tar.gz
Building LuaJIT
To build Nginx with LuaJIT, we need to build LuaJIT first. This is as simple as a make command
$ cd /tmp/nginx/LuaJIT-2.0.4
$ make install
==== Building LuaJIT 2.0.4 ====
make -C src
make[1]: Entering directory `/tmp/nginx/LuaJIT-2.0.4/src'
...
...
ln -sf luajit-2.0.4 /usr/local/bin/luajit
==== Successfully installed LuaJIT 2.0.4 to /usr/local ====
Building Nginx
Next we build configure the Nginx build. We also need to set the environment variables to specifcy the location of the LuaJIT library
$ cd /tmp/nginx/nginx-1.11.10
$ LUAJIT_LIB=/usr/local/lib LUAJIT_INC=/usr/local/include/luajit-2.0 \
./configure \
--user=nobody \
--group=nobody \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module ${NGINX_DEBUG:+--debug} \
--with-cc-opt=-O2 --with-ld-opt='-Wl,-rpath,/usr/local/lib' \
--add-module=/tmp/nginx/ngx_devel_kit-0.3.0 \
--add-module=/tmp/nginx/lua-nginx-module-0.10.8
$ make install
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Note: We have used Nginx 1.11.10 instead of the latest 1.13.0 because a open issue 1051 with nginx lua module
Now let put the Nginx Lua to a test.
Nginx Lua Testing
We will build a simple lua endpoint http://localhost/sum?a=<num>&b=<num>
, which sums two numbers passed through the url get parameters
location /sum {
content_by_lua_block {
local args = ngx.req.get_uri_args();
ngx.say(args.a + args.b)
}
}
Insert the above lines of code in the \etc\nginx\nginx.conf
inside the server
block. Then start nginx in background
$ nginx &
$ curl "http://localhost/sum/?a=10&b=20"
30
For building the same in centos
, ubuntu
or alpine
please follow my code in the tarunlalwani/nginx-lua-docker repository