簡體   English   中英

如何使用 apache httpd 作為其他 web 服務器的前端?

[英]How to use apache httpd as front for other web servers?

我想要做什么

I want to run Apache httpd (2.4.x) as a front-end for an Apache tomcat (9.0.x) and an Elasticsearch server.

例如,用戶應該看到

http://localhost/tomcat/

內部轉發到

http://localhost:8080/

所以例如

http://localhost/tomcat/manager/status (外部由httpd服務)

檢索自

http://localhost:8080/manager/status (內部來自Tomcat)

我做了什么

mod_proxy、mod_proxy_http、mod_rewrite 被激活。

在 httpd.conf 我添加

ProxyPass /tomcat http://localhost:8080
ProxyPassReverse /tomcat http://localhost:8080

或者

<Location "/tomcat">
    ProxyPass http://localhost:8080/
    ProxyPassReverse http://localhost:8080/
</Location>

什么不工作

http://localhost/tomcat/正確提供預期頁面。 但是,該頁面上的鏈接未調整為新的基礎 URL。

例如,頁面包含<a href="/docs/">Documentation</a>

單擊該鏈接轉到 go 到http://localhost/tomcat/docs但指向http://localhost/docs

請注意,如果可能的話,我想在 Apache httpd 端(而不是重新配置 Tomcat)上使用解決方案,因為稍后我想對 Elasticsearch 后端使用相同的機制。

另一個小問題

When omitting the trailing slash http://localhost/tomcat the forwarding works for the HTML page itself, but fails for the embedded resources, eg, the Tomcat logo should be retrieved as http://localhost/tomcat/tomcat.svg -> http://localhost:8080/tomcat.svg but instead points to http://localhost/tomcat.svg which is not translated, since it no longer matches the Location pattern.

但是,該頁面上的鏈接未調整為新的基礎 URL。

這是您配置中的主要問題: web 應用程序通常生成絕對 URI 路徑,例如/manager/images/tomcat.svg而不是相對 URI 路徑,例如images/tomcat.svg 它們使用HttpServletRequest#getContextPath檢索部署它們的路徑 ( /manager )。 他們無法知道他們正在被另一條路徑代理。

這就是為什么 web 應用程序的原始和代理 URL 應該保持相同的 URI 路徑的原因。 在您的情況下,您應該在/tomcat/manager部署 Tomcat 管理器。 這可以通過多種方式完成:

  • 如果您的 Tomcat 管理器應用程序位於<Host>應用程序庫( $CATALINA_BASE/webapps ) 中,請將目錄重命名為tomcat#manager (這通常發生在 Windows 上),
  • if your Tomcat's Manager application is outside of the <Host> 's application base , rename the context file (in $CATALINA_BASE/conf/Catalina/<host_name> ) from manager.xml to tomcat#manager.xml (this happens in Linux distributions像Debian),
  • 如果您在server.xml中定義應用程序上下文(不推薦),您可以使用<Context>元素的path屬性。

數字符號#將轉換為/

在 Apache 方面,將ProxyPass指令更改為:

ProxyPass /tomcat http://localhost:8080/tomcat

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM