NGINX / Server Setup

How to add Lua scripting power to your NGINX in under one minute

by , , revisited on


We have by far the largest RPM repository with dynamic stable NGINX modules and VMODs for Varnish 4.1 and 6.0 LTS. If you want to install NGINX, Varnish, and lots of useful modules for them, this is your one-stop repository to get all performance-related software.
You have to maintain an active subscription in order to be able to use the repository!

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.

Supported systems:

  • CentOS/RHEL 6, 7, 8
  • Amazon Linux 2

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.