将请求转发给KVM主机上的多个guest虚拟机之一?

我有一个3人的Ubuntu主机。 他们都有不同的web服务运行在端口80.我怎么能告诉主机转发请求到适当的客人根据主机名?

主持人:example.com

嘉宾:git.example.com,www.example.com,psql.example.com

由于您需要根据主机名而不是端口来进行路由,因此将stream量NAT到虚拟机的iptables解决scheme已经不存在了。

剩下的就是以反向代理模式运行一个Web服务器,它根据请求的头部读取请求主机头和代理到不同的私有IP。

确切的configuration将取决于您使用哪个Web服务器,以及您select哪个Web服务器取决于您需要的function(SSL?)以及个人偏好。 让我知道你更喜欢哪个Web服务器,如果需要,我可以编辑答案以包含示例configuration。

编辑:基本的nginxconfiguration:

http { # ...existing config basics... server_name, NOT servername server { listen 80; server_name git.example.com; location / { # git server IP below: proxy_pass http://10.xxx:80/; # re-send the host header - this may not be necessary proxy_set_header Host $host; # set the X-Forwarded-For header, so that the public IP of the client is available to the backend server proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name psql.example.com; location / { # psql server IP below: proxy_pass http://10.xxx:80/; # re-send the host header - this may not be necessary proxy_set_header Host $host; # set the X-Forwarded-For header, so that the public IP of the client is available to the backend server proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name www.example.com; location / { # www server IP below: proxy_pass http://10.xxx:80/; # re-send the host header - this may not be necessary proxy_set_header Host $host; # set the X-Forwarded-For header, so that the public IP of the client is available to the backend server proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } } 

您必须安装在可访问互联网,Web服务器的主机上,并使用“基于名称的虚拟主机configuration”(尝试在您的web服务器的名称中使用google)。 每个基于名称的虚拟机都必须是一个虚拟服务器的代理。 你可以使用apache,nginx,lighttpd,只要你select…

桥接所有的连接。 这样你的客人可以在与主机相同的networking上获得IP。 现在在DNS中为这三个客人添加“A”logging。

请记住,如果请求是为了git.example.com,它将永远不会来到example.com,除非你有一个“CNAME”设置为相同的。

[编辑根据评论]
既然你不能使用DNS,我build议不要运行虚拟机。 而是创build一些虚拟主机并使用它们。 在你的nginx中只定义了多个server_name。 查看nginx wiki的例子。