Search code examples
lighttpd

Configure lighttpd to different hosts for different client IPs


I have a server running lighttp. For testing purposes I would like to use a different host for a request if it originates from a specific IP. For example:

$HTTP["host"] =~ "(^|\.)example\.com$" {
    #### for live system
    server.document-root = "/var/www/example.com/http"

    #### for requests from ip xxx
    server.document-root = "/var/www/example.com/testing/http"
    …
}

Is this achievable with the use of config modifications?


Solution

  • Use $HTTP["remoteip"] for this check:

    $HTTP["remoteip"] match on the remote IP or a remote Network (Warning: doesn't work with IPv6 enabled)

    Then your config file:

    $HTTP["host"] =~ "(^|\.)example\.com$" {
        #### for live system
        $HTTP["remoteip"] == "10.10.10.10" {
            server.document-root = "/var/www/example.com/http"
        }
    
        #### for requests from ip xxx
        $HTTP["remoteip"] == "11.11.11.11" {
            server.document-root = "/var/www/example.com/testing/http"
        }
    }
    

    You also can use an else:

    $HTTP["host"] =~ "(^|\.)example\.com$" {
        #### for requests from ip xxx
        $HTTP["remoteip"] == "11.11.11.11" {
            server.document-root = "/var/www/example.com/testing/http"
        }
        #### for live system
        else {
            server.document-root = "/var/www/example.com/http"
        }
    }