Friday, July 14, 2017

How To Configure Nginx Load Balancer on CentOS 7


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.3.1611 (Core)
  • SELinux enable operating systems

Pre-Requisites

  1. epel-release repository is installed on the server.
    • If epel repository is not installed, to install run the command; yum install epel-release -y -q
  2. 3 Servers nginx installed, enabled and started;
    • 1 server acts as load balancer server.
    • 2 server(s) acts as webserver also acts as LB Node 1 and LB Node 2.

Package Install – nginx

After installing “epel-release” repository package; install nginx package, run the command;

yum install nginx -y


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 pre-configured, 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.

Also, 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 vcptest02
Load Balancer Node vcplbsrvr

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 {
         }
    }
    



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;
  }
}




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 location of files to serve from.

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.

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






Slideshare Information

A step by step document is uploaded to Slideshare.





9 comments:

  1. Hello,

    This is really good tutorial. By the way, I already followed step-by-step instruction as written above and I modified according to my environment. Finally when I tested the load balancing server, it wouldn't forward to my any backend server. but I've checked that everything is appropriate. in which part did I missed my configuration

    ReplyDelete
    Replies
    1. sorry, unintentionally blocked you. What is the error message your are getting?

      Delete
    2. Thank you very much for your response.

      There are no error messege from the display result. The load balancing server always shows the default page of its own, it didn't forward to any backend server I have set.

      I'v already tried several scenario also, until the simplest one
      1. all server using vhosts and php enabled -> unsuccessful
      2. all server using IP addresses instead of vhosts and php enabled -> unsuccessful
      3. all server using IP addresses with default configuration, only modified load-balancer.conf -> unsuccessful

      my last configuration of load-balancer.conf, written as bellow
      upstream myserver {
      server 172.17.45.108;
      server 172.17.45.109;
      }
      server {
      listen 80;
      location / {
      proxy_pass http://myserver;
      }
      }

      I'm using centos 7 for all server. The IP addresses of load-balancing server is 172.17.45.107

      I'm wondering. whats wrong with this. the proxy_pass seems never work. I looking forward for any suggestion.

      Delete
    3. Are you able to get the local server default page on both the servers?

      Delete
    4. You can check the nginx errors, log in the folder /var/log/nginx folder.

      Delete
    5. "local server default page on both the servers?". yes I'm able. I can also get it through the network.

      the error log I get is shown like this:
      2018/01/31 21:56:05 [error] 2066#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.45.104, server: _, request: "GET /favicon.ico HTTP/1.1", host: "172.17.45.107", referrer: "http://172.17.45.107/"

      there is no error related with proxy_pass or upstream.

      Delete
    6. do I need to configure something in "/etc/nginx/nginx.conf"? I still left it by default

      Delete
    7. I have found the problem, it turns out the "server {..}" script in "/etc/nginx/nginx.conf", must be deleted or commented, so the declaration of "server {..}" script in "/etc/nginx/conf.d/load-balancer.conf" can run.

      finally it works like a charm..

      Delete