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:
-
No vhost has the requested hostname. A new DNS record points
client-b.example.comat the server, but the vhost file for client-b is insites-available/and was never enabled. Request matches noServerName, falls back to default, serves client-a. -
Vhost file is enabled but has a typo in
ServerName. The file saysSeverName client-b.example.com(typo) and Apache silently parsed it without flagging —configtestwill say “warning, ServerName not set, using fqdn.” The vhost loads but matches nothing. -
Vhost load order surprises. Two vhosts both claim the same name, or a
ServerAlias *.example.comswallows requests intended for a more specific sibling. The first-loaded vhost wins, and load order is alphabetical-by-filename withinsites-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
-
Capture the current vhost map. Save the output of
apache2ctl -Sto a file — you will refer back to it as you change things:sudo apache2ctl -S 2>&1 | tee /tmp/vhost-map-before.txt -
Identify the default server. Look for
default serverin 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. -
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 headerIf curl with
Host: client-b.example.comreturns 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. -
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.logfrom the last restart). -
Check for wildcard ServerAlias swallowing the request. A vhost with
ServerAlias *.example.comwill matchclient-b.example.comunless 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.confloads before100-wildcard.conf). -
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.txtThe diff should show the line you just changed. If it shows nothing, you edited the wrong file —
sites-enabled/entries are symlinks tosites-available/, but a bare file dropped intosites-enabled/works too and can shadow the symlinked one with the same name. -
Add a catch-all default vhost so future overlaps are visible. This is the structural fix — a
000-catchall.confthat 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
ServerNamewithout a port (e.g.ServerName example.com) is fine for 80/443 vhosts. For a vhost listening on a non-standard port, you must writeServerName example.com:8443or external link generation breaks.ServerAliaswildcards match one label only.*.example.commatchesa.example.combut nota.b.example.com. For deep wildcards, list explicitly.- The
UseCanonicalNamedirective controls how Apache constructs self-referential URLs.UseCanonicalName On(useServerName) prevents the requestHost:header from leaking into rewrites — useful when an attacker submitsHost: evil.comhoping to be redirected. - An IPv6 listener configured for
[::]:80is 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
NameVirtualHostdirective — 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.