setup nginx load balancer

How to Setup NGINX Load Balancer

NGINX Load Balancer allows websites to handle high traffic load by distributing them among a set of backend servers. Also a load balancer acts as a reverse proxy and protects back end servers from being exposed to public. Here’s how to setup NGINX load balancer for your website.

 

How to Setup NGINX Load Balancer

Here’s a step by step configuration of how to setup NGINX load balancer. It is very easy to setup NGINX load balancing with the help of proxy_pass and upstream directives.

 

1. Open NGINX server configuration file

Open NGINX configuration file using a text editor.

If you are using NGINX’s main configuration file nginx.conf, without virtual hosts, then open terminal and run the following command

$ sudo vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g www.website.com), such as /etc/nginx/sites-enabled/website.conf then open its configuration with the following command

$ sudo vi /etc/nginx/sites-enabled/website.conf

 

Bonus Read : How to Redirect Subfolder to Subdomain in NGINX

 

2. Setup NGINX Load balancer

Let’s say you have 3 backend servers at IPs 10.1.2.1, 10.1.2.2, 10.1.2.3, each running on port 80

So first we add an upstream block inside http block, for these backend servers.

http{
...
  upstream backend {
    server 10.1.2.1;
    server 10.1.2.2;
    server 10.1.2.3;
 }
...
}

You can name this upstream block anything you want. We have named it as backend

 

Below this upstream block, we need to add our server block that directs requests to these backend servers, by referring to the upstream block in proxy_pass directive.

server {
    listen 80;
    server_name www.website.com;
    location / {
      proxy_pass http://backend;
    }
  }

Putting both blocks together, here’s your NGINX load balancer configuration setup.

 

Bonus Read : How to Redirect Subdomain to Folder in NGINX

 

http {
  upstream myproject {
    server 10.1.2.1;
    server 10.1.2.2;
    server 10.1.2.3;
  }

  server {
    listen 80;
    server_name www.domain.com;
    location / {
      proxy_pass http://myproject;
    }
  }
}

 

By default, NGINX load balancer distributes incoming requests among its backend servers in a round-robin manner. If you want more requests to be sent to a specific backend server, you can assign a weight to it. Here’s an example where the 1st server gets 3 times more requests than each of the other backend servers.

 

http{
...
  upstream backend {
    server 10.1.2.1 weight=3;
    server 10.1.2.2;
    server 10.1.2.3;
 }
...
}

Similarly, if you want to change the load balancing method, you can specify it at the beginning of your upstream block, as shown below.

 

Using least connection load balancing method

http{
...
  upstream backend {
    least_conn;
    server 10.1.2.1;
    server 10.1.2.2;
    server 10.1.2.3;
 }
...
}

 

Using ip_hash load balancing method

http{
...
  upstream backend {
    ip_hash;
    server 10.1.2.1;
    server 10.1.2.2;
    server 10.1.2.3;
 }
...
}

 

If you don’t specify any method, NGINX will use the round-robin algorithm.

 

Bonus Read : How to Block IP Address in NGINX

 

3. Restart NGINX

Finally, run the following command to check syntax of your updated config file.

$ sudo nginx -t

 

 

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

 

That’s it! Hopefully the above tutorial will help you setup NGINX load balancer for your website.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!