使用http:// localhost时,Apache 2.4中的VirtualHost替换DocumentRoot

时间:2014-10-28 14:04:17

标签: apache virtualhost

我正在使用Yosemite和Apache 2.4

/private/etc/apache2/httpd.conf

ServerName 127.0.0.1:80
DocumentRoot "/Library/WebServer/Documents/"
<Directory "/Library/WebServer/Documents">
    Options Multiviews FollowSymLinks
    MultiviewsMatch Any
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

一切都很好,http://localhost将我引导至默认 /Library/WebServer/Documents/index.html

Include /private/etc/apache2/extra/httpd-vhosts.conf添加到 /private/etc/apache2/httpd.conf

将以下配置添加到 /private/etc/apache2/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin admin@xxx.com
    ServerName xxx.local
    DocumentRoot "/Library/WebServer/Documents/xxx"
    <Directory "/Library/WebServer/Documents/xxx/">
        Options +FollowSymLinks
        AllowOverride all
        Require all granted
    </Directory>
</VirtualHost>

在浏览器中使用http://localhosthttp://127.0.0.1时,Apache会将我引导至 /Library/WebServer/Documents/xxx/index.html 而不是 / Library /WebServer/Documents/index.html ,我该如何解决?请指导。

感谢。

1 个答案:

答案 0 :(得分:5)

我今天遇到了同样的问题。

我在这里找到了解决方案:httpd.apache.org/docs/current/vhosts/name-based.html

主要主机消失

任何与现有服务器不匹配的请求都由全局服务器配置处理,无论主机名或ServerName如何。

当您将基于名称的虚拟主机添加到现有服务器,并且虚拟主机参数与预先存在的IP和端口组合匹配时,请求现在将由显式虚拟主机处理。在这种情况下,通常明智的做法是创建一个ServerName与基本服务器匹配的默认虚拟主机。然后,可以将相同接口和端口上的新域(但需要单独的配置)添加为后续(非默认)虚拟主机。