lighttpd作为反向代理

时间:2011-02-01 07:20:59

标签: lighttpd reverse-proxy mod-proxy

DeviceA充当反向代理,应该按如下方式转发请求:

192.168.1.10/DeviceB ==> 192.168.1.20/index.html

192.168.1.10/DeviceC ==> 192.168.1.30/index.html

两个索引文件都位于/ var / www下,并且是静态的“Hello world!”页面。问题是我无法通过DeviceA访问这些文件,但如果我调用也在DeviceC上运行的测试服务(侦听端口12345),一切正常。

我说错误的说DeviceB上的Web服务器,如果端口80上有请求,DeviceC应该用index.html响应???

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

更新

我需要$ HTTP [“host”] == ...围绕proxy.server()来重写/重定向网址吗?或者,如何定义什么是代理(ed)

2 个答案:

答案 0 :(得分:13)

几年来,lighttpd开发人员已经了解您的需求。

可以通过变通方法或新功能来解决,具体取决于版本。

Lighttpd 1.4

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

他们使用此命令(official documentation)添加了此功能:

  

proxy-core.rewrite-request :重写请求标头或请求uri。

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

  proxy-core.rewrite-request = (
    "_uri" => ( "^/DeviceB/?(.*)" => "/$1" ),
    "Host" => ( ".*" => "192.168.1.20" ),
  )
}

答案 1 :(得分:6)

必填包

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

您的前端代理设置:适用于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 ))
    )
}

有关lighttpd mod_proxy的完整文档,您可以参考http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModProxy