Skip to content

Debugging Apache vhost overlap — when the wrong site answers the request

Why Apache served the wrong vhost — ServerName resolution, default vhost rules, and the apache2ctl -S diagnostic loop that pinpoints which line of which file is responsible.

The bug looks impossible: visiting client-b.example.com serves the page from client-a.example.com. Cache-busting headers do not help; curl from the server hits the same wrong content. The vhost files look correct on inspection. This is the classic Apache vhost overlap — two vhosts on the same socket where one of them is implicitly catching requests meant for the other. This article is the debug procedure we run when “Apache served the wrong site” hits the support queue.

How to verify

The single most important command — what the running daemon actually thinks is configured:

sudo apache2ctl -S 2>&1
sudo apache2ctl -D DUMP_VHOSTS 2>&1
sudo apache2ctl configtest 2>&1
ls /etc/apache2/sites-enabled/ 2>&1

apache2ctl -S prints something like:

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server client-a.example.com (/etc/apache2/sites-enabled/client-a.conf:1)
         port 80 namevhost client-a.example.com (/etc/apache2/sites-enabled/client-a.conf:1)
                 alias www.client-a.example.com
         port 80 namevhost client-b.example.com (/etc/apache2/sites-enabled/client-b.conf:1)

Read this output character by character. The line marked default server is the implicit fallback — every request on *:80 whose Host: header does not match a real ServerName lands there. If client-a.example.com is the default server, any DNS hostname pointing at this IP that does not explicitly match a vhost will serve client-a’s content. That is the bug surfacing.

What’s happening

Apache resolves an HTTP request to a vhost in two phases, and most overlap bugs trip on phase two.

Phase one — socket match. The TCP connection arrived on a specific local IP and port. Apache finds every <VirtualHost ip:port> block matching that socket. If you have <VirtualHost *:80> blocks and <VirtualHost 10.0.0.5:80> blocks, the more specific IP match wins; a *:80 block is only consulted if no IP-specific vhost matched.

Phase two — name match. Within the set of vhosts on the matched socket, Apache reads the Host: header from the request and compares against each vhost’s ServerName and ServerAlias. The match is exact (case-insensitive). Wildcards in ServerAlias like *.example.com match one label only.

If no vhost matches the Host: header, Apache falls back to the first vhost in load order on that socket. That fallback vhost is what apache2ctl -S prints as the “default server.” This is where the bug lives. Three scenarios produce the wrong-site-served symptom:

  1. No vhost has the requested hostname. A new DNS record points client-b.example.com at the server, but the vhost file for client-b is in sites-available/ and was never enabled. Request matches no ServerName, falls back to default, serves client-a.

  2. Vhost file is enabled but has a typo in ServerName. The file says SeverName client-b.example.com (typo) and Apache silently parsed it without flagging — configtest will say “warning, ServerName not set, using fqdn.” The vhost loads but matches nothing.

  3. Vhost load order surprises. Two vhosts both claim the same name, or a ServerAlias *.example.com swallows requests intended for a more specific sibling. The first-loaded vhost wins, and load order is alphabetical-by-filename within sites-enabled/.

The diagnostic loop is to read apache2ctl -S for what the daemon actually thinks is loaded, then trace each suspect line back to a file under sites-enabled/ and confirm the ServerName, ServerAlias, and load order match your intent.

