0Pricing
Lua Academy · Lesson

OpenResty Architecture

Understand how ngx_lua embeds LuaJIT in Nginx request phases.

OpenResty Architecture is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “OpenResty Architecture” lesson free?

Yes — the full text of “OpenResty Architecture” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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