Search code examples
httpsreverse-proxylighttpdmod-proxy

Lighttpd reverse proxy HTTPS to another server on HTTP


I've a Lighttpd server running on HTTPS, and I want to have one subdirectory on the server act as a reverse proxy for a separate server that runs on HTTP. I've tried following guides on doing both proxy and url rewrite, but something to do with how the SSL is set up is interfering.

$SERVER["socket"] == ":81" {
    url.rewrite-once = ( "^/directory/(.*)$" => "/index.html" )
    proxy.server  = ( "" => ( "" => ( "host" => "192.0.0.1", "port" => 123 )))
}

$HTTP["scheme"] == "http" {
     $HTTP["host"] =~ ".*" {
        url.redirect = (".*" => "https://%0$0")
     }
}

$SERVER["socket"] == ":443" {
        ssl.engine = "enable"
        ssl.ca-file = "/etc/lighttpd/fullchain.pem"
        ssl.pemfile = "/etc/lighttpd/server.pem"
        $HTTP["url"] =~ "^/directory/" {
               proxy.server = ( "" => ( "" => ( "host" => "127.0.0.1", "port" => 81)))
        }
}

My intention was that going to /directory/ would redirect you to the 192.0.0.1:123/index.html. I followed this guide which mentioned doing the first redirect to port 81, then redirecting port 81 to the second server.

This doesn't seem to work and just gets stuck in a redirection loop, and always returns a 301 to the https site.

If I don't do the :81 redirect, I can get the bottom proxy.server to redirect to the right place, but it keeps the /directory/ ending which doesn't get to where I need it.

Thanks.


Solution

  • Since lighttpd 1.4.46, mod_proxy can rewrite url-prefixes.

    $HTTP["scheme"] == "http" {
         $HTTP["host"] =~ ".*" {
            url.redirect = (".*" => "https://%0$0")
         }
    }
    
    $SERVER["socket"] == ":443" {
            ssl.engine = "enable"
            ssl.ca-file = "/etc/lighttpd/fullchain.pem"
            ssl.pemfile = "/etc/lighttpd/server.pem"
            $HTTP["url"] =~ "^/directory/" {
                   url.rewrite-once = ( "^/directory/(.*)$" => "/directory/index.html" )
                   proxy.header = ( "map-urlpath" => ("/directory/" => "/") )
                   proxy.server = ( "" => ( "" => ( "host" => "192.0.0.1", "port" => 123)))
            }
    }