keepcalculator

06 Numbers and Powers

Nginx Virtual Hosts: Name-Based vs IP-Based

Card No. 06·74263 · Updated · Standard formula, shown below

Nginx uses server blocks (the equivalent of Apache’s virtual hosts) to serve multiple websites from one server. The key difference between name-based and IP-based virtual hosts is what Nginx uses to match an incoming request to the correct configuration: the Host header (the domain name) or the IP address and port the request arrived on. This article explains both methods, when to use each, and common pitfalls.

Background: Why Virtual Hosts Exist

A single web server often hosts many websites. Without virtual hosting, you would need a separate machine (or at least a separate IP address) for every site, which is expensive and wasteful. Virtual hosts let one Nginx process listen on a single IP:port and then route each request to the correct site based on the information in the request itself.

Nginx’s configuration file (usually nginx.conf) contains a top-level http block. Inside it, you define one or more server blocks. Each server block describes how to handle requests for a particular site—its document root, SSL certificates, logging, and other rules.

How Name-Based Virtual Hosts Work

Name-based virtual hosting (also called host-based or vhost-based) is the most common method. When a browser connects to a server and sends an HTTP request, it includes a Host header that specifies the domain name (e.g., example.com). Nginx reads this header and compares it against the server_name directive in each server block.

Here is a minimal example of two name-based server blocks:

server {
    listen 80;
    server_name example.com;
    root /var/www/example;
}

server {
    listen 80;
    server_name example.org;
    root /var/www/example_org;
}

When a request arrives with Host: example.com, Nginx uses the first block. If the Host is example.org, it uses the second. If no server_name matches, Nginx uses the default server for that IP:port (the first one defined, or the one marked with default_server).

Because the routing depends on the Host header, you can host hundreds of sites on a single IP address. This is how shared hosting providers run many websites on one server.

How IP-Based Virtual Hosts Work

IP-based virtual hosting uses the destination IP address and port of the incoming connection to choose the server block. Each site gets its own IP address (or at least its own IP:port combination). Nginx checks the listen directive to see which IP and port the request arrived on.

Example:

server {
    listen 203.0.113.1:80;
    server_name example.com;
    root /var/www/site1;
}

server {
    listen 203.0.113.2:80;
    server_name example.com;
    root /var/www/site2;
}

Here, even though both blocks have the same server_name, Nginx can tell them apart because they listen on different IP addresses. The Host header is irrelevant in this case—the IP address alone determines the site.

IP-based hosting is less common today because IPv4 addresses are scarce and because name-based hosting is simpler and cheaper. However, it is still useful when you need to serve different SSL certificates on the same port (before SNI), or when you want to separate traffic by network interface.

Mixing Both: The listen and server_name Combination

Nginx actually combines both methods. The listen directive selects the IP:port, and server_name selects among blocks that share the same listen address. When a request arrives, Nginx first picks the set of server blocks that match the IP:port, then among those it looks for a matching server_name. If none match, it uses the default server for that IP:port.

This means you can have some sites on one IP and others on another, all within the same configuration. For example:

server {
    listen 203.0.113.1:80;
    server_name a.com b.com;
    root /var/www/a;
}

server {
    listen 203.0.113.2:80;
    server_name b.com;
    root /var/www/b_special;
}

Here, b.com on IP 203.0.113.2 gets a different root than b.com on 203.0.113.1. This is rarely needed but shows the flexibility.

How to Choose the Right Method

For almost all modern use cases, name-based virtual hosts are the right choice. They require only one IP address, are easy to configure, and work with the vast majority of clients. IP-based hosting is only necessary in specific situations:

  • You need to serve different SSL certificates on the same port to clients that do not support SNI (Server Name Indication)—very old browsers or some mobile apps.
  • You have separate network interfaces or IPs for different services (e.g., a management interface vs. public traffic).
  • Your application logic depends on the IP address (e.g., geo-blocking or per-IP rate limiting).
  • You are migrating from an old Apache setup that used IP-based vhosts.

In practice, even with SSL, SNI is now universally supported, so name-based hosting with separate certificates per domain is the standard.

Common Problems and Misconceptions

  • The wrong site appears. This usually means no server_name matches the request, so Nginx falls back to the default server. Check your server_name values and make sure the Host header is correct (e.g., no trailing dot, correct case).
  • SSL certificate mismatch. With name-based HTTPS, Nginx must select the correct certificate before the handshake completes. It uses SNI to do this. If you see certificate errors, ensure SNI is enabled (it is by default) and that each server block has its own ssl_certificate directives.
  • Using _ as a catch-all. A common pattern is server_name _; to match any host. This is fine, but remember it will also catch requests with no Host header (HTTP/1.0).
  • Forgetting default_server. The first server block for a given listen address is the default, but it is clearer to explicitly add default_server to the listen directive: listen 80 default_server;.

Performance and Security Considerations

Name-based virtual hosting has essentially zero performance overhead—Nginx does a simple string comparison. IP-based hosting is equally fast. The main security consideration is that with name-based hosting, any domain that resolves to your server’s IP will be served by your default server block unless you restrict it. This can lead to content being served for unexpected domains (often called “ghost” domains). To mitigate this, you can set a default server that returns a 444 (close connection) or a simple error page.

For HTTPS, name-based hosting requires SNI, which is enabled by default in all modern browsers. If you have a very old client that does not support SNI, it will receive the default certificate, which may cause a warning. IP-based hosting avoids this but requires a dedicated IP per site.

Frequently Asked Questions

What is the difference between a server block and a virtual host?

They are the same thing. Nginx calls them “server blocks,” while Apache calls them “virtual hosts.” Both allow one server to host multiple websites.

Can I use both name-based and IP-based virtual hosts on the same Nginx server?

Yes. You can have some server blocks listening on specific IPs and others on all IPs with different server_name values. Nginx handles this transparently.

How do I set the default server for a specific IP:port?

Add the default_server parameter to the listen directive, e.g., listen 80 default_server;. If you have multiple listen directives, you can mark one as default per IP:port.

Why do I get the wrong site when I access the server by IP address?

Because the Host header is the IP address, not a domain name. Nginx cannot match any server_name and uses the default server. To avoid this, set a default server that returns a 444 or a redirect.

Does Nginx support IPv6 for virtual hosts?

Yes. You can use IPv6 addresses in the listen directive, e.g., listen [::]:80;. Name-based and IP-based routing work the same way over IPv6.

Is there a limit to how many server blocks I can have?

No hard limit in Nginx, but each block consumes a small amount of memory. With thousands of blocks, you may want to split configuration files and use include directives to keep things organized.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →