Apache 虚拟主机未按预期工作
Apache Virtual Hosts Not Working As Expected
我的 Apache "httpd-vhosts.conf" 看起来像这样::
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost laravel.dev:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost learninglaravel.dev:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
我的 "...system32/drivers/etc/hosts" 也看起来像这样::
127.0.0.1 localhost
127.0.0.1 localhost
// I added the following entries. The first two entries above was already there
127.0.0.1 laravel.dev
127.0.0.1 learninglaravel.dev
当我在浏览器中输入 "learningLaravel.dev" 和 "laravel.dev" 时,它们按预期工作正常。但是我的 "www" 文件夹中还有其他文件夹,我用它们来学习 PHP 我希望能够直接从浏览器喜欢说 "localhost/test/me.php"。但是每当我输入这样的地址时,浏览器都会转到 vhosts-conf 文件中的第二个条目 [打印 laravel 错误,这意味着它找不到该文件]。 vhosts-conf 文件中的第一个条目似乎不起作用,Apache 将其绕过到第二个条目。第一个条目应该是捕获所有条目。我试图交换第二个和第三个条目以查看它的行为方式,但它总是将浏览器定向到第二个条目而不是捕获所有(第一个条目),它应该处理像 [=48= 这样的地址]
任何时候我只在浏览器中输入 "localhost",它会直接进入第二个条目,而不是打印 [= 的内容47=]文件夹。
我该如何解决这个问题?谢谢。
问题似乎出在您使用 VirtualHost
指令的方式上。
不建议使用完全限定的域名作为虚拟主机的 IP 地址。它的工作原理具有误导性。基于名称的虚拟主机通过 ServerName
指令确定主机,而不是通过 VirtualHost
指令 (<VirtualHost FQDN:80>
) 中的 FQDN。实际上这被视为 <VirtualHost 127.0.0.1:80>
您的案例记录在 VirtualHost doc 最后 2 段(就在 "Security" 之前),引用:
When a request is received, the server first maps it to the best
matching based on the local IP address and port
combination only. Non-wildcards have a higher precedence. If no match
based on IP and port occurs at all, the "main" server configuration is
used.
If multiple virtual hosts contain the best matching IP address and
port, the server selects from these virtual hosts the best match based
on the requested hostname. If no matching name-based virtual host is
found, then the first listed virtual host that matched the IP address
will be used. As a consequence, the first listed virtual host for a
given IP address and port combination is the default virtual host for
that IP and port combination.
所以当你请求 localhost/somedir
时,服务器会尝试从非通配符的 VHosts 声明中查找,但没有找到任何具有相应主机名(ServerName)的,因此它选择 "default" 第一个 VHost IP:Port,而不是第一个 *:Port.
要解决您的问题,请尝试在所有三个 vhost 声明中使用 <VirtualHost *:80>
:
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
并重新加载/重启 Apache。
(我对此唯一的疑问是为什么 Nasreddine 可以使用您的设置创建一个有效的测试用例。)
我的 Apache "httpd-vhosts.conf" 看起来像这样::
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost laravel.dev:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost learninglaravel.dev:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
我的 "...system32/drivers/etc/hosts" 也看起来像这样::
127.0.0.1 localhost
127.0.0.1 localhost
// I added the following entries. The first two entries above was already there
127.0.0.1 laravel.dev
127.0.0.1 learninglaravel.dev
当我在浏览器中输入 "learningLaravel.dev" 和 "laravel.dev" 时,它们按预期工作正常。但是我的 "www" 文件夹中还有其他文件夹,我用它们来学习 PHP 我希望能够直接从浏览器喜欢说 "localhost/test/me.php"。但是每当我输入这样的地址时,浏览器都会转到 vhosts-conf 文件中的第二个条目 [打印 laravel 错误,这意味着它找不到该文件]。 vhosts-conf 文件中的第一个条目似乎不起作用,Apache 将其绕过到第二个条目。第一个条目应该是捕获所有条目。我试图交换第二个和第三个条目以查看它的行为方式,但它总是将浏览器定向到第二个条目而不是捕获所有(第一个条目),它应该处理像 [=48= 这样的地址]
任何时候我只在浏览器中输入 "localhost",它会直接进入第二个条目,而不是打印 [= 的内容47=]文件夹。
我该如何解决这个问题?谢谢。
问题似乎出在您使用 VirtualHost
指令的方式上。
不建议使用完全限定的域名作为虚拟主机的 IP 地址。它的工作原理具有误导性。基于名称的虚拟主机通过 ServerName
指令确定主机,而不是通过 VirtualHost
指令 (<VirtualHost FQDN:80>
) 中的 FQDN。实际上这被视为 <VirtualHost 127.0.0.1:80>
您的案例记录在 VirtualHost doc 最后 2 段(就在 "Security" 之前),引用:
When a request is received, the server first maps it to the best matching based on the local IP address and port combination only. Non-wildcards have a higher precedence. If no match based on IP and port occurs at all, the "main" server configuration is used.
If multiple virtual hosts contain the best matching IP address and port, the server selects from these virtual hosts the best match based on the requested hostname. If no matching name-based virtual host is found, then the first listed virtual host that matched the IP address will be used. As a consequence, the first listed virtual host for a given IP address and port combination is the default virtual host for that IP and port combination.
所以当你请求 localhost/somedir
时,服务器会尝试从非通配符的 VHosts 声明中查找,但没有找到任何具有相应主机名(ServerName)的,因此它选择 "default" 第一个 VHost IP:Port,而不是第一个 *:Port.
要解决您的问题,请尝试在所有三个 vhost 声明中使用 <VirtualHost *:80>
:
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
并重新加载/重启 Apache。
(我对此唯一的疑问是为什么 Nasreddine 可以使用您的设置创建一个有效的测试用例。)