DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • An Overview of Kubernetes Security Projects at KubeCon Europe 2023
  • What Is Istio Service Mesh?
  • VPN Architecture for Internal Networks
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  1. DZone
  2. Coding
  3. Languages
  4. Supercharging NGINX With Lua (Part 2)

Supercharging NGINX With Lua (Part 2)

Once you have installed NGINX with Lua support, you will be able to work with several simple yet powerful Lua integrations.

Vic van Gool user avatar by
Vic van Gool
·
Jan. 23, 17 · Opinion
Like (1)
Save
Tweet
Share
3.09K Views

Join the DZone community and get the full member experience.

Join For Free

In Supercharging NGINX With Lua: Part I, we demonstrated how to install NGINX with Lua support (via the OpenResty package). In this post, I'll walk through some simple yet 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!

1. Basic Lua Handler (Embedded)

Here we utilize Lua to respond with OK.

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

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: 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 the file /etc/lua/example2.lua with this content:

ngx.say("OK!") 

Then, include the following in our NGINX config:

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

3. Lua for Auth

Here, we utilize Lua to authorize a request based on the remote_user, remote_address, and URI. We create the file /etc/lua/example3.lua with this 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 III 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.

Lua (programming language)

Published at DZone with permission of Vic van Gool, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • An Overview of Kubernetes Security Projects at KubeCon Europe 2023
  • What Is Istio Service Mesh?
  • VPN Architecture for Internal Networks
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: