Setting Up Virtual Hosts and Server Blocks
Now that you understand Nginx's configuration structure, let's dive into one of its most powerful features: virtual hosts (called "server blocks" in Nginx). This allows you to host multiple websites on a single server.
Learning Goals
By the end of this lesson, you will be able to:
- Understand what server blocks are and why they're useful
- Create and configure multiple server blocks
- Set up domain-based and port-based virtual hosting
- Test and troubleshoot server block configurations
What Are Server Blocks?
Server blocks (often called virtual hosts in other web servers) enable you to run multiple websites on a single Nginx instance. Each server block defines how Nginx should handle requests for specific domains, IP addresses, or ports.
Think of server blocks as separate "virtual servers" living on the same physical machine. They're perfect for hosting multiple websites, staging environments, or microservices on one server.
Creating Your First Server Block
Let's start with a basic server block that responds to a domain name. Create a new configuration file in /etc/nginx/sites-available/:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
To enable this site, create a symbolic link in the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Multiple Server Blocks in Action
You can host multiple websites by creating separate server blocks. Here's a practical example with two different sites:
- Blog Site
- API Service
server {
listen 80;
server_name blog.example.com;
root /var/www/blog;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name api.example.com;
root /var/www/api;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
}
Port-Based Virtual Hosting
Sometimes you might want to serve different content on different ports. Here's how to set up port-based virtual hosting:
server {
listen 8080;
server_name localhost;
root /var/www/portaltest;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Testing and Applying Configuration
After creating server blocks, always test your configuration before applying changes:
# Test configuration syntax
sudo nginx -t
# If test passes, reload Nginx
sudo systemctl reload nginx
Never use systemctl restart nginx for configuration changes unless absolutely necessary. reload gracefully applies changes without dropping active connections.
Default Server Block
Nginx uses the first server block as the default when no matching server_name is found. You can explicitly define a default server:
server {
listen 80 default_server;
server_name _;
return 444; # Close connection without response
}
Common Pitfalls
- Missing symbolic links: Creating files in
sites-availablebut forgetting to link them insites-enabled - Syntax errors: Forgetting semicolons at the end of directives
- Permission issues: Web content not accessible by the Nginx user (usually
www-dataornginx) - DNS not configured: Server blocks work based on domain names - ensure DNS points to your server
- Port conflicts: Multiple server blocks trying to use the same port without proper
server_namedifferentiation
Summary
Server blocks are Nginx's powerful mechanism for hosting multiple websites on a single server. You learned how to:
- Create domain-based and port-based virtual hosts
- Enable sites using symbolic links
- Test and safely apply configuration changes
- Set up a default catch-all server
Quiz
Nginx Server Blocks and Configuration Management
What directory should you create server block configuration files in?