This document provides instructions on how to configure Nginx as a load balancer on CentOS 7. It describes installing Nginx, configuring two web servers and a load balancer node, setting up the load balancing configuration in the Nginx configuration file, and testing the load balancer functionality using curl commands and a web browser.
1. How To Configure Load Balancer Nginx on CentOS 7
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To......................................................................................................................................................1
Pre-Requisites ...............................................................................................................................................1
Package Install – nginx..................................................................................................................................1
Package Install – Verify .............................................................................................................................2
Enable Service - nginx ...............................................................................................................................2
Configure – nginx ..........................................................................................................................................2
Configure Nginx – Load Balancer..............................................................................................................2
Configuration – Overview.........................................................................................................................2
Configuration – Server Directive – Node 1 and Node 2............................................................................3
Configuration – Server Directive – Load Balancer Node...........................................................................4
Upstream Section – Load Balancer...........................................................................................................5
Load Balancer Customization Table..........................................................................................................5
Testing – Load Balancer ............................................................................................................................6
Testing - Curl Utility...............................................................................................................................6
Testing – Web Browser.........................................................................................................................6
2. How To Configure Load Balancer Nginx on CentOS 7
1 | P a g e
Overview
Nginx is an open-source, high performance HTTP & ReverseProxy Server, it can also be configured to
operate as IMAP / POP3 Server.
It’s well-known for high performance, stability, rich feature set, simple configuration and low resource
consumption.
In this guide we will configure nginx server to run as a load-balancer on a Linux based operating system.
Applies To
CentOS 7, tested on CentOS Linux release 7.1.1503 (Core)
Pre-Requisites
1) 3 Servers nginx installed, enabled and started;
a) 1 server acts as load balancer server.
b) 2 server(s) acts as webserver also acts as LB Node 1 and LB Node 2.
2) epel-release repository is installed on the server.
a) If epel repository is not installed, to install run the command; yum install epel-release -y -q
Package Install – nginx
After installing “epel-release” repository package; install nginx package, run the command;
yum install nginx -y
3. How To Configure Load Balancer Nginx on CentOS 7
2 | P a g e
Package Install – Verify
After installation of nginx package, to confirm if the package has been successfully installed, you can run
the command;
yum list installed | grep nginx or
rpm -qai screen | grep -E "Name| Install Date"
Enable Service - nginx
After installation of nginx package, next step is to configure nginx service daemon to automatically start
upon server reboots. To start service automatically, run the command;
systemctl enable nginx
Configure – nginx
By default nginx is preconfigured, if you intend to customize server parameters this can be accomplished
by editing the master configuration file “/etc/nginx/nginx.conf” in “server” block section.
By default port “80” is the configured for nginx service to “LISTEN”, and when the server is started, server
starts automatically on IPV4 and IPV6 network interface(s).
Configure Nginx – Load Balancer
First step, for configuring Nginx as load balancer, each node (webserver) has to be configured to serve
webpage. Once you install, configure, enable and start nginx service. Ensure that default page load’s
correctly; execute the command, this command should fetch the default webpage “index.html”.
curl localhost
Configuration – Overview
In this section we will configure load balancer, to configure it we need below servers, you can add more
servers if the work loads are higher. Below table give a typical 2 node webserver and a load balancer.
Server Function Hostname
Web Server Node #1 vcptest01
Web Server Node #2 vcptest01
Load Balancer Node vcplbsrvr
4. How To Configure Load Balancer Nginx on CentOS 7
3 | P a g e
Configuration – Server Directive – Node 1 and Node 2
Next step in load balancer configuration is to configure “server” block, again if there is any specific
business requirement. Default configuration is good enough, default configuration for “server” block
snippet is posted below.
vi /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
5. How To Configure Load Balancer Nginx on CentOS 7
4 | P a g e
Configuration – Server Directive – Load Balancer Node
Configuring Load balancer on Nginx is quite simple; create a new configuration file “load-balancer.conf”
in the folder “/etc/nginx/conf.d/”, and copy and paste the below configuration snippet.
vi /etc/ngnix/conf.d/load-balancer.conf
upstream websrvrlb {
server vcptest01;
server vcptest02;
}
server {
listen 80;
location / {
proxy_pass http://websrvrlb;
}
}
6. How To Configure Load Balancer Nginx on CentOS 7
5 | P a g e
Upstream Section – Load Balancer
Upstream block has few directives that needs to be configured, so that load balancer can work effectively
and efficiently.
Directive Purpose
upstream websrvrlb The servers that Nginx proxies requests to are known as upstream
servers.
server vcptest01; Define load balancer node, define one node per line.
server vcptest02 down; Define host failed state; if any of the participating node is down for
maintenance or hardware failure, set the node value “down”.
listen 80; Define webserver’s Listen port.
location / Define location of files to serve from.
proxy_pass http://websrvrlb Define proxy server originating from specific IP Address and port, for
load balancer, it is associated with upstream defined name.
Load Balancer Customization Table
Nginx load balancer can be customized based on the business need. This directive has to be defined inside
upstream block.
Load balancer method should be determined and configured based on server performance, network
latency, network path, connectivity time.
LB Method Directive Description
Round
Robin
No Directive It’s default LB method; wherein requests are distributed equally
across the servers it also takes into consideration the server weights.
Least
Connection
least_conn; Request is sent to least number of current active connections, this
methods also takes into consideration the server weights.
IP Hash ip_hash; It can also as called sticky session server binding, it sends to request
to same server, unless the requested server is not available.
Hash hash; The server to which a request is sent is determined from a user-
defined key which may be a text, variable, or their combination.
Least Time least_time; Selects the server with the lowest average latency and the least
number of active connections, this option is available for Nginx Plus
version.
7. How To Configure Load Balancer Nginx on CentOS 7
6 | P a g e
Testing – Load Balancer
Once the load balancer is configured, after restarting the service, you can test them 2 methods;
1) Web Browser
2) Command Line Interface (CLI using curl utility)
Testing - Curl Utility
The below screen shot is shown the request processed on bash shell prompt, when the each time the
request is made, request is fetched from alternate webserver i.e., vcptest01 and vcptest02 .
curl localhost
Testing – Web Browser
The below screenshot is shown the request processed on web browser, when the each time the request
is made, request is fetched from alternate webserver i.e., vcptest01 and vcptest02.
Refresh the browser