lighttpd 作为反向代理

发布于 2024-10-15 20:21:16 字数 1222 浏览 5 评论 0原文

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),则一切正常。

如果请求在端口 80 上传入,DeviceB、DeviceC 上的 Web 服务器应该以 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() 重写/重定向 URL?或者,如何定义什么应该被代理(编辑)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻梦 2024-10-22 20:21:16

lighttpd 开发人员多年来都知道您的需求。

根据版本的不同,可以通过解决方法或新功能来解决这个问题。

Lighttpd 1.4

bugtracker 中解释了解决方法:bug #164

$HTTP["url"] =~ "(^/DeviceB/)" {   
  proxy.server = ( "" => ("" => ( "主机" => "127.0.0.1", "端口" => 81 ))) 
}

$SERVER["套接字"] == ":81" {   
  url.rewrite-once = ( "^/DeviceB/(.*)$" => "/$1" )   
  proxy.server = ( "" => ( "" => ( "主机" => "192.168.1.20", "端口" => 80 ))) 
}

Lighttpd 1.5

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

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

$HTTP["url"] =~ "^/DeviceB" {
  代理公司...

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

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" ),
  )
}
阳光的暖冬 2024-10-22 20:21:16

所需包

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

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

走过海棠暮 2024-10-22 20:21:16

使用 proxy.headerLighttpd 1.4 的另一个非解决方案

(从 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文档

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文