Nginx, pronounced “engine x,” is an open-source, high-performance HTTP and reverse proxy server widely used for hosting some of the largest and most visited websites on the internet. It can serve as a standalone web server, reverse proxy, load balancer, or content cache, offering unparalleled flexibility and scalability.
One of its standout features is the ability to configure Nginx server blocks, also known as virtual hosts, which allow hosting multiple websites on a single server with distinct configurations for each domain.
In this article, we will provide a detailed, step-by-step guide on how to set up Nginx server blocks on Ubuntu, enabling you to manage multiple websites efficiently and customize their configurations as needed.
This comprehensive guide will cover everything from prerequisites to testing and verification of server blocks.
Prerequisites
Before proceeding with setting up Nginx server blocks, ensure the following:
- Domain Names: The domain names you plan to use must point to your server’s public IP address.
- Installed Nginx: Nginx should already be installed on your Ubuntu system. If not, follow this command to install it:
sudo apt update sudo apt install nginx -y
- User Privileges: You should have a root or a user account with
sudo
privileges.
Understanding Nginx Server Blocks
Server blocks in Nginx define settings for individual domains or websites hosted on a single server. These settings include the domain’s root directory, log file locations, SSL certificates, and specific configurations unique to that domain. Server blocks enable multiple domains to coexist without interference, offering each domain its own environment.
Each server block functions as a container for configuration directives. For instance, you can set unique document roots, configure separate security policies, or even enable specific performance optimizations for different domains. This flexibility is invaluable for managing multiple projects or clients on the same server.
How To Set Up Nginx Server Blocks on Ubuntu
Step 1: Create the Directory Structure
The document root is the directory where the files for a specific domain are stored. For example, we will set up directories for two domains: domain1.com
and domain2.com
.
1.1. Create Directories
Use the following commands to create directories for each domain:
sudo mkdir -p /var/www/domain1.com/public_html sudo mkdir -p /var/www/domain2.com/public_html
The -p
flag ensures that the entire directory structure is created if it doesn’t already exist.
1.2. Set Up a Sample HTML File
Create a sample index.html
file for each domain to test functionality. Here is an example for domain1.com
:
sudo nano /var/www/domain1.com/public_html/index.html
Add the following content:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Welcome to domain1.com</title> </head> <body> <h1>Success! Welcome to domain1.com</h1> </body> </html>
Repeat the same for domain2.com
by replacing the domain name in the content. This ensures that each domain has a unique and identifiable test page.
1.3. Set Ownership and Permissions
Ensure the Nginx user (www-data
) has ownership of the domain directories:
sudo chown -R www-data: /var/www/domain1.com sudo chown -R www-data: /var/www/domain2.com
This step prevents permission-related errors when Nginx tries to serve files from these directories.
Step 2: Create and Configure Nginx Server Blocks
Nginx’s server block configuration files are stored in the /etc/nginx/sites-available/
directory and activated via symbolic links in /etc/nginx/sites-enabled/
.
2.1. Create a Configuration File for Each Domain
Create a new server block file for domain1.com
:
sudo nano /etc/nginx/sites-available/domain1.com
Add the following content:
server { listen 80; server_name domain1.com www.domain1.com; root /var/www/domain1.com/public_html; index index.html; access_log /var/log/nginx/domain1.com.access.log; error_log /var/log/nginx/domain1.com.error.log; }
Repeat the same for domain2.com
by replacing domain-specific details. These configuration files tell Nginx how to handle requests for each domain, including where to find the files and where to log access and errors.
2.2. Enable Nginx Server Blocks
Create symbolic links to activate the server blocks:
sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/
This step effectively “enables” the server block configurations by linking them to the active configuration directory.
Step 3: Test and Restart Nginx
3.1. Test Configuration Syntax
Run the following command to ensure there are no syntax errors in the configuration:
sudo nginx -t
If there are no issues, you will see output similar to this:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
3.2. Restart Nginx
Apply the changes by restarting Nginx:
sudo systemctl restart nginx
Restarting ensures that the new configurations are loaded and active.
Step 4: Verify Nginx Server Blocks Functionality
Open a web browser and navigate to http://domain1.com
and http://domain2.com
. You should see the respective sample HTML pages confirming the server blocks are working correctly.
If the pages do not load as expected:
- Ensure the domain names are correctly pointing to your server’s IP address.
- Verify DNS propagation if changes were recently made.
- Check the Nginx error log for any issues:
sudo tail -f /var/log/nginx/error.log
Step 5: Add SSL Certificates (Optional but Recommended)
For secure connections, it’s recommended to install SSL certificates. The easiest way to achieve this is by using Let’s Encrypt.
5.1. Install Certbot
Install Certbot, a tool for obtaining Let’s Encrypt certificates:
sudo apt install certbot python3-certbot-nginx -y
5.2. Obtain and Configure SSL Certificates
Run Certbot to obtain and configure certificates for each domain:
sudo certbot --nginx -d domain1.com -d www.domain1.com
Follow the prompts to complete the process. Certbot will automatically update the Nginx configuration to use SSL.
Repeat the process for domain2.com
.
5.3. Test SSL Configuration
After obtaining the certificates, restart Nginx:
sudo systemctl reload nginx
Verify HTTPS functionality by visiting https://domain1.com
and https://domain2.com
.
5.4. Automate Certificate Renewal
Let’s Encrypt certificates expire every 90 days. Automate the renewal process by adding the following to your crontab:
sudo crontab -e
Add this line to renew certificates daily:
0 3 * * * certbot renew --quiet
This ensures uninterrupted SSL coverage.
Conclusion
By following this guide, you’ve successfully set up Nginx server blocks on Ubuntu, allowing you to host multiple websites on a single server. Each domain is isolated with its own configuration, document root, and logs. You also learned how to secure your domains with SSL certificates.
Feel free to expand your setup by adding more domains or customizing server block configurations to suit specific requirements. With Nginx’s powerful capabilities, you’re now equipped to manage a robust, scalable web hosting environment.
For more details, visit the official Nginx documentation. It provides comprehensive guides, configuration examples, and best practices to help you optimize your setup.