Text of httpd.apache.org › docs › current › mod › mod_lua.html as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

mod_lua - Apache HTTP Server Version 2.4

Modules | Directives | FAQ | Glossary | Sitemap | Report a bug Apache HTTP Server Version 2.4 Apache > HTTP Server > Documentation > Version 2.4 > Modules Apache Module mod_lua Available Languages: en | fr Description: Provides Lua hooks into various portions of the httpd request processing Status: Extension Module Identifier: lua_module Source File: mod_lua.c Compatibility: 2.3 and later Summary This module allows the server to be extended with scripts written in the Lua programming language.

The extension points (hooks) available with mod_lua include many of the hooks available to natively compiled Apache HTTP Server modules, such as mapping requests to files, generating dynamic responses, access control, authentication, and authorization More information on the Lua programming language can be found at the the Lua website . Warning This module holds a great deal of power over httpd, which is both a strength and a potential security risk.

It is not recommended that you use this module on a server that is shared with users you do not trust, as it can be abused to change the internal workings of httpd. Topics Basic Configuration Writing Handlers Writing Authorization Providers Writing Hooks Data Structures Built in functions Logging Functions apache2 Package Modifying contents with Lua filters Database connectivity Directives LuaAuthzProvider LuaCodeCache LuaHookAccessChecker LuaHookAuthChecker LuaHookCheckUserID LuaHookFixups LuaHookInsertFilter LuaHookLog LuaHookMapToStorage LuaHookPreTranslate LuaHookTranslateName LuaHookTypeChecker LuaInherit LuaInputFilter LuaMapHandler LuaOutputFilter LuaPackageCPath LuaPackagePath LuaQuickHandler LuaRoot LuaScope Bugfix checklist httpd changelog Known issues Report a bug Basic Configuration ¶ The basic module loading directive is LoadModule lua_module modules/mod_lua.so mod_lua provides a handler named lua-script , which can be used with a SetHandler or AddHandler directive: <Files "*.lua"> SetHandler lua-script </Files> This will cause mod_lua to handle requests for files ending in .lua by invoking that file's handle function.

For more flexibility, see LuaMapHandler . Writing Handlers ¶ In the Apache HTTP Server API, the handler is a specific kind of hook responsible for generating the response. Examples of modules that include a handler are mod_proxy , mod_cgi , and mod_status . mod_lua always looks to invoke a Lua function for the handler, rather than just evaluating a script body CGI style. A handler function looks something like this: example.lua -- example handler require "string" --[[ This is the default method name for Lua handlers, see the optional function-name in the LuaMapHandler directive to choose a different entry point. --]] function handle(r) r.content_type = "text/plain" if r.method == 'GET' then r:puts("Hello Lua World!\n") for k, v in pairs( r:parseargs() ) do r:puts( string.format("%s: %s\n", k, v) ) end elseif r.method == 'POST' then r:puts("Hello Lua World!\n") for k, v in pairs( r:parsebody() ) do r:puts( string.format("%s: %s\n", k, v) ) end elseif r.method == 'PUT' then -- use our own Error contents r:puts("Unsupported HTTP method " .. r.method) r.status = 405 return apache2.OK else -- use the ErrorDocument return 501 end return apache2.OK end This handler function just prints out the uri or form encoded arguments to a plaintext page.

This means (and in fact encourages) that you can have multiple handlers (or hooks, or filters) in the same script. Writing Authorization Providers ¶ mod_authz_core provides a high-level interface to authorization that is much easier to use than using into the relevant hooks directly. The first argument to the Require directive gives the name of the responsible authorization provider. For any Require line, mod_authz_core will call the authorization provider of the given name, passing the rest of the line as parameters.

The provider will then check authorization and pass the result as return value. The authz provider is normally called before authentication. If it needs to know the authenticated user name (or if the user will be authenticated at all), the provider must return apache2.AUTHZ_DENIED_NO_USER . This will cause authentication to proceed and the authz provider to be called a second time. The following authz provider function takes two arguments, one ip address and one user name.

It will allow access from the given ip address without authentication, or if the authenticated user matches the second argument: authz_provider.lua require 'apache2' function authz_check_foo(r, ip, user) if r.useragent_ip == ip then return apache2.AUTHZ_GRANTED elseif r.user == nil then return apache2.AUTHZ_DENIED_NO_USER elseif r.user == user then return apache2.AUTHZ_GRANTED else return apache2.AUTHZ_DENIED end end The following configuration registers this function as provider foo and configures it for URL / : LuaAuthzProvider foo authz_provider.lua authz_check_foo <Location "/"> Require foo 10.1.2.3 john_doe </Location> Writing Hooks ¶ Hook functions are how modules (and Lua scripts) participate in the processing of requests.

Each type of hook exposed by the server exists for a specific purpose, such as mapping requests to the file system, performing access control, or setting mime types: Hook phase mod_lua directive Description Quick handler LuaQuickHandler This is the first hook that will be called after a request has been mapped to a host or virtual host Pre-Translate name LuaHookPreTranslateName This phase translates the requested URI into a filename on the system, before decoding occurs.

Modules such as mod_proxy can operate in this phase. Translate name LuaHookTranslateName This phase translates the requested URI into a filename on the system. Modules such as mod_alias and mod_rewrite operate in this phase. Map to storage LuaHookMapToStorage This phase maps files to their physical, cached or external/proxied storage. It can be used by proxy or caching modules Check Access LuaHookAccessChecker This phase checks whether a client has access to a resource.

This phase is run before the user is authenticated, so beware. Check User ID LuaHookCheckUserID This phase it used to check the negotiated user ID Check Authorization LuaHookAuthChecker or LuaAuthzProvider This phase authorizes a user based on the negotiated credentials, such as user ID, client certificate etc. Check Type LuaHookTypeChecker This phase checks the requested file and assigns a content type and a handler to it Fixups LuaHookFixups This is the final "fix anything" phase before the content handlers are run.

Any last-minute changes to the request should be made here. Content handler fx. .lua files or through LuaMapHandler This is where the content is handled. Files are read, parsed, some are run, and the result is sent to the client Logging LuaHookLog Once a request has been handled, it enters several logging phases, which logs the request in either the error or access log. Mod_lua is able to hook into the start of this and control logging output.

Hook functions are passed the request object as their only argument (except for LuaAuthzProvider, which also gets passed the arguments from the Require directive). They can return any value, depending on the hook, but most commonly they'll return OK, DONE, or DECLINED, which you can write in Lua as apache2.OK , apache2.DONE , or apache2.DECLINED , or else an HTTP status code. translate_name.lua -- example hook that rewrites the URI to a filesystem path. require 'apache2' function translate_name(r) if r.uri == "/translate-name" then r.filename = r.document_root ..

"/find_me.txt" return apache2.OK end -- we don't care about this URL, give another module a chance return apache2.DECLINED end translate_name2.lua --[[ example hook that rewrites one URI to another URI. It returns a apache2.DECLINED to give other URL mappers a chance to work on the substitution, including the core translate_name hook which maps based on the DocumentRoot. Note: Use the early/late f

The rest of this page is on the live site.