vhosts.conf 中的 DocumentRoot 覆盖 httpd.conf 中的全局 DocumentRoot

Art*_*san 5 apache-http-server osx-yosemite

我在优胜美地运行 Apache 2.4

这是我的 /private/etc/apache2/httpd.conf

ServerName 127.0.0.1:80
DocumentRoot "/Library/WebServer/Documents/home_www/"
<Directory "/Library/WebServer/Documents/home_www">   
    Options Multiviews FollowSymLinks
    MultiviewsMatch Any
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)

通过此设置,我可以使用http://127.0.0.1,并http://localhost在Web浏览器中,然后将其引导我/Library/WebServer/Documents/home_www/index.html像往常一样

然后我补充说,Include /private/etc/apache2/extra/httpd-vhosts.conf因为我想在我的机器上使用 vhost

这是我的 /private/etc/apache2/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin jeud@hotmail.com
    ServerName tutor4dev.local 
    DocumentRoot "/Library/WebServer/Documents/home_www/xxx"
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我试着用sudo apachectl -S显示vhost配置,有结果

VirtualHost configuration:
*:80                   xxx.local (/private/etc/apache2/extra/httpd-vhosts.conf:28)
ServerRoot: "/usr"
Main DocumentRoot: "/Library/WebServer/Documents/home_www/"
Main ErrorLog: "/private/var/log/apache2/error_log"
Mutex proxy: using_defaults
Mutex default: dir="/private/var/run/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/private/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70
Group: name="_www" id=70
Run Code Online (Sandbox Code Playgroud)

我可以使用http://xxx.local访问/Library/WebServer/Documents/home_www/xxx/index.html(虚拟主机),但加入的虚拟主机后,http://127.0.0.1http://localhost还告诉我去/Library/WebServer/Documents/home_www/xxx/index.html,而不是/Library/WebServer/Documents/home_www/index.html

请指导如何修复它谢谢

小智 6

您只有一个 VirtualHost,并且其中带有 * ( <VirtualHost *:80>)。您的主服务器永远不会回答任何请求。所有请求将由配置的第一个虚拟主机处理,这是基于名称的虚拟主机的默认服务器。您应该创建一个新的虚拟主机,它必须出现在配置文件中的所有其他主机之前,例如:

# Main server, catches all requests that do not correspond to a ServerName/Alias
<VirtualHost *:80>
    ServerName 127.0.0.1
    DocumentRoot "/Library/WebServer/Documents/home_www/"
    ...
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

apache 文档中的更多信息