Lua Academy · Lesson

OpenResty Architecture

Understand how ngx_lua embeds LuaJIT in Nginx request phases.

Lesson 1 of 413 steps

OpenResty Architecture is a free Lua Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

What Is OpenResty?

OpenResty is a web platform that embeds LuaJIT into Nginx. It turns Nginx into a full-featured web application server where Lua code handles requests.

ngx_lua Module

The ngx_lua module integrates LuaJIT into Nginx. It provides the ngx.* API for accessing request/response data, timers, shared memory, and cosockets.

Nginx Request Phases

OpenResty exposes Nginx's request processing phases as Lua hooks: init_by_lua, access_by_lua, content_by_lua, header_filter_by_lua, log_by_lua.

nginx.conf Basics

Configure Lua handlers in nginx.conf using the appropriate phase directive.

# nginx.conf snippet
server {
  listen 80;
  location /hello {
    content_by_lua_block {
      ngx.header["Content-Type"] = "text/plain"
      ngx.say("Hello from Lua!")
    }
  }
}

Coroutine-per-Request Model

Each incoming request gets its own Lua coroutine. When a coroutine yields (e.g., waiting for a database), Nginx serves other requests. This enables high concurrency without blocking.

The ngx API

Key ngx functions: ngx.say(), ngx.print(), ngx.header, ngx.var, ngx.req.*, ngx.status, ngx.exit().

Shared Memory State

Worker processes share data via ngx.shared.DICT — a shared dictionary declared in nginx.conf with a fixed memory size.

# nginx.conf
lua_shared_dict my_cache 10m;
-- In Lua:
ngx.shared.my_cache:set("key", "value", 60)

Cosockets

OpenResty's cosockets provide non-blocking TCP/UDP and HTTP. They integrate with Nginx's event loop so Lua can do async I/O without blocking the worker.

Timer API

ngx.timer.at(delay, fn) schedules a Lua function to run after a delay. ngx.timer.every(interval, fn) runs periodically.

Performance Profile

OpenResty handles tens of thousands of concurrent connections per worker. Combined with LuaJIT's speed and Nginx's proven I/O multiplexing, it competes with Go and Node.js for I/O-bound workloads.

Common Use Cases

  • API gateways and rate limiting.
  • Authentication/authorization middleware.
  • Dynamic request routing.
  • Redis and Memcached caching layers.

OpenResty Question

How does OpenResty achieve high concurrency?

Recap: OpenResty Architecture

OpenResty embeds LuaJIT in Nginx. Each request runs as a coroutine. The ngx.* API provides access to request/response data, shared memory, timers, and non-blocking cosockets.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “OpenResty Architecture” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “OpenResty Architecture”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

What will I learn in “OpenResty Architecture”?

Understand how ngx_lua embeds LuaJIT in Nginx request phases. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “OpenResty Architecture” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. OpenResty Architecture
  2. Handling HTTP Requests in Lua
  3. Cosockets and Non-Blocking TCP
  4. Shared Memory Dictionaries
← Back to Lua Academy