Make Nginx More Powerful With OpenResty

Posted By : Tarun Singhal | 30-May-2018

If you’ve ever wanted to make some changes to NGINX dynamically or you wanted a bit more flexibility, then OpenResty is the way to do so.
it’s a combination of NGINX, along with Lua scripting, and several additional third-party modules all packaged up and ready to use.
Some of thinking that this additional functionality comes with a performance hit, but this simply isn’t the case. Large platforms, such as Cloudflare, use a combination of NGINX and Lua to achieve what they do at scale, and it’s due to the power of OpenResty.
This power allows complicated scenarios such as Web Application Firewalls (WAFs) to be tightly integrated at the website level, allowing the combination of per-site flexibility with the high-speed aspects NGINX is known for.

Installation of OpenResty

On Ubuntu

  • To install OpenResty on an Ubuntu-based system, first we need to import the GPG key used for signing the packages:
To install OpenResty on an Ubuntu-based system, first we need to import the GPG key used for signing the packages:
apt install -y software-properties-common
  • We can now install the repository for OpenResty and then refresh the package indexes:
add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"apt update
  • With the repository installed, we can now install the OpenResty package:
apt install -y openresty
  • Once all the dependencies and OpenResty packages are installed, you can now set the service to start on boot and then start it so that you can test the service. You can do this via system:
systemctl enable openresty
systemctl start openresty

if you don’t see any errors, you will then be able to to browse to the IP address of your server and see the OpenResty test page.
The locations of the configuration files and system libraries are slightly different to a standard NGINX installation, so it’s important to remember the location. By default, OpenResty installs to /
usr/local/openresty. For example, if you look for the NGINX configuration files, they’ll be stored in /usr/local/openresty/nginx/conf.

  • We can confirm these modules are available by running the following command:
/usr/local/openresty/bin/openresty -V

One of the key powers of OpenResty is the built-in Lua scripting language.

Let’s see how it works

There is content_by_lua_block block directive. This enables us to embed Lua code specifically in accordance with our NGINX setup, giving fast and dynamic changes. The primary formula restores a fundamental string:?

location /test1 { 
    default_type 'text/plain'; 
    content_by_lua_block { 
        ngx.say('Hello World') 
  } 
} 

Now, If you simple curl or hit the browser, you will get Hello World in return.

If you want to return in Json, for that you can use Lua Cjson Library.

location /test2 { 
        default_type 'application/json'; 
        content_by_lua_block { 
            local cjson = require "cjson.safe" 
            ngx.say(cjson.encode({Hello World="Encoded with CJSON",enabled=true})) 
      } 
    } 

Now, if you hit tes2, you will get the below output

{"Hello World":"Encoded with CJSON","enabled":true}

This is Just a basic, what we can do with Lua.

Thanks.

About Author

Author Image
Tarun Singhal

Tarun is a RedHat Certified System Administrator. He is very keen to learn new technologies. He has good command over tools like Ansible, Gitlab-CI etc.

Request for Proposal

Name is required

Comment is required

Sending message..