In this project, we will configure an Nginx Load Balancer solution and ensure secure connections using SSL/TLS certificates.
We’ll explore:
- Configuring Nginx as a Load Balancer.
- Registering a domain name and configuring secure connections using SSL/TLS.
When data moves between a client (browser) and a web server over the internet, it can pass through multiple network devices. If the data isn't encrypted, it can be intercepted via Man-In-The-Middle (MITM) attacks. Using SSL/TLS protects against this by encrypting the session between the browser and web server.
-
How to configure Nginx as a Load Balancer.
-
How to secure your web solution using SSL/TLS certificates.
This project consists of two parts:
- Configure Nginx as a Load Balancer
- Register a domain and configure SSL/TLS certificates
-
Create an EC2 VM based on Ubuntu Server 24.04 LTS, and name it
Nginx-lb
. -
Update the
/etc/hosts
file for local DNS with the web servers' names (e.g.,Web1
,Web2
) and their local IP addresses. -
Install and Configure Nginx as a Load Balancer.
- Update the system and install Nginx:
sudo apt update sudo apt install nginx
- Update the system and install Nginx:
-
Open the Nginx configuration file:
sudo vi /etc/nginx/nginx.conf
-
Insert the following configuration into the
http
section:upstream myproject { server Web1 weight=5; server Web2 weight=5; } server { listen 80; server_name www.domain.com; location / { proxy_pass http://myproject; } }
-
Comment out this line:
# include /etc/nginx/sites-enabled/*;
-
Restart Nginx to apply the changes:
sudo systemctl restart nginx sudo systemctl status nginx
-
Register a domain name using any domain registrar (e.g., GoDaddy, Bluehost). For this project I used Qservers.ng
-
Assign an Elastic IP to your Nginx server and associate your domain name with this Elastic IP.
- Follow this guide to allocate an Elastic IP and associate it with your EC2 instance.
- Follow this guide to allocate an Elastic IP and associate it with your EC2 instance.
-
Update the A record in your domain registrar to point to the Elastic IP of your Nginx server.
-
Update your Nginx configuration file:
sudo vi /etc/nginx/nginx.conf
-
Change the
server_name
value to your new domain:server_name www.<your-domain-name>.com;
-
Restart Nginx to apply the changes:
sudo systemctl restart nginx
-
Ensure the
snapd
service is active:sudo systemctl status snapd
-
Install Certbot:
sudo snap install --classic certbot
-
Request an SSL certificate:
sudo ln -s /snap/bin/certbot /usr/bin/certbot sudo certbot --nginx
-
Test secured access to your website by visiting
https://<your-domain-name>.com
.
-
Test the certificate renewal command:
sudo certbot renew --dry-run
-
Set up a cron job to renew the SSL certificate automatically:
crontab -e
-
Add the following line to schedule certificate renewal twice a day:
* */12 * * * root /usr/bin/certbot renew > /dev/null 2>&1
Solution:
- Connect to the web server on terminal, and run the command to check httpd status and start it.
sudo systemctl status httpd
sudo systemctl start httpd
You have successfully: