Ubuntu Apache 为站点使用了错误的虚拟主机

Ubuntu Apache Using Wrong VHosts For Sites

我正在使用 Ubuntu 16.0.4,并配置了虚拟主机,所有 .conf 文件位于 /etc/apache2/sites-available,所有启用的站点位于 /etc/apache2/sites-enabled 使用 a2ensite 对于每个站点 conf。我禁用了 000-default.conf,我正在使用 certbot,以及最新的 apache 和 PHP 7.2.x.

我的两个站点的虚拟主机配置有问题。这些站点是:

Site 1: clients.site1.com.conf
Site 2: clients.site2.com.conf
Site 3: site3.com.conf (Mention later)

如果我在浏览器中转到 clients.site1.com,它会正常加载。

如果我在浏览器中转到 clients.site2.com,url 显示 clients.site2.com 已加载,但它实际上是从 /var/www/clients.site1.com/public_html 加载 clients.site1.com 内容。

我已经检查过 clients.site1.com.conf,并且有以下内容:

<VirtualHost *:80>
    ServerAdmin webmaster@clients.site1.com
    ServerName clients.site1.com
    ServerAlias clients.site1.com
    DocumentRoot /var/www/clients.site1.com/public_html

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =clients.site1.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我已经检查过 clients.site2.com.conf,并且有以下内容:

<VirtualHost *:80>
    ServerAdmin webmaster@clients.site2.com
    ServerName clients.site2.com
    ServerAlias clients.site2.com
    DocumentRoot /var/www/clients.site2.com/public_html

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =clients.site2.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我一直在研究 conf 文件的文件名,认为有两个 clients. 的 conf 文件存在通配符匹配问题。所以我尝试删除点 .,甚至尝试用连字符 - 替换它。还是一样的结果。

我做的最后一个测试是我禁用了 clients.site1.com.conf。当我这样做然后继续在我的浏览器中加载 clients.site2.com 时,加载的是 conf 文件顺序的下一个站点(上面的站点 3 作为示例)。

更新:

我 运行 apache2ctl -S 得到了以下内容:

root@ip-111-111-111-111:/etc/apache2/sites-available# apache2ctl -S
VirtualHost configuration:
*:443 is a NameVirtualHost

 default server clients.site1.com (/etc/apache2/sites-enabled/clients.site1.com-le-ssl.conf:2)
 port 443 namevhost clients.site1.com (/etc/apache2/sites-enabled/clients.site1.com-le-ssl.conf:2)
         alias clients.site1.com
 port 443 namevhost site3.com (/etc/apache2/sites-enabled/site3.com-le-ssl.conf:2)
         alias www.site3.com
 port 443 namevhost aaaaa.com (/etc/apache2/sites-enabled/aaaaa.com-le-ssl.conf:2)
         alias www.aaaaa.com
 port 443 namevhost bbbbb.com (/etc/apache2/sites-enabled/bbbbb.com-le-ssl.conf:2)
         alias www.bbbbb.com
 port 443 namevhost ccccc.com (/etc/apache2/sites-enabled/ccccc.com-le-ssl.conf:2)
         alias www.ccccc.com
 port 443 namevhost ddddd.com (/etc/apache2/sites-enabled/ddddd.com-le-ssl.conf:2)
         alias www.ddddd.com
 port 443 namevhost eeeee.com (/etc/apache2/sites-enabled/eeeee.com-le-ssl.conf:2)
         alias www.eeeee.com
 port 443 namevhost ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/fffff.com-le-ssl.conf:2)
         alias www.fffff.com
 port 443 namevhost ggggg.com (/etc/apache2/sites-enabled/ggggg.com-le-ssl.conf:2)
         alias www.ggggg.com
 port 443 namevhost site2.com (/etc/apache2/sites-enabled/site2.com-le-ssl.conf:2)
         alias www.site2.com
