将请求转发到 KVM 主机上的几个来宾之一?

Coj*_*nes 1 virtualization port-forwarding kvm-virtualization

我有一台有 3 位客人的 Ubuntu 主机。它们都在端口 80 上运行不同的 Web 服务。我如何告诉主机根据主机名将请求转发给适当的来宾?

主机:example.com

来宾:git.example.com、www.example.com、psql.example.com

Sha*_*den 7

由于您需要基于主机名而不是端口进行路由,因此 iptables 将流量 NAT 传输到 VM 的解决方案已经过时。

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

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

编辑:基本 nginx 配置:

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.x.x.x: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.x.x.x: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.x.x.x: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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)