OpenResty — High-Performance Web Platform on Nginx + Lua
OpenResty bundles Nginx with LuaJIT and dozens of Lua modules to turn the world's most proven reverse proxy into a full-blown application server for gateways, edge APIs, and WAFs.
What it is
OpenResty extends Nginx with LuaJIT, the ngx_lua module, and dozens of curated Lua libraries covering Redis, MySQL, DNS, JSON, JWT, and more. The result is a web platform that retains Nginx's event-driven performance while allowing request handling logic in high-level Lua code. OpenResty is the foundation of Apache APISIX, Kong, and most major open source API gateways.
OpenResty targets backend engineers and platform teams who need programmable request handling at the edge -- custom routing, authentication, rate limiting, or request transformation -- without deploying a separate application server.
How it saves time or tokens
Without OpenResty, implementing custom logic at the Nginx layer requires writing C modules or deploying a separate application behind Nginx. OpenResty lets you write that logic in Lua directly within the Nginx config, executed at near-C speed by LuaJIT. Non-blocking cosocket APIs for Redis, MySQL, and HTTP mean your Lua handlers never block a worker thread.
The single-binary deployment model means you replace stock Nginx with OpenResty and gain full scripting capabilities with zero additional infrastructure.
How to use
- Install OpenResty:
# Ubuntu/Debian
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
echo 'deb http://openresty.org/package/ubuntu '$(lsb_release -sc)' main' \
| sudo tee /etc/apt/sources.list.d/openresty.list
sudo apt update && sudo apt install -y openresty
- Add a Lua handler to your nginx.conf:
server {
listen 8080;
location /hi {
content_by_lua_block {
ngx.say('hello from openresty')
}
}
}
- Reload and test:
sudo openresty -s reload
curl localhost:8080/hi
# hello from openresty
Example
A rate-limiting handler using OpenResty's shared dictionary:
http {
lua_shared_dict rate_limit 10m;
server {
listen 8080;
location /api {
access_by_lua_block {
local limit = ngx.shared.rate_limit
local key = ngx.var.remote_addr
local count = limit:incr(key, 1, 0, 60)
if count > 100 then
ngx.status = 429
ngx.say('rate limit exceeded')
return ngx.exit(429)
end
}
proxy_pass http://backend;
}
}
}
Related on TokRepo
- DevOps Tools -- Infrastructure and deployment tooling
- AI Gateway Providers -- API gateway and proxy solutions
Common pitfalls
- OpenResty uses its own Nginx build. Do not install stock Nginx and OpenResty on the same machine without resolving the port and binary conflicts.
- Lua code in content_by_lua_block runs in the Nginx worker process. Blocking calls (e.g., os.execute, io.read) will stall the entire worker. Always use OpenResty's non-blocking cosocket APIs instead.
- LuaJIT has a 2GB memory limit on 64-bit systems by default. For applications that need large Lua tables or caches, use shared dictionaries (lua_shared_dict) which are allocated outside LuaJIT's heap.
Frequently Asked Questions
Kong is built on top of OpenResty. Kong uses OpenResty as its runtime and adds a plugin architecture, admin API, and database layer on top. Understanding OpenResty helps when debugging Kong at the Nginx/Lua level.
OpenResty includes a full Nginx build with additional modules. It can serve as a drop-in replacement for stock Nginx. All standard Nginx directives and features work unchanged, with Lua scripting as an addition.
OpenResty uses LuaJIT, which implements Lua 5.1 with extensions. LuaJIT provides near-C performance through JIT compilation. Standard Lua 5.2+ features are not available unless using compatibility libraries.
Yes. OpenResty's lua_shared_dict provides a shared memory zone across all worker processes, making it suitable for rate limiting, caching, and counters. The example in this guide demonstrates a basic per-IP rate limiter.
For static content and proxying, performance is identical to stock Nginx. Lua handler performance depends on the code complexity but LuaJIT execution is fast enough that OpenResty-based API gateways handle tens of thousands of requests per second per core.
Citations (3)
- OpenResty Official Site— OpenResty extends Nginx with LuaJIT and curated Lua modules
- OpenResty GitHub— ngx_lua module provides non-blocking Lua scripting inside Nginx
- LuaJIT Official Site— LuaJIT provides near-C performance through JIT compilation
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.