Configuring Nginx to host multiple websites involves setting up server blocks, also known as virtual hosts. This guide will walk you through the process of configuring Nginx to host multiple websites on a single server.
Prerequisites
Before starting, ensure that you have:
- Nginx installed on your server.
- Websites’ content available in separate directories.
Step 1: Create Website Directories
Create separate directories for each website you want to host. For example, let’s create directories for website1.com
and website2.com
.
sudo mkdir -p /var/www/website1.com/html
sudo mkdir -p /var/www/website2.com/html
Step 2: Assign Ownership
Assign ownership of the website directories to the www-data
user, which is the default Nginx user.
sudo chown -R www-data:www-data /var/www/website1.com/html
sudo chown -R www-data:www-data /var/www/website2.com/html
Step 3: Create Sample HTML Pages
Create sample HTML pages for each website. Replace the content with your actual website content.
echo "Welcome to Website 1" | sudo tee /var/www/website1.com/html/index.html
echo "Welcome to Website 2" | sudo tee /var/www/website2.com/html/index.html
Step 4: Configure Nginx Server Blocks
Open the Nginx default configuration file for editing.
sudo nano /etc/nginx/nginx.conf
Within the http
block, add references to the server block configuration files.
http {
include /etc/nginx/sites-enabled/*;
# other configurations...
}
Step 5: Create Server Block Configuration Files
Create separate configuration files for each website in the /etc/nginx/sites-available/
directory.
For website1.com
:
sudo nano /etc/nginx/sites-available/website1.com
Paste the following code snippet into the website1.com
file you’ve just created
server {
listen 80;
server_name website1.com www.website1.com;
root /var/www/website1.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
For website2.com
:
sudo nano /etc/nginx/sites-available/website2.com
Similarly, paste the following code snippet into website2.com
file
server {
listen 80;
server_name website2.com www.website2.com;
root /var/www/website2.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 6: Create Symbolic Links
Create symbolic links to enable the server blocks.
sudo ln -s /etc/nginx/sites-available/website1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website2.com /etc/nginx/sites-enabled/
Step 7: Test Configuration and Reload Nginx
Test the Nginx configuration for syntax errors.
sudo nginx -t
If the test is successful, reload Nginx to apply the changes.
sudo systemctl reload nginx
Step 8: Configure DNS Records
Update your domain’s DNS records to point to your server’s IP address.
Now, you should be able to access website1.com
and website2.com
in your web browser.
Conclusion
Configuring Nginx to host multiple websites involves creating separate server blocks for each site. By following these steps, you can efficiently manage and serve multiple websites from a single Nginx server, providing a scalable and organized hosting solution.