Skip to main content

Name Based Virtual Hosting

Name-based virtual hosting allows multiple websites (domains) to be hosted on a single IP address. NGINX decides which website to serve based on the domain name requested by the client (the Host header in the HTTP request).

This is the most common hosting method today, especially when using shared servers or cloud VMs.

How Name-Based Virtual Hosting Works

Step-by-step request flow

  1. A user types a URL: http://example.com
  2. DNS resolves example.com → same server IP: example.com → 203.0.113.10
  3. The browser sends an HTTP request including: Host: example.com
  4. NGINX:
    • Reads the Host header
    • Matches it against the server_name directive
    • Selects the correct server block
    • Serves the corresponding website

Key Directives Used

DirectivePurpose
server {}Defines a virtual host
listenIP and port to listen on
server_nameDomain(s) this server block responds to
rootDocument root for the website
indexDefault index file

Basic Example: Two Domains, One IP

Scenario

DomainWebsite
example.comCompany website
blog.example.comBlog

Both are hosted on one server IP.

http {

server {
listen 80;
server_name example.com www.example.com;

root /var/www/example;
index index.html;

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

server {
listen 80;
server_name blog.example.com;

root /var/www/blog;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}
}
  1. listen 80;

    • Both sites listen on port 80
    • Same IP, same port → name-based hosting
  2. server_name example.com www.example.com;

    • Matches requests where:
    Host: example.com
    Host: www.example.com
    • You can list multiple domain aliases: server_name blog.example.com;
    • Matches only the blog subdomain
  3. root /var/www/example;

    • Files served from: /var/www/example/index.html
    • Each server block has its own document root.
  4. Request Matching Behavior | Request | Served From | | ------------------------- | ------------------ | | http://example.com | /var/www/example | | http://www.example.com | /var/www/example | | http://blog.example.com | /var/www/blog |

All requests go to the same IP, but different server blocks.

Default Server in Name-Based Hosting

If no server_name matches, NGINX serves the default server.

Default server example

server {
listen 80 default_server;
server_name _;

return 444;
}
  • Prevents unknown domains from being served
  • Improves security
  • Common in production setups

Using Wildcards in server_name

server {
listen 80;
server_name *.example.com;

root /var/www/subdomains;
}
  • Matches: blog.example.com, shop.example.com
  • Does NOT match: example.com

Using Regex in server_name

server {
listen 80;
server_name ~^www\d+\.example\.com$;

root /var/www/regex;
}

Matches: www1.example.com, www2.example.com

Name-Based Virtual Hosting with HTTPS (SNI)

Modern browsers support SNI (Server Name Indication), allowing name-based hosting with SSL.

server {
listen 443 ssl;
server_name example.com;

ssl_certificate /etc/ssl/example.crt;
ssl_certificate_key /etc/ssl/example.key;
}

Each domain can have its own certificate, even on the same IP.

When to Use Name-Based vs IP-Based Hosting

TypeWhen to Use
Name-basedMost websites, shared servers
IP-basedLegacy SSL, strict isolation needs