linux – 如何使用反向代理正确处理相对URL

我在Apache中有如下的反向代理设置:

地址为www.example.com/folder的服务器A是反向代理服务器.

它映射到:服务器B,地址为test.madeupurl.com

这种作品.但我遇到的问题是,在www.example.com/folder上,所有相关链接都是www.example.com/css/examplefilename.css而不是www.example.com/folder/css/examplefilename. CSS

我该如何解决?

到目前为止,我的反向代理在服务器A(www.example.com)上有这个:

<Location /folder>
    ProxyPass  http://test.madeupurl.com
    ProxyPassReverse http://test.madeupurl.com
</Location>

解决方法

Apache ProxyPassRewrite不会重写从 http://test.example.com收到的响应主体,只会重写标题(如重定向到404页面等).

许多替代方案:

一)重写内部应用程序以使用相对路径而不是绝对路径. ie ../css/style.css而不是/css/style.css

2)将内部应用程序重新部署在同一子目录/文件夹中,而不是在test.example.com的根目录中.

三)一两个通常不太可能发生…如果你很幸运,内部应用程序只使用两个或三个子目录,而在主站点上没有使用它们,只需编写一堆ProxyPass行:

# Expose Internal App to the internet.
ProxyPass /externalpath/  http://test.example.com/
ProxyPassReverse /externalpath/  http://test.example.com/
# Internal app uses a bunch of absolute paths. 
ProxyPass /css/  http://test.example.com/css/
ProxyPassReverse /css/  http://test.example.com/css/
ProxyPass /icons/  http://test.example.com/icons/
ProxyPassReverse /icons/  http://test.example.com/icons/

四)为内部应用程序创建一个单独的子域,并简单地反向代理所有内容:

<VirtualHost *:80>
   ServerName app.example.com/
   # Expose Internal App to the internet.
   ProxyPass /  http://test.internal.example.com/
   ProxyPassReverse /  http://test.internal.example.com/
</VirtualHost>

五)有时开发人员完全无能为力,他们的应用程序不仅生成绝对URL,而且甚至在其URL中包含主机名部分,生成的HTML代码如下所示:< img src = http://test.example.com/icons /logo.png\u0026gt ;. A)您可以使用水平分割DNS和方案4的组合解决方案.内部和外部用户都使用test.example.com,但您的内部DNS直接指向test.example.com服务器的ip地址.对于外部用户,test.example.com的公共记录指向公共网络服务器www.example.com的IP地址,然后您可以使用解决方案4. B)您实际上可以获得apache,不仅可以代理对test.example.com的请求,还可以在将响应主体传输给您的用户之前重写它. (通常,代理仅重写HTTP标头/响应). apache 2.2中的mod_substitute.我没有测试它是否与mod_proxy很好地堆叠,但可能以下工作:

<Location /folder/>
  ProxyPass http://test.example.com/
  ProxyPassReverse http://test.example.com/ 
  AddOutputFilterByType SUBSTITUTE text/html
  Substitute "s|test.example.com/|www.example.com/folder/|i" 
</Location>

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...