From: Daniel Gruno Date: Thu, 6 Sep 2012 10:25:54 +0000 (+0000) Subject: Google has a way of finding everything, so let's update this guide just a bit (fix... X-Git-Tag: 2.5.0-alpha~6353 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a0564b650d3367910c318893e46d4449d20d8456;p=thirdparty%2Fapache%2Fhttpd.git Google has a way of finding everything, so let's update this guide just a bit (fix errors, expand on descriptions) git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1381549 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/docs/manual/developer/lua.xml b/docs/manual/developer/lua.xml index c379d0c945f..a3311f7a955 100644 --- a/docs/manual/developer/lua.xml +++ b/docs/manual/developer/lua.xml @@ -151,7 +151,11 @@ end
Example 1: A basic remapping module

- + These first examples show how mod_lua can be used to rewrite URIs in the same + way that one could do using Alias or + RewriteRule, 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.

@@ -169,7 +173,7 @@ LuaHookTranslateName /path/too/foo.lua remap 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 @@ -185,7 +189,12 @@ 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 = { @@ -196,7 +205,7 @@ local map = { }, externals = { source = [[^/ext/(.*)$]], - destination = [[http://www.example.com/$1]], + destination = [[http://www.external.com/$1]], proxy = true } } @@ -236,7 +245,12 @@ bla bla
Example 2: Mass virtual hosting

- + As with simple and advanced rewriting, you can use mod_lua for dynamically + assigning a hostname to a specific document root, much like + mod_vhost_alias 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.

@@ -289,15 +303,16 @@ local cached_vhosts = {} 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 @@ -310,12 +325,12 @@ 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 @@ -324,7 +339,7 @@ end

-bla bla +

@@ -332,7 +347,11 @@ bla bla
Example 3: A basic authorization hook - +

+ 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. +

LuaHookAuthChecker /path/too/foo.lua check_auth @@ -445,7 +464,12 @@ end
Example 4: Authorization using LuaAuthzProvider - +

+ 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: +

LuaAuthzProvider rights /path/to/lua/script.lua rights_handler <Directory /www/private> @@ -489,13 +513,18 @@ end
Example 5: Overlays using LuaMapHandler - +

+Coming soon! +

LuaMapHandler ^/portal/([a-z]+)/ /path/to/lua/script.lua handle_$1
Example 6: Basic Lua scripts +

+Also coming soon +

@@ -763,7 +792,7 @@ r:puts(md5) -- prints out "9e107d9d372bb6826bd81d3542a419d6" )

-convert an OS path to a URL in an OS dependant way. +convert an OS path to a URL in an OS dependent way.

Arguments: