Glossary

Virtual Host

A virtual host is a configuration block that lets one web server serve several separate websites, each with its own domain, files, logs and certificate, from the same machine.

How does a web server know which site a visitor wants?

Every HTTP/1.1 and later request carries a Host header naming the domain the browser asked for. The server compares that name with the server_name (nginx) or ServerName and ServerAlias (Apache) of each virtual host and picks the match. For HTTPS, the decision has to happen earlier, before the encrypted request is readable, so the browser also sends the hostname in the TLS handshake through SNI. The server uses that to choose the right certificate. Server Name Indication (SNI) explains the handshake side.

Name-based or IP-based virtual hosts?

TypeHow sites are told apartWhen it is used
Name-basedHost header and SNIAlmost always today; any number of sites on one address
IP-basedThe address the request arrived onLegacy clients without SNI, or a separate address per site
Port-basedThe port numberInternal tools, staging copies, admin interfaces

Our machines come with one public IPv4, and name-based hosting is the reason that is enough for dozens or hundreds of sites. Every current browser and HTTP client sends SNI.

What does an nginx virtual host look like?

On Debian and Ubuntu, each site gets a file in /etc/nginx/sites-available/ linked into sites-enabled; on AlmaLinux and Rocky Linux, files go into /etc/nginx/conf.d/ ending in .conf.

server {
    listen 443 ssl;
    server_name bakery-lisbon.example www.bakery-lisbon.example;
    root /srv/sites/bakery-lisbon/public;
    index index.php index.html;
    ssl_certificate     /etc/letsencrypt/live/bakery-lisbon.example/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/bakery-lisbon.example/privkey.pem;
    access_log /var/log/nginx/sites/bakery-lisbon_access.log combined buffer=32k;
    error_log  /var/log/nginx/sites/bakery-lisbon_error.log warn;
    location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/bakery.sock; }
}

A separate PHP-FPM pool per site, with its own Unix user and socket, keeps one compromised site from reading another's files. Create the log folder once with mkdir -p /var/log/nginx/sites, then check and apply with nginx -t && systemctl reload nginx. Install nginx on Ubuntu 22.04 covers the base install.

And the Apache equivalent?

<VirtualHost *:443>
    ServerName ceramics-porto.example
    ServerAlias www.ceramics-porto.example
    DocumentRoot /srv/sites/ceramics-porto/public
    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/ceramics-porto.example/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/ceramics-porto.example/privkey.pem
    CustomLog ${APACHE_LOG_DIR}/ceramics-porto.log combined
</VirtualHost>

Enable it with a2ensite ceramics-porto on Debian-family systems and reload Apache. Run apachectl -S to print every virtual host Apache knows about, with the file and line that defined it, which is the fastest way to spot two sites claiming the same name.

What happens when no virtual host matches?

The server falls back to a default: in nginx, the block marked default_server on that port, or the first one loaded; in Apache, the first virtual host for that address. If you do not define a default yourself, visitors who reach your IPv4 directly, and scanners that probe it, land on whichever site happens to be first. Define a catch-all that returns nothing useful:

server {
    listen 443 ssl default_server;    # catch-all for names we do not host
    ssl_reject_handshake on;      # nginx 1.19.4 and later: refuse unknown names
}
server { listen 80 default_server; return 444; }

How do I redirect the bare domain and plain HTTP?

Pick one canonical address per site, such as https://bakery-lisbon.example, and send every other form there with a permanent redirect. In nginx a small block on port 80 listing both names with return 301 https://bakery-lisbon.example$request_uri; covers plain HTTP, and a second 443 block for the www name does the same for HTTPS. One canonical address keeps search results, analytics and cookies consistent, and it means certificates only need to cover the names you actually serve.

How do I keep many sites manageable?

  • One directory tree per site, such as /srv/sites/NAME/, owned by a per-site user.
  • One config file per site, named after the domain.
  • Separate logs per site, rotated by logrotate.
  • Certificates issued per domain or with SAN lists, renewed automatically.
  • Templates for new sites, so every block gets the same security headers and limits.

Hosting panels automate all of this; Web hosting with a control panel lists the common choices.

How many virtual hosts can one machine serve?

The number of server blocks is not the limit; traffic, PHP workers and database load are. Hundreds of small sites fit on the S3 with 64 GB DDR4 and 2x 1 TB NVMe, while busier shops with heavy PHP or database work run better on the S11 with 24 cores. Watch total outgoing traffic too, since all sites share the machine's monthly allowance.

Machines for Hosting Many Sites

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)
S3Intel Xeon E-2356G6 / 1264 GB DDR42x 1 TB NVMe2x 10 Gbps$80.38$80.38 (1 month)
S7Intel Xeon E-2288G8 / 16128 GB DDR42x 1 TB NVMe2x 10 Gbps$100.72$100.72 (1 month)
S11Intel Core i9-14900K24 / 32128 GB DDR52x 2 TB NVMe2x 10 Gbps$149.39$149.39 (1 month)

Keep Reading

Server Name Indication (SNI)

The TLS extension that lets one IP address serve HTTPS for many domains, how it works in nginx, and how to test and troubleshoot it.

Open

Web server

Both meanings of web server, the software that speaks HTTP and the machine that runs it, plus what limits how many visitors one box serves.

Open

Web hosting with a control panel

Host client sites under a free hosting panel; one IPv4 serves every HTTPS site through SNI and NVMe keeps PHP pages fast.

Open

Install nginx on Ubuntu 22.04

Install nginx on Ubuntu 22.04 from the archive or the upstream repository, add a server block and open the firewall in a few minutes.

Open

FAQ

Quick Answers

A web server configuration that serves one website, letting a single server host many sites with separate domains, files and certificates.

Yes. SNI lets the server pick the right certificate by hostname during the handshake, so each site keeps its own certificate.

They get the default virtual host. Define a catch-all that rejects the request so no real site is shown by accident.

Run apachectl -S. It prints each virtual host with the config file and line that defines it.

Yes. Separate users and PHP-FPM pools keep a compromised site from reading or changing the files of other sites.