July 10, 2017

Load Balancer Setup

We move onto configuring the load balancer to bounce traffic between our two application servers.

Previous: Server Setup

We've configured the application servers, but we have not configured the load balancer.

Luckily this is a pretty simple configuration. We'll once again edit the /etc/nginx/sites-available/default file on the load balancer server.

upstream app {
    server 172.31.9.200:80;
    server 172.31.0.30:80;
}

server {
    listen 80 default_server;

    server_name lb.serversforhackers.com;

    charset utf-8;

    location / {
        include proxy_params;
        proxy_pass http://app;
        proxy_redirect off;

        # Handle Web Socket connections
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

The upstream section gets the private IP addresses of the application servers, so we can communicate over the faster local network.

We use a server_name here of a real domain I've setup to point to the load balancer instance.

Finally, we include the proxy_params file, which sets some headers (X-Forwarded-*, X-Real-Ip, Host). Then we pass off the http request to the upstream servers. Nginx will round-robin requests between the two servers configured in the upstream.

Let's see if this works:

# Test the config
sudo nginx -t

# Reload the config
sudo service nginx reload

All Topics