lighttpd tips

— richardwb on Monday, February 09, 2009 @ 16:32

lighttpd is a web server that is optimized for speed. It is particularly adept at serving static content. Since most web hosts provide Apache and most web applications assume you are running on Apache (which is why web hosts provide Apache and why web applications assume you use Apache and why web hosts provide Apache and ...) it can take a bit more effort to find lighttpd documentation.

Some little things that I have picked up for lighttpd:

lighttpd version:

> lighttpd -v

lighttpd version and supported features: (note: that is a capital V)

> lighttpd -V

Using Apache to forward requests to lighttpd? Lighttpd will see that all incoming requests have the IP of the Apache proxy. There is a module mod_extforward which will extract a real IP from the X-Forwarded-For header. Add these lines in the appropriate locations to your lighttpd.conf file:

server.modules = (
    ...                      # (other modules)
    "mod_extforward",        # change x-forwarded-for IPs into real IPs, 
                             # load after mod_accesslog
extforward.forwarder = (
    "" => "trust"   # where is the IP address of the proxy

You can use variables in lighttpd.conf:

server.username = "myusername"                  # lighttpd configuration option
var.basedir = "/users/home/" + server.username  # declares a variable called <em>basedir</em>
server.document-root = basedir + "/www/"        # equal to <em>/users/home/myusername/www/</em>

Set a different server.document-root depending on the host:

$HTTP["host"] == "" {
    server.document-root = basedir + "/www/"  # basedir is our variable

Redirect people to a single canonical web address (in this example will redirect to

# make sure to include the mod_redirect module
$HTTP["host"] == "" {                 # if host matches "" then...
    url.redirect = (                             #   if the URL matches ^/(.*)$
        "^/(.*)$" => "$1" #   redirect to$1
    )                                            #   where $1 is what was captured by the
}                                                #   the parenthesis (.*)

^/(.*)$ is an example of a regular expression. Specifically, lighttpd uses PCRE. Instead of equality, ==, you can also test for inequality with !=. If you want to use regular expressions in the match conditional, you can use =~ and !~ for equality and inequality respectively (this is useful for paths):

# make sure to include the mod_access module
$HTTP["host"] == "" {
    server.document-root = basedir + "/www/"

    $HTTP["url"] =~ "^/private/" {  # if url starts with /private then...
        url.access-deny = ("")      #   deny access to URLs that end with ""
    }                               #   (i.e. deny access to all URLs)

If you use the regexp conditionals remember that many symbols have a special meaning. The '.' (period) is a common example, as it has the special meaning of "match any single character". You can escape it by prepending a backslash: '\.'

comments powered by Disqus