使用 Apache 反向代理为各种服务器提供 1 个公共 IP

使用 Apache 反向代理为各种服务器提供 1 个公共 IP

我的服务器使用 AWS。

我已经创建了几个在本地运行不同 http 网站的实例。(实例 1、实例 2、....)

我已经在我的 Route 53 上注册了一个域名example.com并创建了一个托管区域。example.com

我创建了一个运行 Apache httpd 的实例,我将其用作反向代理。我已将 AWS 的公共弹性 IP 与此服务器关联。此服务器与网站服务器位于同一网络中。之后,我在路由 53 中创建了一个记录表,proxy.example.com指向我的代理服务器的公共 IP,并创建了另一个记录表,instance1.example.com指向其中一个网站托管服务器的私有地址。

然后在代理服务器上的 httpd 配置文件中我添加了以下代码:

ProxyPass "/"  "http://instance1.example.com/"
ProxyPassReverse "/"  "http://instance1.example.com/"

如果我现在proxy.example.com在任何浏览器中输入内容,我就会被重定向到在 instance1 上运行的网站,这非常简洁。

最后,我现在想根据我输入的子域名,通过该代理服务器重定向到具有不同网站的不同服务器:

如果我输入instance1.example.com,流量应该转到我的代理,然后代理会看到子域并将所述流量重定向到我的实例1。

如果我输入instance2.example.com,流量应该转到我的代理,然后代理会看到子域并将所述流量重定向到我的实例2。

等等...

这真的可能吗?我感觉自己离实现目标已经很近了……但由于我对整个 Apache 还很陌生,所以我需要你的帮助,因为我在网上找不到类似这种情况。提前感谢大家的回答!

解决方案

我找到了答案,并想如果有人遇到同样的问题,我会在这里留下我的解决方案。

我向托管区域 example.com 添加了 3 个新记录集,全部指向我的代理的公共地址:

test.example.com
test2.example.com
test3.example.com

之后,我将以下代码行添加到我的代理服务器上的 httpd 配置文件中:

<VirtualHost *:80>
ServerName test.example.com
ProxyPass "/" "http://private IP of webserver1/"
ProxyPassReverse "/" "http://private IP of webserver1/"
</VirtualHost>

<VirtualHost *:80>
ServerName test2.example.com
ProxyPass "/" "http://private IP of webserver2/"
ProxyPassReverse "/" "http://private IP of webserver2/"
</VirtualHost>

<VirtualHost *:80>
ServerName test3.example.com
ProxyPass "/" "http://private IP of webserver3/"
ProxyPassReverse "/" "http://private IP of webserver3/"
</VirtualHost>

就是这样!

相关内容