首页 > 解决方案 > 来自不同子域的 Apache VirtualHost 重定向

问题描述

我有一个 DO droplet (Ubuntu 18.04),我想在其上托管两个站点。假设液滴的 IP 为 101.1.1.1。现在我希望从另一台服务器(具有不同的 IP,比如说 104.1.1.1.)子域指向站点。假设 siteone.example.org 和 sitetwo.example.org。所以我按照指南设置我的 Apache VirtualHost,如下所示:

<VirtualHost *:80>
    ServerAdmin webmaster@example.org
    ServerName siteone.example.org
    ServerAlias www.siteone.example.org
    DocumentRoot /var/www/siteone/public_html

    <Directory /var/www/siteone/public_html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

但是,当我在浏览器中按 siteone.example.org 时,我没有得到任何响应。我在两端都设置了 A 名称以指向 101.1.1.1。有什么我做错了吗?

标签: apache

解决方案


您想在同一台机器上拥有 2 个网站,每个网站都有一个 IP 地址?所以:

  • 在系统上配置两个 IP
  • 在 Listen 中设置两个 IP:80
  • 每个 IP / 域配置一个 VirtualHost

像这样:

Listen *.80

<VirtualHost 101.1.1.1:80>
    ServerName siteone.example.org
    ServerAlias www.siteone.example.org

    ServerAdmin webmaster.example.org

    DocumentRoot "/var/www/siteone/public/html"
    <Directory /var/www/siteone/public_html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/siteone_error.log
    CustomLog ${APACHE_LOG_DIR}/siteone_access.log combined

</VirtualHost>

<VirtualHost 104.1.1.1:80>
    ServerName sitetwo.example.org
    ServerAlias www.sitetwo.example.org

    ServerAdmin webmaster.example.org

    DocumentRoot "/var/www/sitetwo/public/html"
    <Directory /var/www/sitetwo/public_html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/siteotwo_error.log
    CustomLog ${APACHE_LOG_DIR}/sitetwo_access.log combined

</VirtualHost>

在您的 DNS 中,配置:

  • 101.1.1.1 siteone.example.org www.siteone.example.org
  • 104.1.1.1 sitetwo.example.org www.sitetwo.example.org

推荐阅读