Use it when hosting several sites on one server, or when the wrong site appears for a name.
Virtual Hosts
Virtual hosts let one web server serve several sites, selected by host name (Host header and TLS SNI), IP address or port.
1. curl -vI https://<name> — which site and certificate?
2. Apache apachectl -S / Nginx nginx -T — name in a vhost?
3. DNS points the name to this server?
4. Certificate SAN includes the name?
5. Define an explicit default vhost that returns 404/444.
client sends Host/SNI → server matches a virtual host → serves its content and certificate
For Web Services → Virtual Hosts, continue with the first unproven Topic check: Pin destination IP/port, requested hostname, scheme/SNI, expected site and config enablement path. If that boundary is healthy, advance to the adjacent check: Inspect effective listener and vhost/server ordering rather than file presence alone. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Operational evidence path Verify boundary
listener → TLS SNI if used → Host header → server/vhost precedence → site root/proxy policy → response
Healthy evidence: the requested name resolves to the expected listener and deterministic vhost/server selection yields the intended certificate, document root or upstream
Step 2 Verification and output interpretation Verify boundary
Existing fast checks for this Topic. - Pin destination IP/port, requested hostname, scheme/SNI, expected site and config enablement path.
- Inspect effective listener and vhost/server ordering rather than file presence alone.
- Test with the exact Host/SNI from both local and real-client paths and correlate logs.
- Fix only the selection cause, then retest the original hostname and a sibling/default site.
Verification chain. listener → TLS SNI if used → Host header → server/vhost precedence → site root/proxy policy → response
Step 3 Commands Verify boundary
apachectl -S
nginx -T | grep server_name
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: default site captures traffic, duplicate names/listeners create surprising precedence, SNI and Host disagree, DNS points elsewhere, or tests omit the real Host header
Step 5 Common mistakes Narrow the fault
- Overlapping server names between vhosts.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
If no virtual host matches the name, the default (first or explicitly marked) answers — often showing another site. Each HTTPS site needs a certificate matching its name (SNI).
systemctl status nginx|apache2|httpd— web server statenginx -t / apachectl configtest— configuration testss -ltnp | grep -E ':80|:443'— listening portscurl -vI https://<name>— response, headers and TLStail -f /var/log/nginx/error.log— error log (or apache2/httpd)
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
Virtual-host configuration is confirmed when the real request selects the intended site under both TLS and HTTP routing rules, not merely when the site definition exists.
In depth
Virtual hosts: many sites on one server
Virtual hosting serves several sites from one server. The Apache documentation on name-based virtual hosts describes the two approaches:
- IP-based hosting needs one address per site.
- Name-based hosting relies on the host name the client sends, so many sites share one address. The documentation recommends it, unless equipment explicitly requires IP-based hosting.
Apache first finds the most specific <VirtualHost> for the address and port. Among those, it compares ServerName and ServerAlias with the requested name, and uses the first listed vhost when nothing matches. It also advises always setting ServerName explicitly.
Name matching in Apache and nginx
| Rule | Apache | nginx |
|---|---|---|
| Exact name | ServerName www.example.net | server_name www.example.net; |
| Several names | ServerAlias example.net *.example.net | server_name example.net *.example.net; |
| Wildcards | * and ? in ServerAlias | Leading or trailing *, or regular expressions (~^...$) |
| Unknown names | The first listed vhost for the address | The default_server, or the first server |
HTTPS and SNI
With TLS, the server must choose a certificate before it sees the HTTP Host header. SNI (Server Name Indication) sends the name in the TLS handshake, so name-based virtual hosting works for HTTPS too. nginx's HTTPS guide notes that SNI is mandatory to implement in TLS 1.3, and that only domain names are reliable in SNI, not IP addresses.
A layout that scales
| Practice | Reason |
|---|---|
| One file per site | Enable or disable a site independently |
| An explicit default site that rejects unknown names | Nothing is served under names you didn't configure |
| Separate logs per site | Easier analysis and rotation |
| The same names in the certificate (SAN) and the configuration | No certificate warnings |
host$ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName default.invalid
DocumentRoot /var/www/empty
Redirect 404 /
</VirtualHost>
host$ cat /etc/apache2/sites-available/intranet.conf
<VirtualHost *:80>
ServerName intranet.example.net
ServerAlias intra.example.net
DocumentRoot /srv/www/intranet
ErrorLog ${APACHE_LOG_DIR}/intranet-error.log
CustomLog ${APACHE_LOG_DIR}/intranet-access.log combined
</VirtualHost>
host$ sudo a2ensite intranet && sudo apachectl configtest && sudo systemctl reload apache2
host$ sudo apachectl -S 2>&1 | grep -E "namevhost|default server"
default server default.invalid (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost default.invalid (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost intranet.example.net (/etc/apache2/sites-enabled/intranet.conf:1)
host$ curl -s -o /dev/null -w "%{http_code}\n" -H "Host: intra.example.net" http://localhost/
200
Reading apachectl -S
The 000- prefix makes the empty default site load first, so it becomes the default for port 80. Requests for unknown names get a 404, instead of the intranet site. apachectl -S confirms which vhost is the default, and lists every name it answers for.
Documented example, not captured output.
How it works at runtime
Virtual Hosts is part of Web Services → Virtual Hosts. Virtual Hosts maps multiple web identities to shared listeners using request attributes such as address/port, Host and often TLS SNI; diagnosis must show which site actually wins selection.
Topic-specific state model. Virtual Hosts maps multiple web identities to shared listeners using request attributes such as address/port, Host and often TLS SNI; diagnosis must show which site actually wins selection.
Interpretation rule. - A vhost file exists ≠ it is enabled or selected.
Quick reference
Name-based virtual hosts serve several sites from one IP, selected by the Host header (and SNI for TLS).
server {
listen 443 ssl;
http2 on;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
root /srv/www/app;
access_log /var/log/nginx/app.access.log;
}
server { listen 80; server_name app.example.com; return 301 https://$host$request_uri; }
Apache equivalent: <VirtualHost *:443> ServerName app.example.com … </VirtualHost>; apachectl -S shows which vhost answers each name. Always define an explicit default server so unknown names don't land on a real site.
Limits and how it fails
Primary failure point. Virtual-host configuration is confirmed when the real request selects the intended site under both TLS and HTTP routing rules, not merely when the site definition exists.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Distribution/platform context. Virtual Hosts owns site-selection semantics across Apache/Nginx; server-specific syntax remains in their Topics.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Services → Virtual Hosts. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://nginx.org/en/docs/ · confidence 94%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
Virtual Hosts operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for Virtual Hosts.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | Virtual Hosts maps multiple web identities to shared listeners using request attributes such as address/port, Host and often TLS SNI; diagnosis must prove which site actually wins selection. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin exact client/server + time + request/file operation → identify authoritative configuration → read back effective service/listener/session state → trace protocol selection/auth/identity → verify backend/upstream boundary → correlate logs → make smallest persistent change → repeat original workload + regression check. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | listener → TLS SNI if used → Host header → server/vhost precedence → site root/proxy policy → response | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | Virtual-host configuration is proven when the real request selects the intended site under both TLS and HTTP routing rules, not merely when the site definition exists. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux web/file-service evidence can establish this proof path: listener → TLS SNI if used → Host header → server/vhost precedence → site root/proxy policy → response | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove unobserved clients, names, identities, routes, sessions, backends or time periods. Virtual-host configuration is proven when the real request selects the intended site under both TLS and HTTP routing rules, not merely when the site definition exists. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven service boundary, capture adjacent effective state and protocol/application evidence, make only the demonstrated persistent change, then repeat the original client workload plus a regression check. | Continue to the next unresolved boundary while preserving the original test condition. |
Virtual Hosts · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | Virtual Hosts |
|---|---|
| Topic path | Web Services → Virtual Hosts |
| Runtime state | Virtual Hosts maps multiple web identities to shared listeners using request attributes such as address/port, Host and often TLS SNI; diagnosis must prove which site actually wins selection. |
| Fast checks | - Pin destination IP/port, requested hostname, scheme/SNI, expected site and config enablement path. - Inspect effective listener and vhost/server ordering rather than file presence alone. - Test with the exact Host/SNI from both local and real-client paths and correlate logs. - Fix only the selection cause, then retest the original hostname and a sibling/default site. |
| Verification chain | listener → TLS SNI if used → Host header → server/vhost precedence → site root/proxy policy → response Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. |
| Failure boundary | Virtual-host configuration is proven when the real request selects the intended site under both TLS and HTTP routing rules, not merely when the site definition exists. Boundary chain: unit config/drop-ins → dependency/order/condition → exec/process → readiness/active state → owned socket/file/device → dependent client/workload. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |