A server does not have to host only a single website. With virtual host configuration, you can publish dozens of different sites at once on a single server and a single IP address. This guide explains the virtual host concept and how it is configured on Nginx and Apache.

Related reading: Linux file permissions · Connecting to a server with SSH · Nginx Configuration: Reverse Proxy, Cache and Rate Limiting · Nginx vs Apache: Which One Should You Choose and When

What Is a Virtual Host?

A virtual host is the configuration that lets a web server route an incoming request to different sites based on which domain it came for. When a visitor types site-a.com, site A responds; when they type site-b.com, site B responds — both run on the same server.

How is this possible? The browser sends a Host header with every HTTP request. The web server looks at this header to map the request to the correct site's configuration. This is called name-based virtual hosting and is the standard method of the modern web.

When Is a Virtual Host Used?

  • Hosting multiple projects or client sites on one VPS.
  • Configuring the main site and subdomains (blog.site.com, shop.site.com) separately.
  • Separating the live site and a test/staging environment on the same server.
  • Managing many sites on one server in a reseller or agency model.

Virtual Host on Nginx (Server Block)

On Nginx, a server block is defined for each site. Configuration files are usually kept in /etc/nginx/sites-available/ and enabled with a symbolic link to the sites-enabled/ directory:

nginx
server {
    listen 80;
    server_name site-a.com www.site-a.com;
    root /var/www/site-a;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

For a second site, you create a separate server block with different server_name and root values. Nginx matches the incoming request's Host header against server_name.

Virtual Host on Apache

On Apache, <VirtualHost> blocks provide the same function; files are kept in /etc/apache2/sites-available/ and enabled with the a2ensite command. The logic is the same as Nginx: a separate block for each site, separate ServerName and DocumentRoot.

SSL for Each Site

In a virtual host setup, each domain should have its own SSL certificate. Modern TLS, thanks to SNI (Server Name Indication), supports multiple certificates on a single IP. Tools like Let's Encrypt install and renew a free certificate for each domain automatically.

Frequently Asked Questions

How many sites can I host on one server?

There is no technical limit; the practical limit is the server's resources (CPU, RAM, disk). Dozens of low-traffic static sites run without issues, while even a few heavy dynamic sites can strain a server.

Do the sites affect each other?

Because they are on the same server, they share resources (CPU, RAM); a sudden load on one site can slow the others. If isolation is critical, running each site under a separate user or in containers is recommended.

Is IP-based virtual hosting still used?

Rarely. Name-based hosting is enough for almost all scenarios. The IP-based method only comes up for special network or certificate requirements.