使用 Apache/mod_proxy 重定向 URL 路径以转发到 tomcat servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/664287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:23:29  来源:igfitidea点击:

Redirect URL path to forward to tomcat servlet using Apache/mod_proxy

apachetomcatmod-proxy

提问by Ish

I currently have a tomcat servlet 1 running under the ROOT:

我目前有一个在 ROOT 下运行的 tomcat servlet 1:

api1.myhost.com:8080/

api1.myhost.com:8080/

I'm using mod_proxy and simply forwarding all requests from api1.myhost.com to this instance. This is working as of today.

我正在使用 mod_proxy 并简单地将所有请求从 api1.myhost.com 转发到这个实例。这是今天的工作。

I now have installed a second servlet 2 which runs under the same instance of tomcat (same IP address):

我现在已经安装了第二个 servlet 2,它在同一个 tomcat 实例(相同的 IP 地址)下运行:

www.myhost.com:8080/servlet2

www.myhost.com:8080/servlet2

I want all requests to a new URL api2 to go to that second servlet such that:

我希望对新 URL api2 的所有请求都转到第二个 servlet,以便:

api2.myhost.com

api2.myhost.com

now gets forwarded to the second servlet instance.

现在被转发到第二个 servlet 实例。

I've created an A record such that api2.myhost.com points to my server IP. How do you make api2.myhost.com forward to www.myhost.com:8080/servlet2 ?

我创建了一个 A 记录,这样 api2.myhost.com 指向我的服务器 IP。你如何让 api2.myhost.com 转发到 www.myhost.com:8080/servlet2 ?

采纳答案by Maurice Perry

You need to make two VirtualHost's with on pointing to the first webapp, the other to the second.

您需要使两个 VirtualHost 的 on 指向第一个 webapp,另一个指向第二个。

<VirtualHost *:80>
    ServerName api1.myhost.com
    ProxyPass / http://api1.myhost.com:8080/
    ProxyPassReverse / http://api1.myhost.com:8080/
</VirtualHost>

<VirtualHost *:80>
        ServerName api2.myhost.com
        ProxyPass / http://www.myhost.com:8080/servlet2
        ProxyPassReverse / http://www.myhost.com:8080/servlet2
</VirtualHost>

Note that since the path will be different on tomcat than on apache, you will need to use relative URLs in your application.

请注意,由于 tomcat 上的路径与 apache 上的路径不同,因此您需要在应用程序中使用相对 URL。