Nginx with Lua support

Nginx is the most powerful webserver which is designed for performance and scale. Its initial public release was in 2004, and it was able to handle a load of 10k concurrent connections. Nginx has grown in popularity since its release due to its light resource utilization and its ability to scale. It has capability to serve static requests quickly and support  fastcgi for dynamic requests. Nginx is often selected for high traffic environment where speed and resource utilization matters most.

Lua support

Lua is a full featured multi-paradigm scripting language with very simple syntax. Now we can embed it inside the nginx webserver, which make nginx more flexible. We can add small features to the webserver without going for new modules or fastcgi. It is easy to add these features with small lua code inside nginx configuration file. I have posted few examples at the end of this post which you can test

Main benefit of this architecture is its fully asynchronous. It inherits the same event-loop model from nginx, so nginx can interrupt the lua code execution when its waiting on a blocking operation and handle another request.

Installation on Centos 6 and Centos 7

We will first enable epel repos on your machine

Enable epel yum repos for your OS

CentOS and Red Hat Enterprise Linux 7.x

Now we can download the required softwares.

1. Nginx : Download latest source file from nginx.org website

2. Nginx-devel-kit

Download it from https://github.com/simpl/ngx_devel_kit

3. Download nginx lua module

You can download it from github repo https://github.com/openresty/lua-nginx-module

4. Install lua and lua-devel

Now you can configure nginx

There are few dependencies, that you can install using yum. Few of them are zlib, pcre , openssl ..

Now run

You should be able to get it installed , if there are no errors.

Few sample configurations with lua

1. Analyzing POST data with lua.

See following example configuration,  we use lua to analyze the POST data, if the POST data has a key named “destination” and its value is “yahoo”, it forwards the request to backend “http_backend”

 

 

Above example. we use lua to analyze the POST data and forward the request to proxy .

I will post more examples in future , hope this article helped you to understand the basics and architecture of nginx+lua platform.

 

Leave a Reply

Your email address will not be published. Required fields are marked *