<#include '/gkmsid/9058821'>
Load balancing is a system that distributes network or application traffic across a number of servers.
Load balancing is a system that distributes network or application traffic across a number of servers.
For example, you have your primary NGINX web server and want to distribute the load across two or three other NGINX servers; by doing this, you can ensure that no matter how much traffic you're getting, more than one server can take care of the load. Clearly, your web sites (on each NGINX server) will have to be set up identically for this to work (otherwise, you'd be distributing traffic across differing sites).
With that said, I'm going to show you just how easy it is to setup NGINX for load balancing. I'll be demonstrating on Ubuntu Server 16.04, with three servers at IP addresses:
- Server 1 - 192.168.1.232
- Server 2 - 192.168.1.233
- Server 3 - 192.168.1.234
You will alter the configuration to meet your specific IP address scheme and needs (say you want to distribute across more servers). I will assume you already have NGINX up and running on each server.
Initial setup
I'm going with absolute basics here. To demonstrate how this works, I will create a new index.html in each server's /var/www/html directory, with the content:
NGINX SERVER Y
Where Y is either 1, 2, 3 (depending on which server the file is on).
With that in place, let's configure NGINX.
Configuration
We're going to create a new file to configure load balancing on SERVER 1 only. Issue the command sudo nano /etc/nginx/conf.d/load-balancer.conf. In that file, copy the following contents:
# Define which servers to include in the load balancing scheme. upstream backend { server 192.168.1.232; server 192.168.1.233; server 192.168.1.234; } # This server accepts all traffic to port 80 and passes it to the upstream. server { listen 80; location / { proxy_pass http://backend; } }
NOTE: Make sure to configure the above file with your IP address scheme. You can also add as many servers as necessary for load balancing.
Save and close that file.
On our Ubuntu system, we have to remove the symbolic link to default, in the /etc/nginx/sites-enabled folder. Do this with the command:
sudo rm /etc/nginx/sites-enabled/default
Restart NGINX with the command:
sudo systemctl restart nginx
Testing
If you point a browser to the IP address of Server 1 (in our case 192.168.1.232), the load balancing will begin to round-robin requests to the NGINX servers at the other addresses, configured within the load-balancer.conf file. Your first call to Server 1's IP address will take you to Server 2 and the next to Server 3 (and so on).
The default load balancing system is round robin. This can cause problems with session persistence. If your web sites/applications require users to be directed to the same server used for their previous session, IP hashing must be used. With IP hashing in place, a user will always be directed to the same server (so long as the server is available).
To setup IP hashing, open up the /etc/nginx/conf.d/load-balancer.conf file and add the line ip_hash; below upstream backend {. That new file would look like:
# Define which servers to include in the load balancing scheme. upstream backend { ip_hash; server 192.168.1.232; server 192.168.1.233; server 192.168.1.234; } # This server accepts all traffic to port 80 and passes it to the upstream. server { listen 80; location / { proxy_pass http://backend; } }
Save and close that file. Restart NGINX with the command:
sudo systemctl restart nginx
Now if you point your browser to the Server 1 IP address and hit refresh, you will be sent to the same server.
Comments
Post a Comment