What is Nginx?

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

NGINX is one of a handful of servers written to address the C10K problem (Handle ten thousand clients simultaneously). Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint.

What is Lua?

Lua is a scripting language. Specifically, it is a full-featured multi-paradigm language with a simple syntax and semantics that resemble JavaScript.

Lua has always meant to be embedded with larger systems written in other languages (like C and C++), and has thrived at staying very minimal and easy to integrate. As a result, Lua is popular within video games, security oriented software.

Benefits of Nginx+Lua

Nginx+Lua is a self-contained web server embedding the scripting language Lua. Powerful applications can be written directly inside Nginx without using cgi, fastcgi, or uwsgi. By adding a little Lua code to an existing Nginx configuration file, it is easy to add small features.

One of the core benefits of Nginx+Lua is that it is fully asynchronous. Nginx+Lua inherits the same event loop model (“Asynchronous”) that has made Nginx a popular choice of webserver.

“Asynchronous” simply means that Nginx can interrupt your code when it is waiting on a blocking operation, such as an outgoing connection or reading a file, and run the code of another incoming HTTP Request.

Nginx+Lua installation

It is highly recommended to use the OpenResty bundle that bundles Nginx, ngx_lua, LuaJIT 2.0/2.1 (or the optional standard Lua 5.1 interpreter), as well as a package of powerful companion Nginx modules.

Steps to download and install openresty

Create your work directory to configure the Nginx

Set PATH to start the Nginx 

To start and reload the Nginx

Commonly Used Nginx API for Lua

Introduction

We have the various

and

configuration directives serve as gateways to the Lua API within the 

file. Some of the directives are

  • rewrite_by_lua
  • rewrite_by_lua_file
  • content_by_lua

The Nginx Lua API described below can only be called within the Lua code run in the context of these configuration directives.

The API is exposed to Lua in the form of two standard packages

and

.

Get URI

ngx.var.uri – To get the URI information.

Get Host

ngx.var.host – To get the host information.

Get URI Args

ngx.req.get_uri_args() – To get the URI arguments.

Get Request Headers

ngx.req.get_headers()[“Header Name”] – To get the value of request header.

Set Header

ngx.header.<header name> = ‘<value>’ – To set the header.

ngx.header[“header name”] = ‘<value>’

Cookie Handling

We can use lua-resty-cookie library to handle HTTP cookie manipulation.

We have to set lua_package_path to work with external lua libraries.

Get Cookie

Create Cookie

Set Cookie

Json string to Lua value or table

cjson.decode(“json string”) – deserialize any UTF-8 JSON string into a Lua value or table.

Lua value to Json string

cjson.encode(value) – will serialize a Lua value into a string containing the JSON representation.

Handle date

Redirection from Nginx

ngx.redirect(“value”) – We can redirect the request to internal URI and External URL also.

Logging

ngx.log(log_level, …) – To provide the logger messages

Send response to rest calls from Nginx

We can send the response to the rest calls from the Nginx itself with the help of Lua.