使用多个域将 Apache 链接到 Tomcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1765399/
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 18:24:38  来源:igfitidea点击:

Linking Apache to Tomcat with multiple domains

javaapachetomcatmod-jkmultiple-domains

提问by Royce Thigpen

Okay, so I've been working for a while on this, and have been searching, but so far I have not found any answers that actually answer what I want to know. I'm a little bit at the end of my rope with this one, but I'm hoping I can figure this out sometime soon.

好的,所以我已经为此工作了一段时间,并且一直在搜索,但到目前为止,我还没有找到任何真正回答我想知道的答案的答案。我对这个有点束手无策,但我希望我能尽快解决这个问题。

So I have Apache 2 installed and serving up standard webpages, but I also have that linked to a Tomcat instance for one of my domains currently supported. However, I want to add another domain to the server via Apache that points to a separate code base from the one I already have. I have been coming at this from several different angles, and I have determined that I just don't know enough about setting up these servers to really do what I want to do.

因此,我安装了 Apache 2 并提供标准网页,但我也将其链接到当前支持的域之一的 Tomcat 实例。但是,我想通过 Apache 向服务器添加另一个域,该域指向与我已有的代码库不同的代码库。我从几个不同的角度来解决这个问题,我已经确定我对设置这些服务器的了解还不够多,无法真正做我想做的事情。

Little information on my server: Currently running a single Tomcat5.5 instance with Apache 2, using mod_jk to connect them together.

我的服务器上的信息很少:当前使用 Apache 2 运行单个 Tomcat5.5 实例,使用 mod_jk 将它们连接在一起。

I have a worker in workers.properties that points it's "host" field to "localhost" with the correct port my Tomcat instance, so that all works.

我在 workers.properties 中有一个工作人员,它使用我的 Tomcat 实例的正确端口将它的“主机”字段指向“本地主机”,以便一切正常。

In my Tomcat server.xml file, I have a host defined as "localhost" that points at my webapp that I am currently serving up, with that host set as the defaultHost as well.

在我的 Tomcat server.xml 文件中,我有一个定义为“localhost”的主机,它指向我当前提供的 web 应用程序,该主机也设置为 defaultHost。

One thought I had was that I could add a new worker with a different host than "localhost" (i.e. host2) and then define a new host in my server.xml file called "host2" to match it, but after reading around some on the internet, It seems the "host" of the worker must point to a server, and not a hostname in the Tomcat instance, is this correct?

我的一个想法是,我可以添加一个与“localhost”(即 host2)不同的主机的新工作者,然后在我的 server.xml 文件中定义一个名为“host2”的新主机来匹配它,但在阅读了一些关于互联网上,似乎工作人员的“主机”必须指向服务器,而不是 Tomcat 实例中的主机名,这是正确的吗?

Again, a simple rundown of what I want: Setup in apache/tomcat combo such that www.domain1.com points at "webapp1" and www.domain2.com points at "webapp2".

再次,我想要的简单概述:在 apache/tomcat 组合中设置,以便 www.domain1.com 指向“webapp1”,www.domain2.com 指向“webapp2”。

回答by Pascal Thivent

First, setup mod_jk workers for both webapps. Below a sample workers.properties:

首先,为两个 web 应用程序设置 mod_jk 工作人员。下面是一个示例workers.properties

workers.tomcat_home=/usr/local/tomcat/apache-tomcat-6.0.20
workers.java_home=/usr/lib/jvm/java-6-sun
ps=/
worker.list=worker1,worker2
worker.worker1.type=ajp13
worker.worker1.host=www.domain1.com
worker.worker1.port=8009
worker.worker2.type=ajp13
worker.worker2.host=www.domain2.com
worker.worker2.port=8009

Then, set up virtual hosts on apache:

然后,在 apache 上设置虚拟主机:

<VirtualHost *:80>
   ServerName www.domain1.com
   JkMount /* worker1
</VirtualHost>

<VirtualHost *:80>
   ServerName www.domain2.com
   JkMount /* worker2
</VirtualHost>

Make sure the server.xmlcontains an uncommented AJP Connector for port 8009 (matching the workers port). Like this :

确保server.xml8009 端口包含一个未注释的 AJP 连接器(与工作端口匹配)。像这样 :

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Finally, configure the tomcat hosts. Something like this:

最后,配置tomcat主机。像这样的东西:

<Host name="www.domain1.com"
   appBase="/path/to/domain1"
   unpackWARs="true"
   autoDeploy="true"
   xmlValidation="false"
   xmlNamespaceAware="false">

<Host name="www.domain2.com"
   appBase="/path/to/domain2"
   unpackWARs="true"
   autoDeploy="true"
   xmlValidation="false"
   xmlNamespaceAware="false">

You might need to make some adaptation but it should be close to the final result.

您可能需要进行一些调整,但它应该接近最终结果。

回答by Konrad Garus

You could also use much simpler approach with mod_proxy. Have a look at http://squirrel.pl/blog/2010/03/30/mapping-tomcat-apps-to-subdomains-with-apache/

您还可以使用更简单的方法与mod_proxy. 看看http://squirrel.pl/blog/2010/03/30/mapping-tomcat-apps-to-subdomains-with-apache/