<div class="section">
<h2><a name="basic_remap" id="basic_remap">Example 1: A basic remapping module</a></h2>
<p>
-
+ These first examples show how mod_lua can be used to rewrite URIs in the same
+ way that one could do using <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> or
+ <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>, but with more clarity
+ on how the decision-making takes place, as well as allowing for more complex
+ decisions than would otherwise be allowed with said directives.
</p>
<pre class="prettyprint lang-config">
function remap(r)
-- Test if the URI matches our criteria
- local barFile = r.uri:match("/foo/([a-zA-Z0-9]+%.bar)")
+ local barFile = r.uri:match("/foo/([a-zA-Z0-9]+)%.bar")
if barFile then
r.filename = "/internal/" .. barFile
end
Advanced remap example.
This example will evaluate some conditions, and based on that,
remap a file to one of two destinations, using a rewrite map.
-
+ This is similar to mixing AliasMatch and ProxyPass, but
+ without them clashing in any way. Assuming we are on example.com, then:
+
+ http://example.com/photos/test.png will be rewritten as /uploads/www/test.png
+ http://example.com/ext/foo.html will be proxied to http://www.external.com/foo.html
+ URIs that do not match, will be served by their respective default handlers
]]--
local map = {
},
externals = {
source = [[^/ext/(.*)$]],
- destination = [[http://www.example.com/$1]],
+ destination = [[http://www.external.com/$1]],
proxy = true
}
}
<div class="section">
<h2><a name="mass_vhost" id="mass_vhost">Example 2: Mass virtual hosting</a></h2>
<p>
-
+ As with simple and advanced rewriting, you can use mod_lua for dynamically
+ assigning a hostname to a specific document root, much like
+ <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> does, but with more control over what goes
+ where. This could be as simple as a table holding the information about which
+ host goes into which folder, or more advanced, using a database holding the
+ document roots of each hostname.
</p>
<pre class="prettyprint lang-config">
local timeout = 60
-- Function for querying the database for saved vhost entries
-function query_vhosts(host)
+function query_vhosts(r)
+ local host = r.hostname
if not cached_vhosts[host] or (cached_vhosts[host] and cached_vhosts[host].updated < os.time() - timeout) then
- local db = apache2.dbopen(r,"mod_dbd")
- local _host = db:escape(_host)
- local res, err = db:query( ("SELECT `destination` FROM `vhosts` WHERE `hostname` = '%s' LIMIT 1"):format(_host) )
+ local db,err = ap.dbopen(r,"mod_dbd")
+ local _host = db:escape(r,host)
+ local res, err = db:query(r, ("SELECT `destination` FROM `vhosts` WHERE `hostname` = '%s' LIMIT 1"):format(_host) )
if res and #res == 1 then
cached_vhosts[host] = { updated = os.time(), destination = res[1][1] }
else
- cached_vhosts[host] = nil
+ cached_vhosts[host] = { updated = os.time(), destination = nil } -- don't re-query whenever there's no result, wait a while.
end
db:close()
end
function mass_vhost(r)
-- Check whether the hostname is in our database
- local destination = query_vhosts(r.hostname)
+ local destination = query_vhosts(r)
if destination then
-- If found, rewrite and change document root
local filename = r.filename:sub(r.document_root:len()+1)
r.filename = destination .. filename
- apahce2.set_document_root(destination)
+ ap.set_document_root(r,destination)
return apache2.OK
end
return apache2.DECLINED
<p>
-bla bla
+
</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="basic_auth" id="basic_auth">Example 3: A basic authorization hook</a></h2>
-
+<p>
+ 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.
+</p>
<pre class="prettyprint lang-config">
LuaHookAuthChecker /path/too/foo.lua check_auth
</pre>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="authz" id="authz">Example 4: Authorization using LuaAuthzProvider</a></h2>
-
+<p>
+ 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:
+</p>
<pre class="prettyprint lang-config">
LuaAuthzProvider rights /path/to/lua/script.lua rights_handler
<Directory /www/private>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="map_handler" id="map_handler">Example 5: Overlays using LuaMapHandler</a></h2>
-
+<p>
+Coming soon!
+</p>
<pre class="prettyprint lang-config">
LuaMapHandler ^/portal/([a-z]+)/ /path/to/lua/script.lua handle_$1
</pre>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="mod_status_lua" id="mod_status_lua">Example 6: Basic Lua scripts</a></h2>
+<p>
+Also coming soon
+</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="String_manipulation" id="String_manipulation">HTTPd bindings: String manipulation</a></h2>
</a></h3>
<p>
-convert an OS path to a URL in an OS dependant way.
+convert an OS path to a URL in an OS dependent way.
</p>
<p>
<em>Arguments:</em>