轻量级Web服务器lighttpd作为反向代理

21

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)的测试服务,则一切正常。

我说设备 B、C 上的 Web 服务器应该在端口 80 收到请求时响应 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 设备B @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" )

更新

我需要在proxy.server()周围添加$HTTP["host"] == ...来重写/重定向URL吗?或者,如何定义需要被代理的内容。


5
应该发布在 Server Fault 上,而不是 Stack Overflow。 - Daniel DiPaolo
这里的问题是什么?我很乐意帮助解决Lighttpd的问题,但是我看到很多设置,却没有真正的问题(至少根据设置来看没有)...? - ircmaxell
问题是如何设置proxy.server()以将192.168.1.10/DeviceB的请求转发到192.168.1.20/index.html。 - impf
2个回答

19

几年来,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

他们使用以下命令添加了此功能(官方文档):

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

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

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接