How to Use Nginx to Point to Different Hosted Folders in EC2
When you’re running an EC2 instance, you might want to host multiple websites or applications, each pointing to different folders. Nginx, a powerful web server, makes it simple to serve multiple sites on the same instance using a technique called “server blocks.” Server blocks allow you to specify different root directories for different domains or IP addresses.
In this post, we’ll walk you through how to set up Nginx to point to different hosted folders on your EC2 instance.
Why Use Nginx for Multiple Sites?
- Multiple Sites: Nginx allows you to serve different websites or applications from different folders.
- Lightweight and Efficient: Nginx is known for handling many concurrent connections, making it perfect for EC2 instances with limited resources.
- Cost-Effective: Hosting multiple sites on a single EC2 instance saves money on server costs.
Prerequisites
Before we begin, ensure that you have the following:
- An EC2 instance running, with a security group that allows HTTP (port 80) and HTTPS (port 443) traffic.
- Nginx installed on your EC2 instance. You can install Nginx on an Ubuntu-based EC2 instance using: bashCopy
sudo apt update sudo apt install nginx
- Access to the EC2 instance via SSH.
- Some websites or applications already uploaded to different folders (directories) on your EC2 instance.
Step 1: Create Folders for Your Websites
Let’s assume you want to host two websites:
- Site 1:
/var/www/site1
- Site 2:
/var/www/site2
Create the directories and add some test files to each one:
bashCopysudo mkdir -p /var/www/site1
sudo mkdir -p /var/www/site2
# Create a simple index.html for each site
echo "Welcome to Site 1!" | sudo tee /var/www/site1/index.html
echo "Welcome to Site 2!" | sudo tee /var/www/site2/index.html
Step 2: Create Server Blocks for Each Site
Now that you have the folders set up, you need to configure Nginx to point to these folders for different websites.
- Navigate to the Nginx sites-available directory: bashCopy
cd /etc/nginx/sites-available
- Create configuration files for each site. We’ll use
site1.conf
andsite2.conf
as examples: For Site 1: bashCopysudo nano /etc/nginx/sites-available/site1.conf
Add the following content: nginxCopyserver { listen 80; server_name site1.example.com; # Change to your domain or public IP root /var/www/site1; index index.html; location / { try_files $uri $uri/ =404; } }
For Site 2: bashCopysudo nano /etc/nginx/sites-available/site2.conf
Add the following content: nginxCopyserver { listen 80; server_name site2.example.com; # Change to your domain or public IP root /var/www/site2; index index.html; location / { try_files $uri $uri/ =404; } }
These configuration files tell Nginx to:
- Listen on port 80.
- Serve content from different directories for
site1.example.com
andsite2.example.com
.
Step 3: Enable the Sites in Nginx
To make these configurations live, you need to create symbolic links in the sites-enabled directory.
- Create a symbolic link for each site: bashCopy
sudo ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site2.conf /etc/nginx/sites-enabled/
- Check if the configuration is correct: bashCopy
sudo nginx -t
- Reload Nginx to apply the changes: bashCopy
sudo systemctl reload nginx
Step 4: Update Your DNS or Use EC2 Public IP
To access the sites, you can either:
- Point custom domains (e.g.,
site1.example.com
andsite2.example.com
) to the EC2 instance’s public IP. - Access using the public IP. If you don’t have a domain, you can use your EC2 instance’s public IP and modify the
server_name
to be the IP address in the Nginx configuration.
For example:
- Change
server_name site1.example.com;
toserver_name <Your-EC2-IP>;
for each site configuration.
Step 5: Test the Sites
Now that everything is configured, you should be able to access both sites through your browser:
- For Site 1: Visit
http://site1.example.com
(or the public IP). - For Site 2: Visit
http://site2.example.com
(or the public IP).
You should see:
- “Welcome to Site 1!” for Site 1.
- “Welcome to Site 2!” for Site 2.
Troubleshooting Tips
- Site Not Found: If you get a “404 Not Found” error, check that the directory path in the Nginx configuration matches the correct folder.
- Permission Issues: Ensure that the Nginx user (
www-data
) has proper permissions to read from the directories. bashCopysudo chown -R www-data:www-data /var/www/site1 sudo chown -R www-data:www-data /var/www/site2
- Nginx Error Logs: Check the Nginx error logs for more detailed information on issues: bashCopy
tail -f /var/log/nginx/error.log
Conclusion
By following these steps, you’ve successfully set up Nginx to serve multiple websites from different directories on a single EC2 instance. This is an efficient way to host different applications or sites on the same server, making your EC2 instance more cost-effective. You can repeat the process for more sites as needed!
Happy hosting! If you run into any issues or need further clarification, feel free to reach out!