使用 Apache 将端点转发到同一主机上的不同端口

Using Apache to forward endpoints to different ports on the same host

我正在尝试配置 Apache 以不同方式处理应用程序的某些端点。

我需要 /api、/appname 和 /admin 转发到同一主机上的单独 Tomcat 个实例,显然在不同的端口上运行。

A​​pache 正在侦听 443 (SSL)。

查看 Apache 的文档,我看到了 mod_rewrite 用于类似任务的示例,但不适用于此实际用例。

只有端口 80 和 443(80 被重定向到 443)对外开放,所以其他端口只对内部开放...我们称它们为 2001、3001 和 4001。

编辑:附录:我正在使用 apache 2.2,目前没有更新到 2 的计划。4.x。

首先,您需要确保 mod_proxy 已启用。然后,假设您希望每个 /api//appname//admin/ 对最终用户来说看起来像一个子目录,并且他们正在监听 20013001, 和 4001,我想你要找的是这样的:

(在服务器或虚拟主机配置中)

RewriteRule ^/api/(.*)     http://localhost:2001/ [P,L,QSA]
ProxyPassReverse /api/     http://localhost:2001/

RewriteRule ^/appname/(.*) http://localhost:3001/ [P,L,QSA]
ProxyPassReverse /appname/ http://localhost:3001/

RewriteRule ^/admin/(.*)   http://localhost:4001/ [P,L,QSA]
ProxyPassReverse /admin/   http://localhost:4001/

(在 .htaccess 配置中...没有前导斜线...也没有 mod_proxy

RewriteRule ^api/(.*)      http://localhost:2001/otherpath/ [P,L,QSA]
RewriteRule ^appname/(.*)  http://localhost:3001/otherpath/ [P,L,QSA]
RewriteRule ^admin/(.*)    http://localhost:4001/otherpath/ [P,L,QSA]

</code> 表示 parens 捕获的内容。 <code>P 告诉 mod_rewrite 使用 mod_proxy 来处理这个重写。 L 表示将此作为最后一条规则;应用后停止重写。 QSA 将入站请求中的查询字符串附加到重写的请求中。

ProxyPassReverse 用于捕获来自内部实例的任何重定向并重写它以匹配外部 URI。注意:mod_proxy 需要在服务器配置(服务器、虚拟主机、目录)中并且在 .htaccess

无效

一些有用的参考资料:

现在,综上所述,您可能无论如何都不想使用 mod_rewrite 来执行此操作。确实是 mod_proxy 为您完成了所有工作。如果您没有任何实际的重写,您应该直接使用 mod_proxy。即使 mod_rewrite 也只是把它交给了 mod_proxy

来自 http://httpd.apache.org/docs/2.2/rewrite/avoid.html:

RewriteRule provides the [P] flag to pass rewritten URIs through mod_proxy.

RewriteRule ^/?images(.*) http://imageserver.local/images [P]

However, in many cases, when there is no actual pattern matching needed, as in the example shown above, the ProxyPass directive is a better choice. The example here could be rendered as:

ProxyPass /images/ http://imageserver.local/images/ 

Note that whether you use RewriteRule or ProxyPass, you'll still need to use the ProxyPassReverse directive to catch redirects issued from the back-end server:

ProxyPassReverse /images/ http://imageserver.local/images/ 

You may need to use RewriteRule instead when there are other RewriteRules in effect in the same scope, as a RewriteRule will usually take effect before a ProxyPass, and so may preempt what you're trying to accomplish.