Supercharging NGINX with Lua (Part 2)

In Supercharging NGINX with Lua (Part 1) we demonstrated how to install NGINX with Lua support (via the OpenResty package). In this post I’ll walk through some simple, but powerful examples of Lua integrations.

For the below examples I am assuming that you have installed NGINX capable of handling Lua scripts, and have a basic working NGINX configuration file (to embed the location blocks below in).

Important: Although the examples below will work, they are somewhat contrived (over-simplified) and are primarily intended for demonstration of Lua capabilities; if you decide to use the examples in your own environments, please make sure that you understand what they are doing and tailor them to your own requirements; Phew… legal stuff done!

Lastly, there is very comprehensive NGINX Lua Module Documentaion and OpenResty Documentation in which the NGINX Lua commands and constructs are explained in detail; if you are unsure about a particular command or method I highly recommend starting there!

# Example 1. Basic Lua handler (embedded)

Here we utilize Lua to respond with: “OK”:

location /example1 { content_by_lua_block { ngx.say("OK") } }

# Example 2. Basic Lua handler (modular)

Here we again utilize Lua to respond with: “OK!”, however we use a separate Lua file. This is recommended to allow you to separate your concerns.

Note: that with later versions of Lua NGINX you can also provide these files in pre-compiled Lua/LuaJIT bytecode (for faster start times).

To start we create file /etc/lua/example2.lua with content:

ngx.say("OK!")

Then include the following in our NGINX config:

location /example2 { content_by_lua_file /etc/lua/example2.lua; }

# Example 3. Lua for auth

Here we utilize Lua to authorize a request based on the remote_user, remote_address and URI. We create file /etc/lua/example3.lua with content:

local remote_user = ngx.var.remote_user local remote_addr = ngx.var.remote_addr local uri = ngx.var.uri if string.match(remote_user, "frankie") and string.match(remote_addr, "123.123.123.123") and string.match(uri, "[%a%d_%-.]+") then -- we are ok here! else ngx.exit(ngx.HTTP_FORBIDDEN) end

Then include the following in our NGINX config:

location /example3 { access_by_lua_file /etc/lua/example3.lua; }

Next Steps

Part 3 of this series will illustrate some more advanced examples of how we can now use Lua modules to do some ultra-cool stuff like request-caching and embedded http requests.

Originally published at blog.cloud66.com on January 19, 2017.

--

--

--

DevOps-as-a-Service to help developers build, deploy and maintain apps on any Cloud. Sign-up for a free trial by visting: www.cloud66.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Connect MySQL Database with Django Project

How to setup Laravel (PHP 8, Postgres, Nginx, Redis and etc) with Docker & WSL2

Build And Release Artifacts Using GitActions

Integrating LVM with hadoop to provide dynamic storage to datanode

Design Patterns 101: The Null Pattern

Heads-up push notification from REST and Firebase when app is in background or foreground

Notes from 99 Bottles of OOP

Car Software Revolutions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloud 66

Cloud 66

DevOps-as-a-Service to help developers build, deploy and maintain apps on any Cloud. Sign-up for a free trial by visting: www.cloud66.com

More from Medium

How to Build Web Services Relying on Tencent Cloud CVM

What’s New At Releem and Special Offer | March 2022

Creating “Hello World” CI/CD Pipeline on Gitlab for testers.

EC2 Instance Storage : AMI Overview