lighttpd 作为反向代理

问题描述 投票:0回答:3

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

192.168.1.10/设备B ==> 192.168.1.20/index.html

192.168.1.10/DeviceC==>192.168.1.30/index.html

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

如果请求通过端口 80 传入,DeviceB、DeviceC 上的 Web 服务器应该以 index.html 进行响应,我这样说是不是错了???

lighttpd.conf 设备A @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() 来重写/重定向 URL 吗?或者,如何定义什么应该被代理(ed)

lighttpd reverse-proxy mod-proxy
3个回答
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


0
投票

针对 Lighttpd 1.4

的另一个非解决方案

使用

proxy.header
(自 1.4.46 版本起可用,使用 1.4.53 版本测试):

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

不幸的是,

map-urlpath
只能替换URL的前缀,但这涵盖了包括本例在内的大多数情况。 有关详细信息,请参阅mod_proxy
文档

© www.soinside.com 2019 - 2024. All rights reserved.