Introduction
NGINX is a powerful, high-performance web server often used as a reverse proxy, load balancer, and HTTP cache. Setting up NGINX as a reverse proxy allows you to host multiple websites on a single server efficiently. In this guide, you’ll learn how to configure NGINX as a reverse proxy for multiple domains, complete with detailed commands and explanations.
Prerequisites
Before you start, ensure you have:
- An Ubuntu 22.04 server
- Root or sudo privileges
- Two or more domain names pointed to your server’s IP
Step 1: Install NGINX
First, update your package list and install NGINX:
sudo apt update
sudo apt install nginx -y
Once installed, verify that NGINX is running:
sudo systemctl status nginx
If it’s not running, start and enable the service:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Configure the Firewall
Allow NGINX traffic through the UFW firewall to enable HTTP and HTTPS access:
sudo ufw allow ‘Nginx Full’
Then, verify the firewall status:
sudo ufw status
Step 3: Set Up Directories for Each Website
Create directories for each website you plan to host:
sudo mkdir -p /var/www/site1.com/html
sudo mkdir -p /var/www/site2.com/html
Set the correct permissions to allow NGINX to access these files:
sudo chown -R $USER:$USER /var/www/site1.com/html
sudo chown -R $USER:$USER /var/www/site2.com/html
And assign proper read permissions:
sudo chmod -R 755 /var/www
Step 4: Create Sample Web Pages
For testing, create simple HTML pages for both sites:
echo ‘<h1>Welcome to Site1.com!</h1>’ | sudo tee /var/www/site1.com/html/index.html
echo ‘<h1>Welcome to Site2.com!</h1>’ | sudo tee /var/www/site2.com/html/index.html
Step 5: Configure NGINX Server Blocks
NGINX uses server blocks (similar to Apache’s virtual hosts) to manage multiple domains. Create configuration files for each domain:
For site1.com:
sudo nano /etc/nginx/sites-available/site1.com
Add the following configuration:
server {
listen 80;
server_name site1.com www.site1.com;
root /var/www/site1.com/html;
index index.html;location / {
try_files $uri $uri/ =404;
}
}
For site2.com:
sudo nano /etc/nginx/sites-available/site2.com
And add:
server {
listen 80;
server_name site2.com www.site2.com;
root /var/www/site2.com/html;
index index.html;location / {
try_files $uri $uri/ =404;
}
}
Step 6: Enable Server Blocks
Enable both sites by creating symbolic links to the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/
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 7: Test Your Configuration
Open your browser and visit http://site1.com
and http://site2.com
. You should see the respective welcome messages.
Optional: Enabling HTTPS with Let’s Encrypt
For better security, enable HTTPS using Certbot:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot –nginx -d site1.com -d www.site1.com
sudo certbot –nginx -d site2.com -d www.site2.com
Verify automatic renewal:
sudo certbot renew –dry-run
Conclusion
Congratulations! You’ve successfully configured NGINX as a reverse proxy to host multiple websites on a single server. This setup is highly scalable and can be extended to support additional domains as needed. Make sure to regularly update your server and NGINX configuration for optimal security and performance.