How To Set Up Nginx Load Balancing

nginx load balancing

Load balancing is a very useful technique to distribute the incoming network traffic across a number servers. With this technique you can reduce the resource usage, lower the response time and avoid server overload. In this tutorial, we will guide you through the steps of configuring Nginx load balancing on a Linux VPS.

Nginx Load Balancing is one of the most efficient options available to achieve full application redundancy, and it is relatively easy and quick to setup. We will configure Nginx load balancing using the Round robin mechanism. This way it will forward all requests to the corresponding server included in the Nginx configuration.

Let’s start with the installation and configuration.

Login to your server as user root

ssh root@IP_Address

and make sure that all packages installed on it are up to date

apt-get update && apt-get upgrade

We need Nginx web server installed on the VPS. Run the following command to install Nginx

apt-get install nginx

Once it is installed, check if the web server is running

service nginx status

● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running)

Now, open your website’s Nginx configuration file with your favorite text editor

vim /etc/nginx/sites-available/

and append the load balancing configuration at the top of the file

upstream loadbalancer {

You should have Nginx installed and listening on port 80 on all servers listed above.

Within the same configuration file we need to add the upstream module in the virtualhost configuration

server {
location / {
proxy_pass http://loadbalancer;

Save the file and restart Nginx for the changes to take effect.

service nginx restart

This configuration will equally distribute all incoming traffic across the three servers (,, Nginx can be also configured to distribute the traffic more efficiently. It comes with balancing options such as weight balancing, max fails and IP hash balancing.

Weight Balancing

We can use this option to specify the proportion of the traffic distributed to each of the servers we listed in the upstream.

For example:

upstream loadbalancer {
server weight=1;
server weight=2;
server weight=5;

In this example, will receive twice as much traffic compared to, and will receive five times more traffic than

Max Fails

If you use the default Nginx settings, it will send data to the servers even if they are down. We can use the Max fails option to prevent such cases.

upstream loadbalancer {
server max_fails=4  fail_timeout=20s;
server weight=2;
server weight=4;

In the example above, Nginx will attempt to connect to and if it is not responding for more than 20 seconds it will make another attempt. After four attempts will be considered as down.

IP Hash Balancing

With this method, the visitors will be always sent to the same server. So, if a visitor received the content of, it will be always transferred to that server unless the servers are down or inaccessible.

upstream loadbalancer {
server down;

In this example, is known to be inaccessible, and it is marked as down.

Of course you don’t have to do any of this if you use one of our Nginx VPS Hosting services, in which case you can simply ask our expert Linux admins to set up Nginx load balancing for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

How to Use Logrotate to Manage Logs on Ubuntu
6 Basic Network Commands in Linux
Install BigTree on a CentOS 7 VPS
  • david


    Author Reply

    Nice tutorial
    Can this configuration be done on the same dedicated server or is it pointless
    without extra servers ?

    Thank you

    • admin


      Author Reply

      You need at least 3 servers.