This document expands on the
Stuff about what
This document will discuss several cases where
First and foremost, you are expected to have a basic knowledge of how the Lua programming language works. In most cases, we will try to be as pedagogical as possible and link to documents describing the functions used in the examples, but there are also many cases where it is necessary to either just assume that "it works" or do some digging yourself into what the hows and whys of various function calls.
Setting the right once, which means
that every call to a Lua script will spawn a new Lua state that handles that
script and is destroyed immediately after. This option keeps the memory
footprint of mod_lua low, but also affects the processing speed of a request.
If you have the memory to spare, you can set the scope to thread,
which will make mod_lua spawn a Lua state that lasts the entirity of a thread's
lifetime, speeding up request processing by 2-3 times. Since mod_lua will create
a state for each script, this may be an expensive move, memory-wise, so to
compromise between speed and memory usage, you can choose the server
option to create a pool of Lua states to be used. Each request for a Lua script or
a hook function will then acquire a state from the pool and release it back when it's
done using it, allowing you to still gain a significant performance increase, while
keeping your memory footprint low. Some examples of possible settings are:
As a general rule of thumb: If your server has none to low usage, use once
or request, if your server has low to medium usage, use the server
pool, and if it has high usage, use the thread setting. As your server's
load increases, so will the number of states being actively used, and having your scope
set to once/request/conn will stop being beneficial to your memory footprint.
Note: The min and max settings for the
server scope denotes the minimum and maximum states to keep in a pool per
server process, so keep this below your ThreadsPerChild limit.
By default, forever value, which will cause mod_lua
to skip the stat process and always reuse the compiled byte-code from the first access to the
script, thus speeding up the processing. For Lua hooks, this can prove to increase peformance,
while for scripts handled by the lua-script handler, the increase in performance
may be negligible, as files httpd will stat the files regardless.
For maximum performance, it is generally recommended that any initialization of libraries, constants and master tables be kept outside the handle's scope:
These first examples show how mod_lua can be used to rewrite URIs in the same
way that one could do using
bla bla
As with simple and advanced rewriting, you can use mod_lua for dynamically
assigning a hostname to a specific document root, much like
With the authorization hooks, you can add custom auth phases to your request processing, allowing you to either add new requirements that were not previously supported by httpd, or tweaking existing ones to accommodate your needs.
If you require even more advanced control over your authorization phases, you can add custom authz providers to help you manage your server. The example below shows you how you can split a single htpasswd file into groups with different permissions:
Coming soon!
Also coming soon
apache2.base64_encode
apache2.base64_decode
apache2.escape
apache2.unescape
apache2.escapehtml
apache2.md5
apache2.sha1
apache2.os_escape_path
apache2.escape_logitem
Decodes a base64-encoded string
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| string | The string to decode |
Return value(s):
The base64-decoded string.
Example:
Encodes a string using the base64 encoding scheme.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| string | The string to encode |
Example:
url-escapes a string
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| string | The string to escape |
Return value(s):
The URL-escaped string.
Example:
Escape a string for logging
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| path | The string to escape |
Return value(s):
The converted string
Escapes HTML entities.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| html | The HTML code to escape |
| toasc | Whether to escape all non-ASCI characters as &#nnn; |
Return value(s):
The escaped HTML code.
Example:
Computes an MD5 digest sum based on a string (binary safe)
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| string | The (binary) string to digest |
Return value(s):
The MD5 digest sum of the data provided
Example:
convert an OS path to a URL in an OS dependent way.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| path | The path to convert |
| partial | partial if set, assume that the path will be appended to something with a '/' in it (and thus does not prefix "./") |
Return value(s):
The converted URL
Example:
Computes an SHA-1 digest sum based on a string (binary safe)
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| string | The (binary) string to digest |
Return value(s):
The SHA-1 digest sum of the data provided
Example:
unescapes an URL-escaped string
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| string | The string to unescape |
Return value(s):
The URL-unescaped string
Example:
apache2.requestbody
apache2.add_input_filter
apache2.get_basic_auth_pw
apache2.set_document_root
apache2.set_context_prefix
apache2.get_server_name_for_url
apache2.set_keepalive
apache2.make_etag
apache2.send_interim_response
Adds an input filter to the request
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| filter | The name of the filter handler to add |
Example:
Returns the password from a basic authorization request or nil if none was supplied
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
Return value(s):
The password from a basic authorization request or nil if none was supplied
Get the current server name from the request for the purposes of using in a URL. If the server name is an IPv6 literal address, it will be returned in URL format (e.g., "[fe80::1]").
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
Constructs an entity tag from the resource information. If it's a real file, build in some of the file characteristics.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| force_weak | force_weak Force the entity tag to be weak - it could be modified again in as short an interval. |
Return value(s):
The entity tag
Reads the request body. If a filename is specified, the request body will be written to that file and the number of bytes written returned, otherwise, the full request body will be returned as a string.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| size | The maximum size allowed, or 0/nil for unlimited size |
| filename | The file to save the output to, or nil to return it as a string |
Return value(s):
The number of bytes written if a filename was specified, otherwise it returns the entire request body as a string.
Example:
Sends an interim (HTTP 1xx) response immediately.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| send_headers | send_headers Whether to send&clear headers in r->headers_out |
Example:
Set context_prefix and context_document_root for a request.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| prefix | The URI prefix, without trailing slash |
| document | The corresponding directory on disk, without trailing slash |
Sets the document root of the request.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| root | root |
Example:
Sets the keepalive status for this request
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
Return value(s):
True if keepalive can be set, false otherwise
apache2.expr
apache2.regex
apache2.strcmp_match
Evaluates an ap_expr (think <If ...>) expression and returns true if the expression is true, false otherwise. A second value containing an error string is returned if the expression is invalid.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| expression | expression |
Return value(s):
True if the expression evaluates as true, false if the expression doesn't evaluate as true or if an error occurred. If an error occurred during parsing, a second value will be returned, containng the error string.
Example:
Evaluates a regular expression and, if it matches the source string, captures the variables and returns the matches as a table. On error, it returns nil.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| expression | expression to match for |
| source | the source string to capture from |
Return value(s):
True if the expression evaluates as true, false if the expression doesn't evaluate as true or if an error occurred. If an error occurred during parsing, a second value will be returned, containng the error string.
Example:
Determines if a string matches a pattern containing the wildcards '?' or '*'
Arguments:
| Argument | Description |
|---|---|
| str | The string to check |
| expexted | The pattern to match against |
| ignoreCase | Whether to ignore case when matching |
Return value(s):
True if the two strings match, false otherwise.
Example:
apache2.add_version_component
apache2.mpm_query
apache2.terminate
apache2.scoreboard_process
apache2.scoreboard_worker
apache2.module_info
apache2.loaded_modules
apache2.runtime_dir_relative
apache2.server_info
apache2.state_query
apache2.custom_response
apache2.exists_config_define
Adds a component to the server description and banner strings
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| component | The component to add |
Example:
Install a custom response handler for a given status
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| status | The status for which the custom response should be used |
| string | The custom response. This can be a static string, a file or a URL |
Example:
Checks for a definition from the server command line
Arguments:
| Argument | Description |
|---|---|
| name | The define to check for |
Example:
Returns a table containing the name (c filename) of all loaded modules
Arguments:
None
Return value(s):
A table containing the name (c filename) of all loaded modules
Returns information about a specific module (if loaded)
Arguments:
| Argument | Description |
|---|---|
| c | c |
| file | file |
Return value(s):
The various commands available to this module as a table, or nil if the module wasn't found.
Queries the MPM for a specific value
Arguments:
| Argument | Description |
|---|---|
| i | i |
Return value(s):
The queried value
Returns the path of a file relative to the default runtime directory
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| file | file |
Return value(s):
The path of a file relative to the default runtime directory
Returns the scoreboard for a server daemon as a table
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| child | The server child to query |
Return value(s):
The scoreboard for a server daemon as a table
Returns the scoreboard for a single thread as a table
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| child | The server child to query |
| thread | The thread to query |
Return value(s):
The scoreboard for a single thread as a table
Returns a table with information about the server program
Arguments:
None
Return value(s):
A table with information about the server program
Query the server for some state information
Arguments:
| Argument | Description |
|---|---|
| field | Which information is requested |
Example:
Kills off a server process. This has no other use than to show how dangerous mod_lua can be ;)
Arguments:
None
apache2.dbopen
db:query
db:do
db:close
Opens up a new database connection. See the DB functions for mod_pLua for more info on this.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| dbtype | dbtype |
| conn_string | connection string |
Return value(s):
The database connection as a table with functions, or nil if the connection failed. If a connection failed, a second argument (string) with the error code is returned.
Example:
Closes a database connection
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
Example:
Executes a statement that doesn't return a result set
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| query | The SQL statement to execute |
Return value(s):
If the statement is valid, a table of results are returned. If an error occurred, the first return value is false and the second return value is a string containing an error message.
Example:
Queries the database for information using the specified statement.
Arguments:
| Argument | Description |
|---|---|
| r | The mod_lua request handle |
| query | The SQL statement to execute |
Return value(s):
If the statement is valid, a table of results are returned. If an error occurred, the first return value is false and the second return value is a string containing an error message.
Example:
Returns the current time in microseconds.
Arguments:
None
Return value(s):
The current time in microseconds.
Sleeps for a while. Floating point values can be used to sleep for less than a second.
Arguments:
| Argument | Description |
|---|---|
| seconds | The number of seconds to sleep. |
Example: