Apache + Tomcat:使用 mod_proxy 而不是 AJP

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

Apache + Tomcat: Using mod_proxy instead of AJP

apachetomcatmod-proxyajp

提问by Marcus Downing

Is there any way I connect Apache to Tomcat using an HTTP proxy such that Tomcat gets the correct incoming host name rather than localhost? I'm using this directive in apache:

有什么方法可以使用 HTTP 代理将 Apache 连接到 Tomcat,以便 Tomcat 获得正确的传入主机名而不是 localhost?我在 apache 中使用这个指令:

ProxyPass /path http://localhost:8080/path

But it comes through as localhost, which is useless when we have a bunch of sites on the same server. I could set the host manually in the server config:

但是它作为本地主机出现,当我们在同一台服务器上有一堆站点时,这是无用的。我可以在服务器配置中手动设置主机:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           proxyName="pretend.host" proxyPort="80" />

But that again doesn't serve more than one site. And I don't like the idea of using a different internal port for each site, that sounds really ugly.

但这同样不会为多个站点提供服务。而且我不喜欢为每个站点使用不同的内部端口的想法,这听起来非常难看。

Is there no way to transfer the port when I proxy it?

代理的时候有没有办法转移端口?

(If you ask why I don't just use AJP, the answer is this error. I'm trying everything I can before giving up on Tomcat and Apache entirely)

(如果你问我为什么不只使用 AJP,答案就是这个错误。在完全放弃Tomcat 和 Apache之前,我正在尽我所能)

回答by Robert Munteanu

The settings you are looking for are:

您正在寻找的设置是:

<VirtualHost *:80>
  ServerName public.server.name

  ProxyRequests Off
  ProxyPreserveHost On

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Note that we're using localhost as the proxy target. We can do this since we enable ProxyPreserveHost. The documentation states that

请注意,我们使用 localhost 作为代理目标。我们可以这样做,因为我们启用了ProxyPreserveHost。该文件指出

It is mostly useful in special configurations like proxied mass name-based virtual hosting, where the original Host header needs to be evaluated by the backend server.

它在特殊配置中最有用,例如基于代理的大众名称虚拟主机,其中原始主机标头需要由后端服务器评估。

which sounds exactly like what you are doing.

这听起来就像你在做什么。

回答by gareth_bowles

I think your best bet if you want multiple sites on the same server is to use virtual hosts in your Apache configuration. Here's an example:

如果您希望在同一台服务器上有多个站点,我认为最好的办法是在 Apache 配置中使用虚拟主机。下面是一个例子:

<VirtualHost *:80>
ServerName server.domain.com

ProxyRequests Off
<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>

ProxyPass / http://server.domain.com:8080/
ProxyPassReverse / http://server.domain.com:8080/
<Location />
    Order allow,deny
    Allow from all
</Location>

As long as you have server.domain.com registered in your external DNS, the incoming host name will be displayed in client URLs. I'm running a single server hosting 6 separate sites, including 3 that are back by Tomcat, using this method.

只要您在外部 DNS 中注册了 server.domain.com,传入的主机名就会显示在客户端 URL 中。我正在使用这种方法运行托管 6 个独立站点的单个服务器,其中包括 3 个由 Tomcat 支持的站点。

回答by John Mikic

You can still use AJP, and you should since it's faster than HTTP. Just make sure to enable it in http.conf:

您仍然可以使用 AJP,而且您应该使用它,因为它比 HTTP 快。只需确保在 http.conf 中启用它:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

In that case, this configuration works for me:

在这种情况下,此配置对我有用:

<VirtualHost *:80>
  ServerName public.server.name

  ProxyRequests Off
  ProxyPreserveHost On

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass / ajp://localhost:8080/
# ProxyPassReverse might not be needed,
# it's only for redirecting from inside.
#  ProxyPassReverse / ajp://localhost:8080/
</VirtualHost>