This repository demonstrates setting up an Nginx load balancer to distribute traffic between two static websites deployed on separate servers.
- Three servers: Server A, Server B, Server C
- Nginx installed on all three servers
Ensure all servers are set up and accessible via SSH.
-
Install Nginx:
sudo apt update sudo apt install nginx
-
Configure websites:
-
Server A:
sudo mkdir -p /var/www/site1 echo "Server A" | sudo tee /var/www/site1/index.html
-
Server B:
sudo mkdir -p /var/www/site2 echo "Server B with a change" | sudo tee /var/www/site2/index.html
-
-
Install Nginx:
sudo apt update sudo apt install nginx
-
Configure Nginx as a load balancer:
-
Edit nginx.conf:
sudo nano /etc/nginx/nginx.conf
-
-
Add the following upstream block and server block within the http block:
upstream my_sites { server <ServerA_IP>; server <ServerB_IP>; } server { listen 80; server_name example.com; location / { proxy_pass http://my_sites; } }
Replace
<ServerA_IP>
and<ServerB_IP>
with the actual IP addresses of Server A and Server B.
Add Server C's IP address (load balancer) to the DNS A record of your domain.
Access the website (example.com). With each reload, you should see different content from Server A or Server B, indicating load balancing is functional.
Experiment with different Nginx load balancing algorithms and options within the nginx.conf file.
Learn about Layer 7 (L7) load balancing for more sophisticated routing decisions based on application-level data.
To ensure servers are properly set up and accessible via SSH, follow these steps:
-
Gather necessary credentials:
- IP addresses of each server (Server A, Server B, Server C).
- Username and password or SSH key for accessing each server.
- Open a terminal or SSH client on your local machine.
-
Use the following command to connect via SSH to Server A:
ssh username@ServerA_IP
Replace
username
with your server username andServerA_IP
with the IP address of Server A. -
If prompted, enter the password or SSH key passphrase to authenticate.
Once connected to Server A, ensure access for executing commands. Perform basic checks like checking server status and available disk space to ensure proper functionality.
Follow the same process as in Step 3 for Server B and Server C, replacing the IP addresses accordingly. Verify connections to each server and perform basic checks to ensure accessibility and functionality.
For enhanced security, consider implementing SSH key-based authentication:
- Generate SSH keys locally using
ssh-keygen
. - Copy the public key generated (
id_rsa.pub
) to each server's~/.ssh/authorized_keys
file.
If servers have firewalls enabled, ensure that SSH ports (usually port 22) are open to allow SSH connections. Update firewall rules using the appropriate commands or firewall management tools.
Maintain a record of essential server information for future reference, including:
- Server IP addresses
- SSH usernames
- Any other necessary configuration details
By following these steps, you'll ensure that all servers are properly set up, accessible via SSH, and ready for further configuration and setup.
To direct traffic to the load balancer (Server C) using your domain, follow these steps:
- Log in to your domain registrar's website or access your DNS management console.
- Navigate to the section where you manage DNS settings for your domain.
- Find the option to add a new DNS record (usually labeled as "Add Record," "Add DNS," etc.).
- Select the record type as "A" (Address).
- In the "Name" or "Host" field, add the desired subdomain or leave it blank to use the root domain.
- In the "Value," "IPv4 Address," or similar field, enter the IP address of Server C (the load balancer).
- Set the TTL (Time to Live) based on your preference or leave it as default.
- Save the new A record configuration.
- Wait for DNS propagation (it may take some time depending on your DNS provider).
- Verify the changes by pinging the domain or accessing the domain in a web browser to ensure it points to the load balancer's IP address (Server C).
This will direct incoming traffic for your domain to the load balancer, allowing it to distribute the traffic between Server A and Server B.
- Nginx Documentation
- Understanding Nginx Load Balancing
- DigitalOcean: How To Set Up Nginx Load Balancing
This project is licensed under the MIT License