Embed the Power of Lua into NGINX HTTP servers
Embed the Power of Lua into NGINX HTTP servers
ngx_http_lua_module - Embed the power of Lua into Nginx HTTP Servers.
This module is a core component of OpenResty. If you are using this module, then you are essentially using OpenResty :)
This module is not distributed with the Nginx source. See the installation instructions.
Production ready.
This document describes ngx_lua v0.10.29, which was released on Oct 24, 2025.
…This module embeds LuaJIT 2.0/2.1 into Nginx. It is a core component of OpenResty. If you are using this module, then you are essentially using OpenResty.
Since version v0.10.16 of this module, the standard Lua
interpreter (also known as "PUC-Rio Lua") is not supported anymore. This
document interchangeably uses the terms "Lua" and "LuaJIT" to refer to the
LuaJIT interpreter.
By leveraging Nginx's subrequests, this module allows the integration of the powerful Lua threads (known as Lua "coroutines") into the Nginx event model.
Unlike Apache's mod_lua and Lighttpd's mod_magnet, Lua code executed using this module can be 100% non-blocking on network traffic as long as the Nginx API for Lua provided by this module is used to handle requests to upstream services such as MySQL, PostgreSQL, Memcached, Redis, or upstream HTTP web services.
At least the following Lua libraries and Nginx modules can be used with this module:
Almost any Nginx modules can be used with this ngx_lua module by means of
ngx.location.capture or
ngx.location.capture_multi but it is
recommended to use those lua-resty-* libraries instead of creating
subrequests to access the Nginx upstream modules because the former is usually
much more flexible and memory-efficient.
The Lua interpreter (also known as "Lua State" or "LuaJIT VM instance") is shared across all the requests in a single Nginx worker process to minimize memory use. Request contexts are segregated using lightweight Lua coroutines.
Loaded Lua modules persist in the Nginx worker process level resulting in a small memory footprint in Lua even when under heavy loads.
This module is plugged into Nginx's "http" subsystem so it can only speak downstream communication protocols in the HTTP family (HTTP 0.9/1.0/1.1/2.0, WebSockets, etc...). If you want to do generic TCP communications with the downstream clients, then you should use the ngx_stream_lua module instead, which offers a compatible Lua API.
Just to name a few:
The possibilities are unlimited as the module allows bringing together various elements within Nginx as well as exposing the power of the Lua language to the user. The module provides the full flexibility of scripting while offering performance levels comparable with native C language programs both in terms of CPU time as well as memory footprint thanks to LuaJIT 2.x.
Other scripting language implementations typically struggle to match this performance level.
The latest version of this module is compatible with the following versions of Nginx:
Nginx cores older than 1.6.0 (exclusive) are not supported.
It is highly recommended to use OpenResty releases which bundle Nginx, ngx_lua (this module), LuaJIT, as well as other powerful companion Nginx modules and Lua libraries.
It is discouraged to build this module with Nginx yourself since it is tricky to set up exactly right.
Note that Nginx, LuaJIT, and OpenSSL official releases have various limitations and long-standing bugs that can cause some of this module's features to be disabled, not work properly, or run slower. Official OpenResty releases are recommended because they bundle OpenResty's optimized LuaJIT 2.1 fork and Nginx/OpenSSL patches.
Alternatively, ngx_lua can be manually compiled into Nginx:
Build the source with this module:
…Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the --add-dynamic-module=PATH option instead of --add-module=PATH on the
./configure command line above. And then you can explicitly load the module in your nginx.conf via the load_module
directive, for example,
load_module /path/to/modules/ndk_http_module.so; # assuming NDK is built as a dynamic module too
load_module /path/to/modules/ngx_http_lua_module.so;While building this module either via OpenResty or with the Nginx core, you can define the following C macros via the C compiler options:
NGX_LUA_USE_ASSERT
When defined, will enable assertions in the ngx_lua C code base. Recommended for debugging or testing builds. It can introduce some (small) runtime overhead when enabled. This macro was first introduced in the v0.9.10 release.NGX_LUA_ABORT_AT_PANIC
When the LuaJIT VM panics, ngx_lua will instruct the current nginx worker process to quit gracefully by default. By specifying this C macro, ngx_lua will abort the current nginx worker process (which usually results in a core dump file) immediately. This option is usefultest: CI test with DNS resolved feature is flaky
heap-use-after-free in ngx_http_lua_socket_tcp_finalize after receiveuntil userdata GC
[alert] 65#65: worker process 74 exited on signal 11 (core dumped)
Support TLS 1.3 certificate compression for dynamic certificates
failed to build the module for Win32 using MSYS2
Outdated ubuntu install guide
libluajit-5.1.so.2: undefined symbol: ngx_lua_ffi_disable_listening_unix_socket
bug: `tcpsock:getsslsession()` returns "not resumable" with BoringSSL (TLS 1.3), breaking session reuse tests
Response body missing when using ngx.location.capture() to fetch response from a location that uses proxy_pass, cache & aio
[Feature Request] Support lua ability in nginx-quic