How to setup an Nginx load balancer example

How to configure Nginx as a load balancer

To setup Nginx as a load balancer for backend servers, follow these steps:

  1. Open the Nginx configuration file with elevated rights
  2. Define an upstream element and list each node in your backend cluster
  3. Map a URI to the upstream cluster with a proxy_pass location setting
  4. Restart the Nginx server to incorporate the config changes
  5. Verify the Nginx load balancer setup was configured successfully

Which Nginx config file should I edit?

As your Nginx configuration grows, the file in which the upstream element and the proxy_pass location setting must be configured will change.

But for this Nginx load balancer example, we will edit the file named default which can be found in the /etc/nginx/sites-available folder.

How do I use the Nginx upstream setting?

All of the backend servers that work together in a cluster to support a single application or microservice should be listed together in an Nginx upstream block.

For our example, we will have two localhost servers running on separate ports. Each will be listed as a server that is part of the Nginx upstream set named samplecluster:

upstream samplecluster {
  server localhost:8080;
  server localhost:8090;
}

Is the load balancer also a reverse proxy?

The Nginx load balancer will also act as a reverse proxy.

To inform Nginx which URLs should cause requests to forward to the workload managed cluster, a location element with an Nginx proxy_pass entry must also be configured in the default configuration file.

The proxy_pass element will

location /sample {
  proxy_pass http://samplecluster/sample;
}

With this configuration, all requests to Nginx that include the /sample URL will be forwarded to one of the two application servers listed in the upstream element named samplecluster.

nginx load balancer config file

The Nginx load balancer settings will be written to the default file in the sites-available folder.

Complete Nginx load balancer config file

Here is full config file used in this Nginx load balancer example:

### Nginx Load Balancer Example

upstream samplecluster {
  # The upstream elements lists all
  # the backend servers that take part in 
  # the Nginx load balancer example

  server localhost:8090;
  server localhost:8080;

}
### Nginx load balancer example runs on port 80
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/html;
  server_name _;

  location / {
    try_files $uri $uri/ =404;
  }

  # The proxy_pass setting will also make the
  # Nginx load balancer a reverse proxy
  location /sample {
    proxy_pass http://samplecluster/sample;
  }

} # End of Nginx load balancer and reverse proxy config file

Test Nginx as a reverse proxy load balancer

With the Nginx load balancer configured, you can restart the server and test the load balancer.

sudo service nginx restart

When the server restarts, Nginx will spray all requests with the /sample URL to the workload managed Apache Tomcat servers on the backend.

nginx load balancer reverse proxy

Port 80 request being handled by Tomcat, proves the Nginx reverse proxy load balancer is configured correctly.

How do you weight to an Nginx load balanced server?

A common configuration to add to the Nginx load balancer is a weighting on individual servers.

If one server is more powerful than another, you will want it to handle a larger number of requests. To do this, add a higher weighting to that server.

upstream samplecluster {
  server localhost:8090 weight=10;
  server localhost:8080 weight=20;
}

In this example, the server running on port 8080 will receive twice as many requests as the server on port 8090 due to the fact that its weight setting is double its counterpart.

How do you implement stick sessions in Nginx?

Stateful application rely on the fact that even in a highly clustered environment, the reverse proxy will try to send requests from a given client to the same backend server on each request-response cycle.

Nginx supports sticky sessions if the ip_hash element is added to the upstream cluster configuration:

upstream samplecluster {
  ip_hash;
  server localhost:8090 weight=10;
  server localhost:8080 weight=20;
}

How do you configure Nginx load balancers when a server that is offline?

If one of the upstream servers in an Nginx load balanced environment go offline, they can be marked as down in the config file.

upstream samplecluster {
  ip_hash;
  server localhost:8090 weight=10;
  server localhost:8080 weight=20;
  server localhost:8070 down;
}

If an upstream server is marked as being down, the Nginx load balancer will not forward any requests to that server until the flag is removed.

 

 

 

 

 

 

 

 

 

 

 

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close