Robert Eisele
Systems Engineer, Architect and DBA

Add HTTP Response Headers in lighttpd

You can send additional HTTP Response headers with lighttpd by adding the following line to your configuration, possibly encapsulated into conditions:

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

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!"
                       )
                     )
                   )
                 )

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

You might also be interested in the following

 

Sorry, comments are closed for this article. Contact me if you have an inventive contribution.