Apache 2.4 VirtualHost ServerName 被忽略

Apache 2.4 VirtualHost ServerName is ignored

在 Amazon EC2 上,我有以下配置:

<VirtualHost *:80>
    ServerName   a.example.com
    ServerRoot   /var/www/a.example.com
    DocumentRoot html
</VirtualHost>

<VirtualHost *:80>
    ServerName   b.example.com
    ServerRoot   /var/www/b.example.com
    DocumentRoot html
</VirtualHost>

<VirtualHost *:80>
    ServerName   c.example.com
    ServerRoot   /var/www/c.example.com
    DocumentRoot html
</VirtualHost>

问题是,尽管上述配置是正确的,但对 3 个域名中任何一个的所有请求都被定向为好像请求转到了 c.example.com - 就像 ServerName 值一样只是被忽略了。

有人看到这里有问题吗?

我发现的问题是我构建的 Apache2:

Server version: Apache/2.4.18 (Amazon)
Server built:   Mar  7 2016 22:32:11

没有正确处理 DocumentRoot 参数。

在 DocumentRoot 配置定义 here 中,它说

If the directory-path is not absolute then it is assumed to be relative to the ServerRoot.

嗯,这显然被忽略了,因为如果我按如下方式更改 DocumentRoot 值:

<VirtualHost *:80>
    ServerName   b.example.com
    ServerRoot   /var/www/b.example.com
    DocumentRoot html
</VirtualHost>

为此:

<VirtualHost *:80>
    ServerName   b.example.com
    ServerRoot   /var/www/b.example.com
    DocumentRoot /var/www/b.example.com/html   #<-- updated
</VirtualHost>

然后配置工作。我还没有测试过这是否是核心 Apache 构建的问题,或者它是否与 Amazon 版本有关,无论哪种方式,我希望这个答案对某人有所帮助。

ServerRoot is allowed only in server config Context,不在VirtualHost

If you try to use it elsewhere, you'll get a configuration error that will either prevent the server from handling requests in that context correctly, or will keep the server from operating at all -- i.e., the server won't even start.