Skip to main content

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.

tip

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/:

/etc/nginx/sites-available/example.com
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:

Enable the site
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:

/etc/nginx/sites-available/blog.example.com
server {
listen 80;
server_name blog.example.com;

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

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

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:

/etc/nginx/sites-available/port-8080
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 and reload Nginx
# Test configuration syntax
sudo nginx -t

# If test passes, reload Nginx
sudo systemctl reload nginx
warning

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:

Default catch-all server
server {
listen 80 default_server;
server_name _;

return 444; # Close connection without response
}

Common Pitfalls

  • Missing symbolic links: Creating files in sites-available but forgetting to link them in sites-enabled
  • Syntax errors: Forgetting semicolons at the end of directives
  • Permission issues: Web content not accessible by the Nginx user (usually www-data or nginx)
  • 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_name differentiation

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?

Question 1/5