编程语言
首页 > 编程语言> > php-Apache虚拟主机未按预期工作

php-Apache虚拟主机未按预期工作

作者:互联网

我的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绕过了它进入第二个条目.假定第一个条目是全部捕获条目.我尝试交换第二个和第三个条目以查看其行为,但是它始终将浏览器定向到第二个条目,而不是捕获所有假定处理“ localhost / test / me.php”之类的地址(第一个条目)

每当我在浏览器中仅输入“ localhost”时,它都会直接进入第二个条目,而不是说打印“ www”文件夹的内容.

我该如何解决这个问题?谢谢.

解决方法:

看来问题出在您使用VirtualHost指令的方式上.

不建议对虚拟主机的IP地址使用完全限定的域名.这会误导其工作方式.基于名称的虚拟主机通过ServerName指令而不是VirtualHost指令(< VirtualHost FQDN:80>)中的FQDN来确定主机.实际上,这被视为< VirtualHost 127.0.0.1:80\u0026gt; 发生的情况是您的情况记录在VirtualHost doc的后两段中(紧接在“安全性”之前),并引用:

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)相对应的任何主机,因此它将第一个具有IP的VHost选择为“默认”:端口,而不是带*:Port的第一个端口.

要解决您的问题,请尝试使用< VirtualHost *:80>在所有三个vhost声明中:

<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可以使用您的设置制作一个有效的测试用例.)

标签:laravel,apache,virtualhost,php
来源: https://codeday.me/bug/20191028/1949688.html