Robert Eisele on twitter

Add HTTP Response Headers in lighttpd

July 24th, 2008

You can send additional HTTP Response headers with lighty by add

setenv.add-response-header = ( "X-Secret-Message" => "42" )

in your configuration file - maybe encapsulated with conditions. But there is one problem. You have no possibility to send an additional header if there was an error such as 404 because lighty sends the error as fast as possible.

If there is PHP on the box this task is really easy. Set the error-handler-404 to a PHP file and send the desired header with header():

<?php

header("X-Sendfile: /var/www/404.html");
header("X-Extra: Yes!");

?>

If there is no PHP installed, you could install it. But this overhead only for an additional header?!

Whats about LUA behind mod_magnet? This is a good solution, but you don't have LUA on every box. But with a few lines C, this problem is solved very quickly.

You need the following lighttpd configuration and a little binary:

server.error-handler-404   = "/handler.404"

fastcgi.server = ( ".404" =>
                   ( "localhost" =>
                     (
                       "socket" => "/tmp/404-fcgi.socket",
                       "bin-path" => "/usr/local/handler/404-handler",
                       "check-local" => "disable",
                       "allow-x-send-file" => "enable",
                       "bin-environment" => (
                          "SENDFILE" => "/var/www/404.html",
                          "HEADER" => "X-Extra: Yes!"
                       )
                     )
                   )
                 )

Download the 404 handler source

It's also possible to override the http-status code by set the HEADER env to "Status: 503 Service not available" for example.

Leave a Reply