【问题标题】:VirtualHost in Apache 2.4 replaces DocumentRoot when using http://localhostApache 2.4 中的 VirtualHost 在使用 http://localhost 时替换了 DocumentRoot
【发布时间】:2014-12-23 23:26:23
【问题描述】:

我在 Apache 2.4 中使用 Yosemite

/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,如何解决?请指导。

谢谢。

【问题讨论】:

  • “修复”是什么意思?这完全是意料之中的行为...如果您想要的话,为什么不将 DocumentRoot 更改为 /Library/WebServer/Documents
  • 很抱歉,我错过了DocumentRoot "/Library/WebServer/Documents/",它已经在文件 httpd.conf 中,但我仍然有这个问题
  • 但是现在你的DocumentRoot/Library/WebServer/Documents/xxx 而不是/Library/WebServer/Documents/ 所以显然问题仍然存在。
  • @arco444 没错,其实DocumentRoot 已经出现了但是我只是忘记在帖子中提到了,http://localhost 指向VirtualHostDocumentRoot 中指定的路径
  • 如果httpd-vhosts.conf 不是您想要的DocumentRoot 所在的位置,您为什么不直接删除包含的httpd-vhosts.conf 文件?一旦定义了 VirtualHost,它将优先于默认设置。

标签: apache virtualhost


【解决方案1】:

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

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

主要宿主离开

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

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

【讨论】:

  • 我的意思是,我的要求是我喜欢同时维护http://localhosthttp://127.0.0.1 以访问全局DocumentRoot/Library/WebServer/Documents/http://xxx.local 以访问/Library/WebServer/Documents/xxx/ 这是否相似你曾经遇到过什么问题,并且已经解决了?
猜你喜欢
  • 2012-03-22
  • 2021-01-28
  • 2021-01-30
  • 2023-03-04
  • 2015-06-02
  • 1970-01-01
  • 2016-10-02
  • 2014-02-17
  • 2018-09-08
相关资源
最近更新 更多