lighttpd as reverse-proxy
Asked Answered
C

3

21

DeviceA serves as a reverse-proxy and is supposed to forward requests as follows:

192.168.1.10/DeviceB ==> 192.168.1.20/index.html

192.168.1.10/DeviceC ==> 192.168.1.30/index.html

Both index files are located under /var/www and are static "Hello world!" pages. The problem is that I can't access those files through DeviceA, but if I call a test service also running on DeviceC (listening on port 12345) everything works fine.

Am I wrong saying that the web server on DeviceB, DeviceC should respond with index.html if a request comes in on port 80 ???

lighttpd.conf DeviceA @192.168.1.10 server.modules = ( "mod_proxy" )

proxy.server = ( 
"/DeviceB" => ( "" => ( "host" => "192.168.1.20", "port" => 80 )),
"/DeviceC" => ( "" => ( "host" => "192.168.1.30", "port" => 80 )),  
"/TestService" => ( "" => ( "host" => "192.168.1.30", "port" => 12345 ))
)

lighttpd.conf DeviceB @192.168.1.20

server.document-root = "/var/www"
server.port = 80
index-file.names = ( "index.html" )

lighttpd.conf DeviceC @192.168.1.30

server.document-root = "/var/www"
server.port = 80
index-file.names = ( "index.html" )

Update

Do I need $HTTP["host"] == ... around proxy.server() to rewrite/redirect URLs? Or, how to define what shall be proxy(ed)

Conspire answered 1/2, 2011 at 7:20 Comment(3)
Should be on serverfault, not SOPolly
What's the question here? I've got no problem helping with Lighttpd, but I see a lot of setup and no real question (at least that makes sense given the setup)...?Melodee
The question was how to setup proxy.server() in order to forward 192.168.1.10/DeviceB requests to 192.168.1.20/index.htmlConspire
V
19

Your need is known by lighttpd developers from several years.

It is answered by a workaround or new feature depending on the version.

Lighttpd 1.4

A workaround is explained in the bugtracker : bug #164

$HTTP["url"] =~ "(^/DeviceB/)" {   
  proxy.server  = ( "" => ("" => ( "host" => "127.0.0.1", "port" => 81 ))) 
}

$SERVER["socket"] == ":81" {   
  url.rewrite-once = ( "^/DeviceB/(.*)$" => "/$1" )   
  proxy.server  = ( "" => ( "" => ( "host" => "192.168.1.20", "port" => 80 ))) 
}

Lighttpd 1.5

They added this feature with this command (official documentation) :

proxy-core.rewrite-request : rewrite request headers or request uri.

$HTTP["url"] =~ "^/DeviceB" {
  proxy-co...

  proxy-core.rewrite-request = (
    "_uri" => ( "^/DeviceB/?(.*)" => "/$1" ),
    "Host" => ( ".*" => "192.168.1.20" ),
  )
}
Vacationist answered 19/10, 2013 at 13:46 Comment(0)
M
6

Required package

server.modules  =  (
...
   "mod_proxy",
...
)

Your frontend proxy setting : for lighttpd.conf @192.168.1.10

$HTTP["url"] =~ "^.*DeviceB" {
    proxy.server  = ( "" => 
        (( "host" => "192.168.1.20", "port" => 80 ))
    )
}

$HTTP["url"] =~ "^.*DeviceC" {
    proxy.server  = ( "" => 
        (( "host" => "192.168.1.30", "port" => 80 ))
    )
}

For the full documentation of lighttpd mod_proxy, you can refer to http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModProxy

Malefic answered 27/7, 2012 at 10:42 Comment(0)
L
0

Another, non-workaround solution for Lighttpd 1.4

Using proxy.header (available since version 1.4.46, tested using version 1.4.53):

$HTTP["url"] =~ "(^/DeviceB/)" {   
  proxy.server = ( "" => ("" => ( "host" => "192.168.1.20", "port" => 80 )))
  proxy.header = (
    "map-urlpath" => ( "/DeviceB" => "" )
  )
}

Unfortunately, map-urlpath is only able to replace the prefix of the URL, but that covers most cases including this one. For details refer to the documentation of mod_proxy.

Larina answered 4/1 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.