<-
Apache > HTTP Server > Documentation > Version 2.3 > Modules

Apache Module mod_lua

Available Languages:  en 

Description:Provides Lua hooks into various portions of the httpd request processing
Status:Experimental
Module Identifier:lua_module
Source File:mod_lua.c
Compatibility:2.4 and later

Summary

Someone needs to write this.

Directives

Topics

top

Basic Configuration

The basic module loading directive is

LoadModule apreq_module modules/mod_apreq2.so
LoadModule wombat_module modules/mod_wombat.so

mod_lua provides a handler named lua-script, which can be used with an AddHandler directive:

AddHandler lua-script .lua

This will cause any .lua file to be evaluated by mod_lua.

top

Writing Handlers

mod_lua always looks to invoke a function for the handler, rather than just evaluating a script body CGI style. A handler function looks something like this:

example.lua

    require "string"

    function handle_something(r)
        r.content_type = "text/plain"
        r:puts("Hello Lua World!\n")
    
        if r.method == 'GET' then
            for k, v in pairs( r:parseargs() ) do
                r:puts( string.format("%s: %s", k, v) )
            end
        elseif r.method == 'POST' then
            for k, v in pairs( r:parsebody() ) do
                r:puts( string.format("%s: %s", k, v) )
            end
        else
            r:puts("unknown HTTP method " .. r.method)
        end 
    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.

top

Data Structures

request_rec

The request_rec is mapped in as a userdata. It has a metatable which lets you do useful things with it. For the most part it has the same fields as the request_rec struct (see httpd.h until we get better docs here) many of which are writeable as well as readable, and has (at least) the following methods:

r:puts("hello", " world", "!") -- print to response body

top

Logging Functions

r:debug("This is a debug log message")
r:info("This is an info log message")
r:notice("This is an notice log message")
r:warn("This is an warn log message")
r:err("This is an err log message")
r:alert("This is an alert log message")
r:crit("This is an crit log message")
r:emerg("This is an emerg log message")

top

LuaCodeCache Directive

Description:Configure the compiled code cache.
Syntax:LuaCodeCache stat|forever|never
Context:
Status:Experimental
Module:mod_lua

Specify the behavior of the in-memory code cache. The default is stat, which stats the top level script (not any included ones) each time that file is needed, and reloads it if the modified time indicates it is newer than the one it has already loaded. The other values cause it to keep the file cached forever (don't stat and replace) or to never cache the file.

In general stat or forever is good production and stat or never for deveopment.

Examples:

LuaCodeCache stat
LuaCodeCache forever
LuaCodeCache never

top

LuaHookAccessChecker Directive

Description:
Syntax:LuaHookAccessChecker /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

...

top

LuaHookAuthChecker Directive

Description:
Syntax:LuaHookAuthChecker /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

...

top

LuaHookAuthChecker Directive

Description:
Syntax:LuaHookAuthChecker /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

...

top

LuaHookCheckUserID Directive

Description:Provide a hook for the check_user_id phase of request processing
Syntax:LuaHookCheckUserID /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

...

top

LuaHookFixups Directive

Description:Provide a hook for the fixups phase of request processing
Syntax:LuaHookFixups /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua
top

LuaHookInsertFilter Directive

Description:
Syntax:LuaHookInsertFilter /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

Not Yet Implemented

top

LuaHookMapToStorage Directive

Description:Provide a hook for the map_to_storage phase of request processing
Syntax:LuaHookMapToStorage /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

...

top

LuaHookTranslateName Directive

Description:Provide a hook for the translate name phase of request processing
Syntax:LuaHookTranslateName /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

Add a hook (at APR_HOOK_MIDDLE) to the translate name phase of request processing. The hook function receives a single argument, the request_rec, and should return a status code, which is either an HTTP error code, or the constants defined in the apache2 module: apache2.OK, apache2.DECLINED, or apache2.DONE.

For those new to hooks, basically each hook will be invoked until one of them returns apache2.OK. If your hook doesn't want to do the translation it should just return apache2.DECLINED. If the request should stop processing, then return apache2.DONE.

Example:

LuaHookTranslateName /scripts/conf/hooks.lua silly_mapper

-- /scripts/conf/hooks.lua --
function silly_mapper(r)
if r.uri == "/" then
r.file = "/var/www/home.lua"
return apache2.OK
else
return apache2.DECLINED
end
end

top

LuaHookTypeChecker Directive

Description:Provide a hook for the type_checker phase of request processing
Syntax:LuaHookTypeChecker /path/to/lua/script.lua hook_function_name
Context:
Status:Experimental
Module:mod_lua

...

top

LuaMapHandler Directive

Description:Map a path to a lua handler
Syntax:LuaMapHandler uri-pattern /path/to/lua/script.lua [function-name]
Context:
Status:Experimental
Module:mod_lua

This directive matches a uri pattern to invoke a specific handler function in a specific file. It uses PCRE regular expressions to match the uri, and supports interpolating match groups into both the file path and the function name be careful writing your regular expressions to avoid security issues.

Examples:

LuaMapHandler /(\w+)/(/w+) /scripts/$1.lua handle_$2

This would match uri's such as /photos/show?id=9 to the file /scripts/photos.lua and invoke the handler function handle_show on the lua vm after loading that file.

LuaMapHandler /bingo /scripts/wombat.lua

This would invoke the "handle" function, which is the default if no specific function name is provided.

top

LuaPackageCPath Directive

Description:Add a directory to lua's package.cpath
Syntax:LuaPackageCPath /path/to/include/?.soa
Context:
Status:Experimental
Module:mod_lua

Add a path to lua's shared library search path. Follows the same conventions as lua. This just munges the package.cpath in the lua vms.

top

LuaPackagePath Directive

Description:Add a directory to lua's package.path
Syntax:LuaPackagePath /path/to/include/?.lua
Context:
Status:Experimental
Module:mod_lua

Add a path to lua's module search path. Follows the same conventions as lua. This just munges the package.path in the lua vms.

Examples:

LuaPackagePath /scripts/lib/?.lua
LuaPackagePath /scripts/lib/?/init.lua

top

LuaQuickHandler Directive

Description:Provide a hook for the quick handler of request processing
Syntax:
Context:
Status:Experimental
Module:mod_lua

...

top

LuaRoot Directive

Description:Specify the base path for resolving relative paths for mod_lua directives
Syntax:LuaRoot /path/to/a/directory
Context:
Status:Experimental
Module:mod_lua

Specify the base path which will be used to evaluate all relative paths within mod_wombat. If not specified they will be resolved relative to the current working directory, which may not always work well for a server.

top

LuaScope Directive

Description:One of once, request, conn, server -- default is once
Syntax:LuaScope once|request|conn|server [max|min max]
Context:
Status:Experimental
Module:mod_lua

Specify the lifecycle scope of the Lua interpreter which will be used by handlers in this "Directory." The default is "once"

once:
use the interpreter once and throw it away.
request:
use the interpreter to handle anything based on the same file within this request, which is also request scoped.
conn:
Same as request but attached to the connection_rec
server:
This one is different than others because the server scope is quite long lived, and multiple threads will have the same server_rec. To accommodate this server scoped interpreter are stored in an apr resource list. The min and max arguments are intended to specify the pool size, but are unused at this time.

Available Languages:  en