首页 > 解决方案 > 在tomcat html输出中重写URL

问题描述

我正在尝试使用与后端 tomcat 应用程序不同的 URL 配置外部 Apache 前端。我以tomcat的管理器应用为例。

我希望用户像外部一样访问我的页面: https ://myhost.com/tomcat-manager

在内部,它被重定向到 http://localhost:8080/manager

我在 apache2 中使用 mod_proxy 和 mod_rewrite 尝试了以下配置:

  RewriteEngine on
  SSLProxyEngine on

  RewriteRule "/manager/(.*)$" https://myhost.com/tomcat-manager/$1 [P]
  ProxyPass /tomcat-manager http://localhost:8080/manager
  ProxyPassReverse /tomcat-manager http://localhost:8080/manager

它主要工作,但我不喜欢 tomcat manager webapp 将其链接输出为“/manager”而不是“/tomcat-manager”,这迫使我添加上面的 mod_rewrite 规则。理想情况下,我希望最终用户只看到该模式的浏览器 URL:

https://myhost.com/tomcat-manager/(随便)

并且永远不会:

https://myhost.com/manager/(随便)

在不修改底层 webapp 的情况下配置 Apache 有什么建议吗?谢谢!

标签: javaapachetomcaturl-rewritingreverse-proxy

解决方案


我找到了我的问题的答案。它是 Apache 的 mod_proxy_html。我启用了相应的模块并修复了 Ubuntu 的 mod_proxy_html 缺失配置,如下所述:https ://serverfault.com/questions/684905/proxyhtmlurlmap-not-working-in-apache2-4

然后,我添加了以下配置代码:

ProxyHTMLEnable On
ProxyHTMLURLMap http://localhost:8080/manager/ /tomcat-manager/
ProxyHTMLURLMap /manager/ /tomcat-manager/
SetOutputFilter proxy-html
RequestHeader      unset  Accept-Encoding

就是这样。现在,我可以访问http://myhost.com/tomcat-manager并且其中的 html 代码也被重写了!


推荐阅读