ConfigsApr 15, 2026·3 min read

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.

TL;DR
OpenResty extends Nginx with LuaJIT to build high-performance API gateways and edge services using Lua scripting.
§01

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.

§02

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.

§03

How to use

  1. 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
  1. Add a Lua handler to your nginx.conf:
server {
  listen 8080;
  location /hi {
    content_by_lua_block {
      ngx.say('hello from openresty')
    }
  }
}
  1. Reload and test:
sudo openresty -s reload
curl localhost:8080/hi
# hello from openresty
§04

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;
    }
  }
}
§05

Related on TokRepo

§06

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

What is the relationship between OpenResty and Kong?+

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.

Does OpenResty replace Nginx?+

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.

What Lua version does OpenResty use?+

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.

Can I use OpenResty for API rate limiting?+

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.

How does OpenResty performance compare to Nginx?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets