Lua is a very lightweight programming language. It is primarily used as a scripting language to empower many apps, including webservers, like NGINX.
LuaJIT is a very fast Lua interpreter. This is the very thing that allows running Lua code with blazing speed.
The open-source world is sometimes like a zoo, with all kinds of wild animals.
There are numerous LuaJIT implementations/forks, some abandoned and some are well maintained.
For scripting in NGINX, LuaJIT has to be very fast to match up with the high performance of NGINX itself.
OpenResty’s LuaJIT2 is the maintained fork of LuaJIT, which provides such performance.
Thanks to GetPageSpeed RPM repo, you can empower your NGINX with fast LuaJIT2 scripting.
Let’s go through the quick few steps to add Lua scripting to NGINX, in less than a minute of time.
Install the RPM repository configuration
sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
Install NGINX Lua module
sudo yum install nginx-module-lua
Enable Lua module
Edit /etc/nginx/nginx.conf
and add the following at the very top:
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
After this, reload NGINX if it is running:
sudo systemctl reload nginx
Or enable its service at boot time and start it, if this is a new installation:
sudo systemctl enable --now nginx
Verify it works
Adjust your existing server
configuration in NGINX, to add a test Lua empowered location, like this:
server {
location /lua_content {
# MIME type determined by default_type:
default_type 'text/plain';
content_by_lua_block {
ngx.say('Hello,world!')
}
}
# ...
}
Reload with sudo systemctl reload nginx
and navigate to /lua_content
.
The result should be “Hello,world!”.
Time to script!
There are so many things you can do in NGINX with Lua. For instance, you can protect your signup forms.
To see what extra Lua modules are available for installation, you can run sudo yum search lua-resty
.