The procedure

  1. Capture the current vhost map. Save the output of apache2ctl -S to a file — you will refer back to it as you change things:

    sudo apache2ctl -S 2>&1 | tee /tmp/vhost-map-before.txt
  2. Identify the default server. Look for default server in the output. The path and line number after it (/etc/apache2/sites-enabled/client-a.conf:1) tells you which file is acting as the fallback. If the file shown is not what you intended to be the fallback (you wanted a catch-all 404 vhost to be the fallback), the problem is load order.

  3. Reproduce the bug with curl, controlling the Host header. This separates DNS issues from Apache issues:

    # From the server, hitting localhost with the explicit hostname
    curl -sI -H 'Host: client-b.example.com' http://127.0.0.1/
    curl -s -H 'Host: client-b.example.com' http://127.0.0.1/ | head -20
    # Verify Apache is responding with the right ServerName in any Location header

    If curl with Host: client-b.example.com returns client-a’s content, the bug is server-side and your debug is in Apache. If curl returns the right content but a browser returns the wrong content, DNS or a proxy is involved.

  4. Search every enabled vhost for the contested hostname. A typo or accidental wildcard match is the usual culprit:

    sudo grep -rn -E 'ServerName|ServerAlias' /etc/apache2/sites-enabled/ \
      | grep -iE 'client-b|\*\.example\.com'

    The grep result tells you which files claim or could claim the hostname. If two files both have ServerName client-b.example.com, the first-loaded one wins (Apache will log “VirtualHost overlap on name” at startup — check /var/log/apache2/error.log from the last restart).

  5. Check for wildcard ServerAlias swallowing the request. A vhost with ServerAlias *.example.com will match client-b.example.com unless a more specific vhost loads first:

    sudo grep -rn 'ServerAlias.*\*' /etc/apache2/sites-enabled/

    If a wildcard alias exists and the specific vhost loads after it alphabetically, the wildcard wins. Fix by renaming the specific vhost file with a lower-sorting prefix (010-client-b.conf loads before 100-wildcard.conf).

  6. Force a clean reload and verify. A graceful reload sometimes misses module-level changes — if the bug persists after edits, do a full restart:

    sudo apache2ctl configtest
    sudo systemctl reload apache2
    sudo apache2ctl -S 2>&1 | tee /tmp/vhost-map-after.txt
    diff /tmp/vhost-map-before.txt /tmp/vhost-map-after.txt

    The diff should show the line you just changed. If it shows nothing, you edited the wrong file — sites-enabled/ entries are symlinks to sites-available/, but a bare file dropped into sites-enabled/ works too and can shadow the symlinked one with the same name.

  7. Add a catch-all default vhost so future overlaps are visible. This is the structural fix — a 000-catchall.conf that loads first and answers any unmatched hostname with a 403 makes the next overlap obvious in the access log rather than serving the wrong site:

    <VirtualHost *:80>
        ServerName catchall.invalid
        DocumentRoot /var/www/catchall
        <Directory /var/www/catchall>
            Require all denied
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/catchall-error.log
        CustomLog ${APACHE_LOG_DIR}/catchall-access.log combined
    </VirtualHost>

Common pitfalls

  • ServerName without a port (e.g. ServerName example.com) is fine for 80/443 vhosts. For a vhost listening on a non-standard port, you must write ServerName example.com:8443 or external link generation breaks.
  • ServerAlias wildcards match one label only. *.example.com matches a.example.com but not a.b.example.com. For deep wildcards, list explicitly.
  • The UseCanonicalName directive controls how Apache constructs self-referential URLs. UseCanonicalName On (use ServerName) prevents the request Host: header from leaking into rewrites — useful when an attacker submits Host: evil.com hoping to be redirected.
  • An IPv6 listener configured for [::]:80 is technically a different socket from *:80. Some installs end up with vhosts defined only for IPv4; an IPv6 request falls back to the daemon-level default, which may be different from the IPv4 default.
  • Apache 2.4 dropped the NameVirtualHost directive — it is implicit. If you copied a config from an Apache 2.2 system and got “NameVirtualHost has no effect” warnings, your real bug may be elsewhere; the warnings are noise.

Stack Harbor manages Apache fleets where vhost layout is config-managed, the catch-all is mandatory, and apache2ctl -S output is captured into a daily diff so vhost-overlap drift surfaces the day it happens. If your last overlap incident took two hours of bisecting sites-enabled/ to find the typo, the audit and the catch-all rollout are part of the standard work under Managed Operations.