阿帕奇+ Tomcat:使用 mod_proxy 代替 AJP

发布于 2024-07-22 09:29:04 字数 668 浏览 6 评论 0原文

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

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

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

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

但这又不能为多个站点提供服务。 我不喜欢为每个站点使用不同的内部端口的想法,这听起来真的很难看。

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

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

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:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

这样的小城市 2024-07-29 09:29:04

您要查找的设置是:

<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>

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

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

这听起来和你正在做的一模一样。

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

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.

酒浓于脸红 2024-07-29 09:29:04

我认为,如果您希望同一服务器上有多个站点,最好的选择是在 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>

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

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:

<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.

白云不回头 2024-07-29 09:29:04

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

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

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

<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>

You can still use AJP, and you should since it's faster than HTTP. Just make sure to enable it in 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文