在创build虚拟主机时,Apache主服务器不会像httpd.conf中所述的那样默认 – 为什么?

设置Apache服务器时,主服务器部分指出以下内容:

# 'Main' server configuration # # The directives in this section set up the values used by the 'main' # server, which responds to any requests that aren't handled by a # <VirtualHost> definition. These values also provide defaults for # any <VirtualHost> containers you may define later in the file. # # All of these directives may appear inside <VirtualHost> containers, # in which case these default settings will be overridden for the # virtual host being defined. # 

但是一旦虚拟主机被创build,服务器就不再响应。

如何让主服务器提供这些页面作为上面说的默认服务器呢?

多年来,我一直在谈这个问题,当我试图向一些客户解释这个问题的时候,他们有时会和我争论这个问题,因为它确实出现了(实际上可能实际上是强调的 – 这有点含糊不清),主服务器应该被configuration为自己的所有configuration,并为所有后续的虚拟主机设置这些默认值。

正因为如此,我经常在httpd.conf文件中,在主服务器部分的起始位置以及我支持的各种客户门户网站上的知识库文章中包含以下内容。 这有助于减lessVPS客户的支持服务单数量,当尝试在其httpd服务器上configuration对虚拟主机的支持时,VPS客户的支持服务单数量将会减less。

 # I know it says above that the main server responds # to ANY requests that aren't handled by a <VirtualHost> # definition - but THIS IS NOT TRUE!!! # # As you can see below, the "Main Server Goes Away"... # # From: https://httpd.apache.org/docs/current/vhosts/name-based.html#defaultvhost # (Note that there is no mention in the official Apache docs recommending the # use of a _default_ directive in the <VirtualHost> tag. Hm.... # # Main host goes away # # Any request that doesn't match an existing <VirtualHost> is handled by the #global server configuration, regardless of the hostname or ServerName. # # When you add a name-based virtual host to an existing server, and the virtual # host arguments match preexisting IP and port combinations, requests will now # be handled by an explicit virtual host. In this case, it's usually wise to # create a default virtual host with a ServerName matching that of the base server. # # OBSERVATION: Wise? It's wise? earlier docs (ie, Apache 2.2 docs) don't put # it that way - you must, if you want the 'Main Server' to be served. # Again, _default_ is not mentioned because it should be, and is, only used for a # default server in IP based virtual hosting, as clarified here: # https://serverfault.com/questions/567320/difference-between-default-and-in-virtualhost-context # # <Virtualhost _default_:*> with Servername foo.com : should not be used with name based virtualhosting # # You may proceed now - ie, You now can haz cheezburgerz! 

因此,为了澄清和总结,您可能会注意到一些默认/原始的conf文件在VirtualHost标签中有一个定义的“ 默认 ”,但是这些仅仅是基于IP的虚拟主机 – 这就是为什么2.4文档没有提到这在名称为基础的虚拟主机文档,所以你不会丢失任何东西,这只是与基于名称的虚拟主机无关。

您可能会认为,自“主要主机消失”以来,当您在第一个VirtualHost容器的上下文中再次复制部分或全部指令时,您将收到有关重复的错误 – 但是您不会,这是完全合法的,并希望你会或可能复制这个来创build第一个(并作为第一个,也是“默认”)的虚拟主机。 你的conf文件中的其他重复会导致服务器投诉,但不是在这个特殊的情况下。

所以你可能会问自己:

“由于我创build了虚拟主机容器,所以主服务器的所有默认值又是如何呢?我是否还需要重新指定这些默认值?

你不可以。 httpd.conf中的“主服务器”和其他指令的这些特定方面实际上被configuration和设置为您创build的任何后续虚拟主机的默认设置 – 它只是需要的主服务器本身(Server Administrator,ServerName等)在VirtualHost容器的上下文中被重复和定义。

在httpd.conf的内容中可能已经有了更好的说明,但是它并不是,至less看起来有误导性 – 所以不是,你并不疯狂,服务器应该这样回应。

为方便起见,以上是包含在上面注释代码块中的链接:

主服务器消失(Apache 2.4文档): https : //httpd.apache.org/docs/current/vhosts/name-based.html#defaultvhost

默认 ”服务器 – 在基于名称和基于IP的虚拟服务器之间的使用说明:虚拟主机上下文中_default _:*和*:*的区别

我希望这个问答有助于帮助一些人,因为在上述概念清晰之前,需要通过Apache文档进行一些挖掘 – 但是我应该注意到系统pipe理员在httpd.conf文件一开始就会被警告到rtfm,而不是只是开始configuration文件中的东西,然后再理解每个设置的作用;)

请享用!