0

When setting up an Apache server the Main Server section states the following:

# '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.
#

But the server no longer responds at all once virtual hosts are created.

How does one get the Main Server to serve up these pages as the default server that the above says it will do?

tallship
  • 31
  • 8

1 Answers1

1

Over the years I've gotten this question a lot, and when I try to explain this to some customers they sometimes argue this point with me, because it does indeed appear (and may actually in fact state emphatically - it's a bit ambiguous in that regard), that the main server should be served with all of those configs as its own, and set those defaults for all subsequent virtual hosts.

Because of this, I often include something like the following in the httpd.conf file, just below where the main server section starts, as well as in the knowledge base articles on the various customer portals I support. This helps to cut down (a little) on the number of support tickets of VPS customers that end up pulling their hair out when attempting to configure support for virtual hosts on their httpd servers:

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

So to clarify, and summarize, you may notice that some of the default/original conf files have a defined 'default' within the VirtualHost tags, but these are only for IP based virtual hosts - this is why the 2.4 docs make no mention of this in the name based virtual hosting documentation, so you're not missing anything there, it's just not relevant for name based virtual hosting.

You may be thinking that since the "Main host goes away", that when you duplicate some or all of the directives for it again within the context of the first VirtualHost container, that you will receive errors about duplicates - but you won't, it's perfectly legal and expected that you will or may duplicate this to create the first (and by virtue of being the first, also the "default") virtual host. Other duplications in your conf files will cause the server to throw complaints, but not in this particular, and special instance.

So you may be asking yourself:

"Since I created virtualhost containers, what about all those defaults that the main server had? Do I need to specify those all over again too?"

No. You do not. Those specific aspects of the "main server" and other directives in httpd.conf are actually configured and set as the default for any subsequent virtual hosts that you create - it is only the main server itself (Server Administrator, ServerName, etc) that needs to be reiterated and defined within the context of a VirtualHost container.

It could have been clarified better within the included comments of httpd.conf, but it wasn't, and at the very least, appears misleading - so no, you're not crazy, the server was supposed to respond this way.

Here are the links included in the commented code block above for convenience:

Main server goes away (Apache 2.4 docs): https://httpd.apache.org/docs/current/vhosts/name-based.html#defaultvhost

The "default" server - explanation of use between Name Based and IP Based virtual servers: difference between _default_:* and *:* in VirtualHost Context

I hope this Q&A serves to help out a few folks because it does take a bit of digging through the Apache Docs before the above concepts are crystal clear - but I should note that the sysadmin is warned at the very beginning of the httpd.conf file to rtfm and NOT to just start configuring stuff in that file before understanding what each of those settings do ;)

Enjoy!

tallship
  • 31
  • 8