In the internet, we often see multiple websites running on a single server. For example, a company might have an official website, a blog, and a product showcase site simultaneously. If each website is hosted on a separate server, the cost would be extremely high. However, Nginx’s virtual host feature allows us to easily deploy multiple websites on a single server, making each website appear as if it were running independently on its own server.
一、What is a Virtual Host?¶
In simple terms, a virtual host is a technique that enables a single physical server to simulate multiple “virtual servers.” Each virtual host can be independently configured with its own domain name, website files, and access rules without interfering with others.
Nginx implements virtual hosts in three main ways:
- Domain-based: The most common method, where different domains access different websites (e.g., www.abc.com and www.xyz.com).
- Port-based: Different ports are used to distinguish websites (e.g., www.abc.com:80 and www.abc.com:8080).
- IP-based: Different server IP addresses are used to distinguish websites (requires the server to have multiple IPs).
二、Prerequisites¶
Before configuration, ensure the following:
1. Nginx Installed: If not installed, use the following command (Ubuntu example):
sudo apt update && sudo apt install nginx
Start Nginx after installation: sudo systemctl start nginx, and enable auto-start: sudo systemctl enable nginx.
-
Website Content Prepared: Create a directory and index file (e.g.,
index.html) for each virtual host.
Example:
- Site 1:/var/www/site1withindex.htmlcontent:Welcome to Site 1!
- Site 2:/var/www/site2withindex.htmlcontent:Welcome to Site 2! -
Test Domain (Optional): For real domains, ensure they resolve to the server IP (e.g., set A records for
site1.comandsite2.comto point to1.2.3.4).
三、Domain-based Virtual Host (Most Common)¶
Assume the server IP is 1.2.3.4, and we need to deploy two websites: site1.com and site2.com.
Step 1: Create Nginx Configuration File¶
Nginx configuration files are typically in /etc/nginx/. For Ubuntu/Debian, create a new file in sites-available:
sudo nano /etc/nginx/sites-available/site1.com
Step 2: Configure First Website (site1.com)¶
Add the following content to the configuration file:
server {
listen 80; # Listen on port 80 (HTTP default port)
server_name site1.com www.site1.com; # Match domains (space-separated)
root /var/www/site1; # Website root directory
index index.html; # Default index file
# Optional: Access and error logs
access_log /var/log/nginx/site1_access.log;
error_log /var/log/nginx/site1_error.log;
}
Step 3: Configure Second Website (site2.com)¶
Create another configuration file site2.com:
server {
listen 80;
server_name site2.com www.site2.com;
root /var/www/site2;
index index.html;
access_log /var/log/nginx/site2_access.log;
error_log /var/log/nginx/site2_error.log;
}
Step 4: Enable Configurations and Test¶
- Symlink the configuration files to
sites-enabled(so Nginx loads them):
sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/
- Test configuration for syntax errors:
sudo nginx -t
If it shows syntax is ok and test is successful, the configuration is correct.
- Restart Nginx to apply changes:
sudo systemctl restart nginx
Step 5: Verify Access¶
Visit site1.com and site2.com in a browser; each should display its respective homepage content.
四、Port-based Virtual Host¶
If additional domains are unavailable, use different ports to distinguish websites (e.g., 80 and 8080).
Example: Two Websites on Port 80 and 8080¶
- Website 1 (Port 80):
server {
listen 80;
server_name _; # Match any domain
root /var/www/site1;
index index.html;
}
- Website 2 (Port 8080):
server {
listen 8080;
server_name _;
root /var/www/site2;
index index.html;
}
- After restarting Nginx, access:
-http://ServerIP(Port 80)
-http://ServerIP:8080(Port 8080)
五、IP-based Virtual Host (Requires Multiple IPs)¶
If the server has multiple IPs (e.g., 1.2.3.4 and 1.2.3.5), use distinct IPs to separate websites:
# Site 1 bound to IP 1.2.3.4
server {
listen 1.2.3.4:80;
server_name site1.com;
root /var/www/site1;
}
# Site 2 bound to IP 1.2.3.5
server {
listen 1.2.3.5:80;
server_name site2.com;
root /var/www/site2;
}
六、Common Issues and Solutions¶
-
404 or Blank Page:
- Check directory permissions:sudo chown -R www-data:www-data /var/www/site1(Ubuntu useswww-dataas default user).
- Verifyrootpath and existence ofindex.html. -
Nginx Fails to Start:
- Check configuration syntax:sudo nginx -t.
- View logs:tail -n 100 /var/log/nginx/error.log. -
Domain Resolution Not Working:
- Ensure A records point to the server IP. Test withping site1.com.
七、Summary¶
Nginx’s virtual host feature allows hosting multiple websites on a single server. The core is configuring distinct server blocks with different domains, ports, or IPs, each with independent root directories and access rules. Beginners should start with domain-based configuration, then practice port/IP-based methods. Always test configurations with nginx -t and restart Nginx after changes.