Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

lua-nginx-module

> 编程语言
Open source

Embed the Power of Lua into NGINX HTTP servers

11.8K stars0 likes5 views
WebsiteGitHub

About

Embed the Power of Lua into NGINX HTTP servers

Name

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.

Table of Contents

  • Name
  • Status
  • Version
  • Videos
  • Synopsis
  • Description
  • Typical Uses
  • Nginx Compatibility
  • Installation
    • Building as a dynamic module
    • C Macro Configurations
  • Community
    • English Mailing List
    • Chinese Mailing List
  • Code Repository
  • Bugs and Patches
  • LuaJIT bytecode support
  • System Environment Variable Support
  • HTTP 1.0 support
  • Statically Linking Pure Lua Modules
  • Data Sharing within an Nginx Worker
  • Known Issues
    • TCP socket connect operation issues
    • Lua Coroutine Yielding/Resuming
    • Lua Variable Scope
    • Locations Configured by Subrequest Directives of Other Modules
    • Cosockets Not Available Everywhere
    • Special Escaping Sequences
    • Mixing with SSI Not Supported
    • SPDY Mode Not Fully Supported
    • Missing data on short circuited requests
  • TODO
  • Changes
  • Build And Test
  • Test Suite
  • Copyright and License
  • See Also
  • Directives
  • Nginx API for Lua
  • Obsolete Sections
    • Special PCRE Sequences
    • Lua/LuaJIT bytecode support

Status

Production ready.

Version

This document describes ngx_lua v0.10.29, which was released on Oct 24, 2025.

Videos

  • YouTube video "Hello World HTTP Example with OpenResty/Lua"
  • YouTube video "Write Your Own Lua Modules in OpenResty/Nginx Applications"
  • YouTube video "OpenResty's resty Command-Line Utility Demo"
  • YouTube video "Measure Execution Time of Lua Code Correctly in OpenResty"
  • YouTube video "Precompile Lua Modules into LuaJIT Bytecode to Speedup OpenResty Startup" You are welcome to subscribe to our official YouTube channel, OpenResty.

Back to TOC

Synopsis

…

Back to TOC

Description

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:

  • lua-resty-memcached
  • lua-resty-mysql
  • lua-resty-redis
  • lua-resty-dns
  • lua-resty-upload
  • lua-resty-websocket
  • lua-resty-lock
  • lua-resty-logger-socket
  • lua-resty-lrucache
  • lua-resty-string
  • ngx_memc
  • ngx_postgres
  • ngx_redis2
  • ngx_redis
  • ngx_proxy
  • ngx_fastcgi

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.

Back to TOC

Typical Uses

Just to name a few:

  • Mashup'ing and processing outputs of various Nginx upstream outputs (proxy, drizzle, postgres, redis, memcached, etc.) in Lua,
  • doing arbitrarily complex access control and security checks in Lua before requests actually reach the upstream backends,
  • manipulating response headers in an arbitrary way (by Lua)
  • fetching backend information from external storage backends (like redis, memcached, mysql, postgresql) and use that information to choose which upstream backend to access on-the-fly,
  • coding up arbitrarily complex web applications in a content handler using synchronous but still non-blocking access to the database backends and other storage,
  • doing very complex URL dispatch in Lua at rewrite phase,
  • using Lua to implement advanced caching mechanism for Nginx's subrequests and arbitrary locations.

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.

Back to TOC

Nginx Compatibility

The latest version of this module is compatible with the following versions of Nginx:

  • 1.31.x (last tested: 1.31.2)
  • 1.31.x (last tested: 1.31.1)
  • 1.29.x (last tested: 1.29.8)
  • 1.29.x (last tested: 1.29.2)
  • 1.27.x (last tested: 1.27.1)
  • 1.25.x (last tested: 1.25.1)
  • 1.21.x (last tested: 1.21.4)
  • 1.19.x (last tested: 1.19.3)
  • 1.17.x (last tested: 1.17.8)
  • 1.15.x (last tested: 1.15.8)
  • 1.14.x
  • 1.13.x (last tested: 1.13.6)
  • 1.12.x
  • 1.11.x (last tested: 1.11.2)
  • 1.10.x
  • 1.9.x (last tested: 1.9.15)
  • 1.8.x
  • 1.7.x (last tested: 1.7.10)
  • 1.6.x

Nginx cores older than 1.6.0 (exclusive) are not supported.

Back to TOC

Installation

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:

  1. LuaJIT can be downloaded from the latest release of OpenResty's LuaJIT fork. The official LuaJIT 2.x releases are also supported, although performance will be significantly lower for reasons elaborated above
  2. Download the latest version of the ngx_devel_kit (NDK) module HERE
  3. Download the latest version of ngx_lua HERE
  4. Download the latest supported version of Nginx HERE (See Nginx Compatibility)
  5. Download the latest version of the lua-resty-core HERE
  6. Download the latest version of the lua-resty-lrucache HERE

Build the source with this module:

…

Back to TOC

Building as a dynamic 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,

nginx

 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;

Back to TOC

C Macro Configurations

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 useful

Issues· 398 open

View all issuesOpen on GitHub
  • #2493

    test: CI test with DNS resolved feature is flaky

    Updated Sep 24, 2026
  • #2517

    heap-use-after-free in ngx_http_lua_socket_tcp_finalize after receiveuntil userdata GC

    Updated Aug 18, 2026
  • #2486

    [alert] 65#65: worker process 74 exited on signal 11 (core dumped)

    Updated Aug 18, 2026
  • #2516

    Support TLS 1.3 certificate compression for dynamic certificates

    Updated Jul 31, 2026
  • #2498

    failed to build the module for Win32 using MSYS2

    Updated May 8, 2026
  • #2467

    Outdated ubuntu install guide

    Updated Apr 28, 2026
  • #2465

    libluajit-5.1.so.2: undefined symbol: ngx_lua_ffi_disable_listening_unix_socket

    Updated Apr 18, 2026
  • #2481

    bug: `tcpsock:getsslsession()` returns "not resumable" with BoringSSL (TLS 1.3), breaking session reuse tests

    Updated Mar 10, 2026
  • #587

    Response body missing when using ngx.location.capture() to fetch response from a location that uses proxy_pass, cache & aio

    Updated Feb 12, 2026
  • #1893

    [Feature Request] Support lua ability in nginx-quic

    Updated Jan 9, 2026

> Tags

C

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言