lighty's life

lighty developer blog

Lighttpd Unleashed - Part Two

The big thing of lighttpd 1.4.0 were the nested conditionals, but how can they be used ? And what are includes ?

A example for nested confitionals went already into the announcement:

$HTTP["host"] == "www.example.org" {
  $HTTP["url"] =~ "^/dav($|/)" {
    webdav.activate = "enable" 
  }
  $HTTP["remoteip"] != "10.0.0.0/16" {
    auth.require = ( "" => ( "method" => "basic",
                             "realm" => "webdav",
                             "require" => "valid-user" ) )
  }
}

But what are includes for ? For a start we want to go the mass-hosting way and want to create a generic pattern for virtual hosts without using mod_simple_vhost or similar modules.

We have a simple webserver structure:

  • all servers are under /var/www/servers/
  • the part is the hostname of the server
  • and logs and public files are at logs/ and pages/

resulting in /var/www/servers/www.example.org/pages/.

In lighttpd 1.3.x you had to copy the conditionals for all host including all the options. There was no way to do some interpretation of the config file.

In lighttpd 1.4.0 this has changed and we can use expressions and includes:

server.document-root = "/tmp"
server.port = 1025

server.modules = ( "mod_access" )

access.deny = ( "~", ".inc" )

$HTTP["host"] == "www.example.org" {
  var.hostname = "www.example.org"
  include "baseconfig.conf"
}

$HTTP["host"] == "www2.example.org" {
  var.hostname = "www2.example.org"
  include "baseconfig.conf"
}

and a baseconfig.conf

server.document-root = "/var/www/servers/" + hostname + "/pages/"
access.logfile = "/var/www/servers/" + hostname + "/logs/accesslog"

With var.hostname you can set a user-variable in the configuration which can be used everywhere in the config to pass data around. We use it here to substiture the parts of the baseconfig for each host and set the document-root and the location of the accesslog for each host.

But how does the config look like that is used by lighttpd ? The option -p will tell us:

$ /lighttpd -p -f ./lighttpd-includes.conf
config {
    server.document-root = "/tmp"
    server.port          = 1025
    server.modules       = ("mod_indexfile", "mod_access", "mod_dirlisting", "mod_staticfile")
    access.deny          = ("~", ".inc")


    $HTTP["host"] == "www.example.org" {
        # block 1
        var.hostname         = "www.example.org"
        server.document-root = "/var/www/servers/www.example.org/pages/"
        access.logfile       = "/var/www/servers/www.example.org/logs/accesslog"

    } # end of $HTTP["host"] == "www.example.org"

    $HTTP["host"] == "www2.example.org" {
        # block 2
        var.hostname         = "www2.example.org"
        server.document-root = "/var/www/servers/www2.example.org/pages/"
        access.logfile       = "/var/www/servers/www2.example.org/logs/accesslog"

    } # end of $HTTP["host"] == "www2.example.org"
}

Nice, isn’t it ? :)