Master Nginx Virtual Hosts: Complete Configuration Guide
This article explains what virtual hosts are, how to configure them in Nginx using server blocks, the syntax for listen, server_name, location, index, and root directives, and provides practical examples with wildcard and regex hostnames, auto‑index settings, and access controls.
A virtual host divides a single physical server into multiple logical servers, each with its own domain name and directory.
In Nginx, virtual hosts are defined by server blocks in nginx.conf. To host multiple sites, add multiple server blocks.
Simple Virtual Host Example
server {
listen 80;
server_name a.test.com;
location / {
index index.html;
root /home/www/host_a/;
}
}listen 80 makes the host listen on port 80. server_name a.test.com binds the block to the domain a.test.com .
Server name can be expressed in four ways:
Exact name, e.g., a.test.com Wildcard at the start, e.g., *.test.com Wildcard at the end, e.g., mail.* Regular expression, e.g., ~^www\d+\.test\.com$ (must start with ~)
Multiple names can be listed in one server_name directive, and Nginx resolves them by the following priority:
Exact name
Longest wildcard starting with *
Longest wildcard ending with *
First matching regular expression
The location / block matches all requests because every URL starts with /. index index.html; sets the default page, and root /home/www/host_a/; points to the physical directory.
Case Studies
1. Two domains with separate directories
# a.test.com → /home/www/a
server {
listen 80;
server_name a.test.com;
autoindex on; # enable directory listing
index index.html;
root /home/www/a/;
}
# b.test.com → /home/www/b
server {
listen 80;
server_name b.test.com;
index index.html;
root /home/www/b/;
location /(self)/ {
deny all; # block access to /self
}
}2. Different URL paths mapped to different physical directories
server {
listen 80;
server_name ~^\d+\.\d+\.\d+\.\d+$; # match any IP address
index index.html index.htm;
root /home/lg/www/;
location /share {
root /home/lg/Downloads;
}
location ^~ /Videos {
root /home/lg/;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
allow all;
}
location ^~ /html5 {
root /home/lg/workspace/nodejs/;
index index.html index.htm;
}
location = /404.html {
root /usr/share/nginx/html;
}
}Directives autoindex_exact_size (default on) shows exact file sizes in bytes; setting it off displays rounded sizes (KB/MB/GB). autoindex_localtime (default off) shows file timestamps in GMT; turning it on shows server local time. allow all; permits all clients to access the location.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
