Host header, matching the server_name directive within each server block (often called virtual hosts). By the end of this tutorial, you’ll have two separate site roots and configurations, each serving its own content.
Table of Contents
- Remove the Default Site
- Create Site Configurations
- Set Up Document Roots and Index Files
- Enable Sites and Reload Nginx
- Verify Virtual Host Routing
- Optional: Single Combined Config
- Best Practices
- References
1. Remove the Default Site
Before adding your own configurations, disable the default Nginx welcome page.Removing the
default site will cause localhost to refuse connections until you enable at least one server block.2. Create Site Configurations
We’ll copy the default config twice—once for each domain—and then adjust theroot and server_name.
2.1 example1.com
/etc/nginx/sites-available/example1:
2.2 example2.com
/etc/nginx/sites-available/example2:
3. Set Up Document Roots and Index Files
Create directories for each site and add a simpleindex.html:
4. Enable Sites and Reload Nginx
Create symbolic links insites-enabled, then test and reload.
5. Verify Virtual Host Routing
Simulate browser requests usingcurl with custom Host headers:
6. Optional: Single Combined Config
You may merge both server blocks into one file, though we recommend keeping them separate.Keeping separate files in
sites-available makes it simpler to enable/disable individual domains without affecting others.7. Best Practices
| Practice | Benefit |
|---|---|
| Separate config per domain | Isolate changes and minimize risk to other sites. |
| Use descriptive file names | Quickly identify which domain each configuration serves. |
| Regularly test configuration | Run nginx -t before reloading to catch syntax errors. |
| Limit server-wide directives | Keep domain-specific settings in individual server blocks, not in global nginx.conf. |