*:80 is a NameVirtualHost
 default server ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/000-default.conf:1)
 port 80 namevhost ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/000-default.conf:1)
 port 80 namevhost clients.site1.com (/etc/apache2/sites-enabled/clients.site1.com.conf:1)
         alias clients.site1.com
 port 80 namevhost clients.site2.com (/etc/apache2/sites-enabled/clients.site2.com.conf:1)
         alias clients.site2.com
 port 80 namevhost site3.com (/etc/apache2/sites-enabled/site3.com.conf:1)
         alias www.site3.com
 port 80 namevhost aaaaa.com (/etc/apache2/sites-enabled/aaaaa.com.conf:1)
         alias www.aaaaa.com
 port 80 namevhost bbbbb.com (/etc/apache2/sites-enabled/bbbbb.com.conf:1)
         alias www.bbbbb.com
 port 80 namevhost ccccc.com (/etc/apache2/sites-enabled/ccccc.com.conf:1)
         alias www.ccccc.com
 port 80 namevhost ddddd.com (/etc/apache2/sites-enabled/ddddd.com.conf:1)
         alias www.ddddd.com
 port 80 namevhost eeeee.com (/etc/apache2/sites-enabled/eeeee.com.conf:1)
         alias www.eeeee.com
 port 80 namevhost ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/fffff.com.conf:1)
         alias www.fffff.com
 port 80 namevhost ggggg.com (/etc/apache2/sites-enabled/ggggg.com.conf:1)
         alias www.ggggg.com
 port 80 namevhost site2.com (/etc/apache2/sites-enabled/site2.com.conf:1)
         alias www.site2.com

ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
root@ip-111-111-111-111:/etc/apache2/sites-available#

您可以做几件事来帮助调试:

apache2ctl -S

这将向您显示 Apache 认为您拥有的 vhost 配置。在重命名之前 a2dissite 配置文件很重要,等等

I have 000-default.conf disabled

您最好保持此启用状态并创建一个默认文件,当 Apache 不知道去哪里时加载该文件。它可以是一个非常简单的文件,只写 "error",但是当 Apache 认为它无处可去时,您就会意识到。此外,为所有不合法的请求设置一个蜜罐可以提高安全性。最后,我有一段模糊的记忆,发现如果未设置 000-default,它实际上可能会随机选择其中一个虚拟主机,但不确定,但这可以解释站点 3 的行为。

最后,您将 ServerNameServerAlias 设置为相同的 URL。这不是必需的,虽然我认为这不是问题所在,但最好将其从混合中删除。

否则我没有发现你的 vhosts 文件有任何问题并且应该可以工作。

更新

您正在重定向到 https URL。因此,指示 Apache 的是 -le-ssl.com.conf 文件。我也曾与 Let's Encrypt 进行过类似的斗争。以下是我让 Apache 和 Let's Encrypt 都满意的过程:

  • 为非安全端口 80 创建一个配置 (website.com.conf)
  • a2ensite website.com.conf
  • apache2ctl graceful(所以 Apache 知道不安全 URL)
  • letsencrypt --apache -d website.com(这将生成整个 -le-ssl 文件)
  • apache2ctl graceful

如果它可以帮助某人:我遇到了完全相同的问题。

在 Apache 中配置的几个网站。他们工作正常。

我将它们移到了新服务器。我不知道为什么(可能是默认 Apache conf 的不同)但它们开始表现得像问题中解释的那样,比如 "pointing" 到默认站点。

原因是我没有将站点的 "non www" 名称转发到 "www prefixed" 名称的 ServerAlias 行。

我习惯于输入 "mydomainname.com" 而不是 "www.mydomainname.com",因为我的 DNS 配置中有一行如果缺少 www 则将请求转发到 www.xxxx。

因此,Apache 未找到服务器名称,并试图将请求传递到默认站点。

但是,因为我删除了 000-default.conf 文件的内容,请求被传递到第一个工作网站。

所以请确保您有:

  • 一个有效的 000-default.conf 文件
  • 您的 www 前缀网站的 ServerAlias

    服务器别名www.yourdomainname.com