为什么所有不匹配的流量都转到第一个VirtualHost而不是httpd.conf中的默认站点配置

时间:2022-01-08 13:47:43

I have always wondered why all non matching traffic go to the first VirtualHost rather than the default site config in httpd.conf?

我一直想知道为什么所有不匹配的流量都转到第一个VirtualHost而不是httpd.conf中的默认站点配置?

Lets assume httpd.conf has not been edited.

假设httpd.conf尚未编辑。

I create a file called /etc/httpd/conf.d/vhost.conf

我创建了一个名为/etc/httpd/conf.d/vhost.conf的文件

With the following:

具有以下内容:

<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>

This the above example, if you send a request for *.com you would get filtered into the first vhost, no matter what the filters are rather than the default website in httpd.conf

以上示例,如果您发送*.com请求,您将被过滤到第一个vhost,无论过滤器是什么,而不是httpd.conf中的默认网站

What am I missing?

我错过了什么?

1 个解决方案

#1


3  

You’re not doing anything wrong; that’s simply how Apache’s Name-based Virtual Hosts work:

你没有做错任何事;这就是Apache基于名称的虚拟主机的工作方式:

After you configure virtual hosts on Apache, the original default web server (if any) becomes just another virtual host; there’s nothing special about the web server that’s configured in httpd.conf. The default virtual host (for an IP address that Apache “listens” to) is simply the first one listed in the Apache configuration file(s):

在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.

如果要将虚拟主机添加到现有Web服务器,还必须为现有主机创建 块。此虚拟主机中包含的ServerName和DocumentRoot应与全局ServerName和DocumentRoot相同。首先在配置文件中列出此虚拟主机,以使其充当默认主机。

Also, from the same page:

另外,从同一页面:

If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

如果未找到匹配的虚拟主机,则将使用与IP地址匹配的第一个列出的虚拟主机。

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.

因此,第一个列出的虚拟主机是默认的虚拟主机。当IP地址与NameVirtualHost指令匹配时,将永远不会使用主服务器的DocumentRoot。如果您希望对与任何特定虚拟主机不匹配的请求进行特殊配置,只需将该配置放在 容器中,然后在配置文件中首先列出该配置。


The best overall description for how Virtual Hosts work on Apache that I have found is An In-Depth Discussion of Virtual Host Matching. This also specifies that,

我发现虚拟主机如何在Apache上工作的最佳整体描述是对虚拟主机匹配的深入讨论。这也指明了,

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.

配置文件中给定IP:端口对的第一个基于名称的vhost很重要,因为它用于在该地址和端口上接收的所有请求,对于该IP:端口对没有其他vhost具有匹配的ServerName或ServerAlias。如果服务器不支持服务器名称指示,它也用于所有SSL连接。

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).

具有指定IP地址的配置文件中的第一个vhost具有最高优先级,并捕获对未知服务器名称的任何请求,或者没有Host:头字段的请求(例如HTTP / 1.0请求)。

#1


3  

You’re not doing anything wrong; that’s simply how Apache’s Name-based Virtual Hosts work:

你没有做错任何事;这就是Apache基于名称的虚拟主机的工作方式:

After you configure virtual hosts on Apache, the original default web server (if any) becomes just another virtual host; there’s nothing special about the web server that’s configured in httpd.conf. The default virtual host (for an IP address that Apache “listens” to) is simply the first one listed in the Apache configuration file(s):

在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.

如果要将虚拟主机添加到现有Web服务器,还必须为现有主机创建 块。此虚拟主机中包含的ServerName和DocumentRoot应与全局ServerName和DocumentRoot相同。首先在配置文件中列出此虚拟主机,以使其充当默认主机。

Also, from the same page:

另外,从同一页面:

If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

如果未找到匹配的虚拟主机,则将使用与IP地址匹配的第一个列出的虚拟主机。

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.

因此,第一个列出的虚拟主机是默认的虚拟主机。当IP地址与NameVirtualHost指令匹配时,将永远不会使用主服务器的DocumentRoot。如果您希望对与任何特定虚拟主机不匹配的请求进行特殊配置,只需将该配置放在 容器中,然后在配置文件中首先列出该配置。


The best overall description for how Virtual Hosts work on Apache that I have found is An In-Depth Discussion of Virtual Host Matching. This also specifies that,

我发现虚拟主机如何在Apache上工作的最佳整体描述是对虚拟主机匹配的深入讨论。这也指明了,

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.

配置文件中给定IP:端口对的第一个基于名称的vhost很重要,因为它用于在该地址和端口上接收的所有请求,对于该IP:端口对没有其他vhost具有匹配的ServerName或ServerAlias。如果服务器不支持服务器名称指示,它也用于所有SSL连接。

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).

具有指定IP地址的配置文件中的第一个vhost具有最高优先级,并捕获对未知服务器名称的任何请求,或者没有Host:头字段的请求(例如HTTP / 1.0请求)。