ThisCalculator

Everyday

Load Balancing & Failover Across Two VPS Servers

Updated · Standard formula, shown below

This guide walks you through setting up load balancing and failover across two VPS servers, so your web application stays online even if one server goes down. You’ll configure a primary server to distribute traffic between two backend servers and automatically redirect requests if one fails. Before starting, you need two VPS servers (both with a public IP), a domain name pointing to the primary server, and root or sudo access to all servers.

What You’ll Need

  • Two VPS servers running Linux (Ubuntu 22.04 or similar) with a public IP each.
  • A domain name (or subdomain) that you can point to the primary server’s IP.
  • SSH access to both servers (root or a user with sudo privileges).
  • Your web application installed on both backend servers (or at least a test page).
  • Basic familiarity with the command line and editing config files.

Step-by-Step

  1. Identify your servers. Decide which server will be the load balancer (the front-end) and which two will be the backends. In this guide, we’ll call them lb1 (load balancer), web1, and web2. Note their IP addresses.
  2. Install Nginx on the load balancer. SSH into lb1 and run: sudo apt update && sudo apt install nginx -y. Nginx is a popular, lightweight web server that can act as a reverse proxy and load balancer.
  3. Configure the upstream block. Edit the Nginx config: sudo nano /etc/nginx/sites-available/loadbalancer. Add the following, replacing web1_ip and web2_ip with your actual backend IPs:
    upstream backend {
        server web1_ip;
        server web2_ip;
    }
    
    server {
        listen 80;
        server_name yourdomain.com;
    
        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    Save and exit.

  4. Enable the site and test. Create a symlink: sudo ln -s /etc/nginx/sites-available/loadbalancer /etc/nginx/sites-enabled/. Then test the config: sudo nginx -t. If it says OK, reload Nginx: sudo systemctl reload nginx.
  5. Point your domain. In your DNS provider’s control panel, create an A record for yourdomain.com (and www if needed) pointing to lb1‘s IP address. Wait for DNS to propagate (a few minutes to a few hours).
  6. Set up failover with health checks. To automatically fail over when a backend goes down, add a health check to the upstream block. Edit the config again and add max_fails=3 fail_timeout=30s to each server line:
    upstream backend {
        server web1_ip max_fails=3 fail_timeout=30s;
        server web2_ip max_fails=3 fail_timeout=30s;
    }

    Reload Nginx: sudo systemctl reload nginx. Now if a backend fails to respond 3 times within 30 seconds, Nginx marks it as down and sends all traffic to the other one.

  7. Test the failover. Visit your domain in a browser. You should see your application. Then, on web1, stop the web server: sudo systemctl stop nginx (or your app’s server). Refresh the page – it should still load, now served by web2. Restart web1 afterward.
  8. Add session persistence (optional). If your app uses sessions, add ip_hash; at the top of the upstream block to ensure a user always hits the same backend. This prevents session loss but reduces load balancing fairness.
  9. Secure with HTTPS (recommended). Install Certbot on lb1: sudo apt install certbot python3-certbot-nginx -y, then run sudo certbot --nginx -d yourdomain.com. Follow the prompts. This will automatically configure SSL and renewals.

Common Mistakes

  • Forgetting to open firewall ports. If you have a firewall (like UFW), allow traffic on ports 80 and 443 on the load balancer, and on the backend servers allow traffic from the load balancer’s IP on the port your app listens on (e.g., 8080).
  • Using the same port for backends. If both backends run on port 80, that’s fine, but ensure the load balancer is not also trying to serve content on port 80 for the same domain.
  • Not testing health checks. Without proper health checks, Nginx will still send traffic to a dead server, causing intermittent errors. Always test by stopping a backend.
  • DNS pointing to the wrong IP. Double-check that your domain points to the load balancer’s IP, not the backend IPs.

Tips

  • Use a dedicated port for backend servers (e.g., 8080) to avoid conflicts with the load balancer’s own web server.
  • Keep your backend servers identical (same code, same config) to avoid inconsistent behavior.
  • Monitor your servers with a tool like UptimeRobot or a simple cron job that pings the domain.
  • For more advanced load balancing (e.g., weighted distribution), add weight=2 to a server line in the upstream block.

Troubleshooting

  • Page not loading at all. Check that Nginx is running on the load balancer: sudo systemctl status nginx. Check the error log: sudo tail -f /var/log/nginx/error.log. Also verify DNS resolution: dig yourdomain.com (should show lb1’s IP).
  • 502 Bad Gateway. This means Nginx can’t reach the backends. Check that the backend web servers are running and that the IPs in the upstream block are correct. Also check the backend’s firewall.
  • Failover not working. If you stop a backend and the site goes down, your health check may be misconfigured. Ensure max_fails and fail_timeout are set, and that the backend actually fails to respond (not just returns a 500 error).
  • SSL certificate errors. If you installed HTTPS, make sure the domain resolves to the load balancer before running Certbot. Also check that port 443 is open.

Frequently Asked Questions

Can I use Apache instead of Nginx for load balancing?

Yes, Apache can do load balancing with the mod_proxy_balancer module, but Nginx is lighter and more commonly used for this purpose.

What if my backends are on different ports?

That’s fine. Just specify the port in the upstream server line, e.g., server web1_ip:8080;.

How do I know which backend is serving my request?

You can add a custom header in the Nginx config, like add_header X-Backend $upstream_addr; in the location block, then inspect the response headers in your browser’s dev tools.

Does this work with WebSockets?

Yes, but you need to add the Upgrade and Connection headers in the proxy settings. Add proxy_http_version 1.1; and proxy_set_header Upgrade $http_upgrade; and proxy_set_header Connection "upgrade"; in the location block.

Can I add more than two backend servers later?

Absolutely. Just add more server lines to the upstream block and reload Nginx.

What if my backend servers have dynamic IPs?

Use hostnames instead of IPs in the upstream block, but ensure the hostnames resolve correctly on the load balancer. For cloud environments, consider using a cloud load balancer service instead.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.