Virtual Hosts Explained
A virtual host is a configuration block that tells a web server how to answer requests for one specific domain or subdomain. It lets a single physical or virtual server run many independent websites, each with its own document root and settings, by matching the domain name sent in each request. Apache calls these VirtualHost blocks and Nginx calls them server blocks.
Name-based vs. IP-based virtual hosts
Almost all hosting today uses name-based virtual hosting: many domains share the same IP address, and the server tells them apart using the domain name sent by the browser, either in the plain HTTP Host header or, for HTTPS, in the TLS handshake through a feature called SNI (Server Name Indication). IP-based virtual hosting, where each site gets its own dedicated IP address, is now mostly reserved for special cases such as certain legacy clients or specific compliance requirements, since IPv4 addresses are limited and SNI solved the HTTPS problem that once required separate IPs.
A basic Apache virtual host
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/log/apache2/example.com-error.log
CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>
Apache reads every VirtualHost block, usually from files under /etc/apache2/sites-available/, and matches the incoming ServerName or ServerAlias against the domain in the request to pick the right block.
A basic Nginx server block
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.html index.php;
}
Nginx calls the same concept a server block. Configuration typically lives under /etc/nginx/sites-available/, with a symlink into sites-enabled/ to activate it, followed by a configuration test and reload.
Common virtual host problems
| Symptom | Likely cause |
|---|---|
| Wrong site loads for a domain | A missing or misordered ServerName, or the default/first block catching unmatched requests |
| New domain shows a 404 or the server's default page | Virtual host file not enabled or web server not reloaded after adding it |
| SSL certificate error on one of several domains | Certificate not issued or configured for that specific virtual host |
Frequently asked questions
What is a virtual host in simple terms?
A virtual host is a configuration section that tells a web server how to handle requests for one particular domain name, so a single server can run several separate websites at once.
What is the difference between a virtual host and a VPS?
A virtual host is a web server software feature for separating domains on the same machine, while a VPS is a virtual machine with its own operating system. A single VPS can run many virtual hosts inside it.
How does a server know which site to show when domains share an IP address?
The browser sends the requested domain name in the HTTP Host header, and for HTTPS also during the TLS handshake via SNI. The web server matches that name against its configured virtual hosts to pick the right site.
What is the difference between an Apache VirtualHost and an Nginx server block?
They serve the same purpose, defining how a web server responds to a specific domain, but use different configuration syntax and directive names. Apache uses VirtualHost blocks and ServerName/ServerAlias, while Nginx uses server blocks and server_name.
Do I need a separate IP address for each website?
No, in almost all cases. Name-based virtual hosting with SNI lets each domain have its own SSL certificate and configuration while sharing one IP address, which is standard practice today.
Why does my new domain show the wrong website?
This usually means the virtual host for the new domain was not created, not enabled, or the web server was not reloaded after the change, so requests fall through to another matching or default virtual host.
Related topics
- What Is a Web Server? Web Servers
- What Is a Domain Name? Domains & DNS
- What Is Apache (Apache HTTP Server)? Web Servers
- What Is Nginx? Web Servers
- What Is an IP Address? Networking & IP