为什么所有不匹配的流量都转到第一个 VirtualHost 而不是 httpd.conf 中的默认站点配置
Why does all non matching traffic go to the first VirtualHost rather than the default site config in httpd.conf
我一直想知道为什么所有不匹配的流量都转到第一个 VirtualHost 而不是 httpd.conf 中的默认站点配置?
假设 httpd.conf 尚未编辑。
我创建了一个名为 /etc/httpd/conf.d/vhost.conf
的文件
具有以下内容:
<VirtualHost *:80>
ServerName website.com
ServerAlias www.website.com
DocumentRoot "/site1"
<Directory "/site1">
AllowOverride All
Require all granted
</Directory>
Some Rules Here
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "/site2"
<Directory "/site2">
AllowOverride All
Require all granted
</Directory>
Some Rules Here
</VirtualHost>
在上面的示例中,如果您发送 whosebug.com 的请求,您将被过滤到第一个 vhost,无论过滤器是什么而不是 httpd.conf[=13 中的默认网站=]
我错过了什么?
你没有做错任何事;这就是 Apache Name-based Virtual Hosts 的工作原理:
在Apache上配置虚拟主机后,原来默认的web服务器
(如果有的话)成为另一个虚拟主机;没什么特别的
在 httpd.conf
中配置的 Web 服务器。默认虚拟主机(对于 Apache“侦听”的 IP 地址)只是 Apache 配置文件中列出的第一个:
If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost>
block for the existing host. The ServerName
and DocumentRoot
included in this virtual host should be the same as the global ServerName
and DocumentRoot
. List this virtual host first in the configuration file so that it will act as the default host.
此外,来自同一页面:
If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot
from the main server will never be used when an IP address matches the NameVirtualHost
directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a <VirtualHost>
container and list it first in the configuration file.
我找到的关于虚拟主机如何在 Apache 上工作的最佳总体描述是 An In-Depth Discussion of Virtual Host Matching。这也说明,
The first name-based vhost in the configuration file for a given IP:port pair is significant because it is used for all requests received on that address and port for which no other vhost for that IP:port pair has a matching ServerName
or ServerAlias
. It is also used for all SSL connections if the server does not support Server Name Indication.
The first vhost in the config file with the specified IP address has the highest priority and catches any request to an unknown server name, or a request without a Host: header field (such as a HTTP/1.0 request).
我一直想知道为什么所有不匹配的流量都转到第一个 VirtualHost 而不是 httpd.conf 中的默认站点配置?
假设 httpd.conf 尚未编辑。
我创建了一个名为 /etc/httpd/conf.d/vhost.conf
具有以下内容:
<VirtualHost *:80>
ServerName website.com
ServerAlias www.website.com
DocumentRoot "/site1"
<Directory "/site1">
AllowOverride All
Require all granted
</Directory>
Some Rules Here
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "/site2"
<Directory "/site2">
AllowOverride All
Require all granted
</Directory>
Some Rules Here
</VirtualHost>
在上面的示例中,如果您发送 whosebug.com 的请求,您将被过滤到第一个 vhost,无论过滤器是什么而不是 httpd.conf[=13 中的默认网站=]
我错过了什么?
你没有做错任何事;这就是 Apache Name-based Virtual Hosts 的工作原理:
在Apache上配置虚拟主机后,原来默认的web服务器
(如果有的话)成为另一个虚拟主机;没什么特别的
在 httpd.conf
中配置的 Web 服务器。默认虚拟主机(对于 Apache“侦听”的 IP 地址)只是 Apache 配置文件中列出的第一个:
If you are adding virtual hosts to an existing web server, you must also create a
<VirtualHost>
block for the existing host. TheServerName
andDocumentRoot
included in this virtual host should be the same as the globalServerName
andDocumentRoot
. List this virtual host first in the configuration file so that it will act as the default host.
此外,来自同一页面:
If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The
DocumentRoot
from the main server will never be used when an IP address matches theNameVirtualHost
directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a<VirtualHost>
container and list it first in the configuration file.
我找到的关于虚拟主机如何在 Apache 上工作的最佳总体描述是 An In-Depth Discussion of Virtual Host Matching。这也说明,
The first name-based vhost in the configuration file for a given IP:port pair is significant because it is used for all requests received on that address and port for which no other vhost for that IP:port pair has a matching
ServerName
orServerAlias
. It is also used for all SSL connections if the server does not support Server Name Indication.The first vhost in the config file with the specified IP address has the highest priority and catches any request to an unknown server name, or a request without a Host: header field (such as a HTTP/1.0 request).