Nginx is a powerful web server that uses a non-threaded, event-driven architecture which enables it to handle thousands of concurrent connections with minimal resource usage. One of its key features is the ability to host multiple websites (virtual hosts) on a single server. In this tutorial, we'll show you how to configure Nginx virtual hosts on Ubuntu Linux.

What are Virtual Hosts?

Virtual hosts allow you to run multiple websites on a single server. Each virtual host has its own configuration file that defines how Nginx should handle requests for that particular domain. This is essential for web hosting providers and developers who need to host multiple websites on a single VPS or dedicated server.

Prerequisites

  • Ubuntu 16.04/18.04/20.04 server
  • Root or sudo privileges
  • Nginx installed (sudo apt install nginx)
  • Domain names pointing to your server IP

Nginx Configuration Structure

Nginx configuration files are located in:

/etc/nginx/nginx.conf           # Main configuration file
/etc/nginx/sites-available/    # Available site configurations
/etc/nginx/sites-enabled/      # Enabled site configurations (symlinks)

Step 1: Create Virtual Host Configuration

Create a new configuration file for your domain in the sites-available directory:

sudo nano /etc/nginx/sites-available/example.com

Add the following configuration:

server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    
    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

Step 2: Create Web Directory

Create the document root directory for your website:

sudo mkdir -p /var/www/example.com/html

Set proper permissions:

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Create a test HTML file:

sudo nano /var/www/example.com/html/index.html

Add basic HTML content:

<html>
    <head>
        <title>Welcome to Example.com!</title>
    </head>
    <body>
        <h1>Success! The example.com virtual host is working!</h1>
    </body>
</html>

Step 3: Enable Virtual Host

Create a symbolic link from sites-available to sites-enabled:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Step 4: Test Configuration

Test your Nginx configuration for syntax errors:

sudo nginx -t

If successful, you should see:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 5: Restart Nginx

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Configuring Multiple Domains

To host multiple websites, simply repeat the process for each domain:

# Create config for second domain
sudo nano /etc/nginx/sites-available/seconddomain.com

# Enable it
sudo ln -s /etc/nginx/sites-available/seconddomain.com /etc/nginx/sites-enabled/

# Test and restart
sudo nginx -t
sudo systemctl restart nginx

Troubleshooting

Common issues and solutions:

  • Permission denied errors: Check directory permissions with ls -la /var/www/
  • 404 errors: Verify the root directory path in your configuration
  • Configuration syntax errors: Use sudo nginx -t to check syntax
  • Port already in use: Check if Apache or another service is using port 80: sudo netstat -tulpn | grep :80

How to Disable a Virtual Host

To disable a virtual host without deleting its configuration:

sudo rm /etc/nginx/sites-enabled/example.com
sudo nginx -t
sudo systemctl reload nginx

Conclusion

You have successfully configured Nginx virtual hosts. This setup allows you to host multiple websites on a single server efficiently. For production environments, consider adding SSL/TLS certificates, security headers, and caching configurations to optimize performance and security.