创建虚拟主机时,Apache 主服务器并不像 httpd.conf 中所述那样作为默认服务器运行,为什么?

创建虚拟主机时,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.
#

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

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

答案1

多年来,我经常被问到这个问题,当我尝试向一些客户解释这一点时,他们有时会与我争论这一点,因为它确实出现(并且实际上可能确实强调说明 - 在这方面有点模棱两可),主服务器应该使用所有这些配置作为它自己的配置,并为所有后续虚拟主机设置这些默认值。

因此,我经常在 httpd.conf 文件中(就在主服务器部分开始的位置下方)以及我支持的各种客户门户的知识库文章中包含以下内容。这有助于(稍微)减少 VPS 客户的支持单数量,这些客户在尝试配置 httpd 服务器上的虚拟主机支持时最终会抓狂不已:

#                       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 (i.e., 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 - i.e., You now can haz cheezburgerz!

因此,为了澄清和总结,您可能会注意到一些默认/原始配置文件具有定义的“默认' 在 VirtualHost 标签内,但这些仅适用于基于 IP 的虚拟主机 - 这就是为什么 2.4 文档在基于名称的虚拟主机文档中没有提及这一点,因此您不会遗漏任何内容,它只是与基于名称的虚拟主机无关。

您可能会想,既然“主主机消失”,当您在第一个 VirtualHost 容器的上下文中再次复制部分或全部指令时,您将收到有关重复的错误 - 但您不会收到,这是完全合法的,并且预计您会或可能会复制它以创建第一个(并且由于是第一个,也是“默认”)虚拟主机。conf 文件中的其他重复项将导致服务器抛出投诉,但在此特定和特殊情况下不会发生。

因此你可能会问自己:

“由于我创建了虚拟主机容器,那么主服务器的所有默认设置怎么办?我是否也需要重新指定它们?”

不,您不需要。“主服务器”的具体方面以及 httpd.conf 中的其他指令实际上已配置并设置为您创建的任何后续虚拟主机的默认设置 - 只有主服务器本身(服务器管理员、服务器名称等)需要在 VirtualHost 容器的上下文中重申和定义。

它本来可以在 httpd.conf 所包含的注释中得到更好的澄清,但事实并非如此,而且至少看起来具有误导性 - 所以,不,你没有疯,服务器应该以这种方式响应。

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

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

这 ”默认“服务器-基于名称和基于 IP 的虚拟服务器之间的使用解释: VirtualHost 上下文中 _default_:* 和 *:* 之间的区别

我希望这个问答能够帮助一些人,因为需要花点时间研究一下 Apache 文档才能弄清楚上述概念 - 但我应该注意,系统管理员在 httpd.conf 文件的最开头就被警告要阅读全文,而不是在理解每个设置的作用之前就开始配置该文件中的东西 ;)

享受!

相关内容