Skip to main content

How to Use Nginx as a Load Balancer

<#include '/gkmsid/9058821'>
Load balancing is a system that distributes network or application traffic across a number of servers.
For example, you have your primary NGINX web server and want to distribute the load across two or three other NGINX servers; by doing this, you can ensure that no matter how much traffic you're getting, more than one server can take care of the load. Clearly, your web sites (on each NGINX server) will have to be set up identically for this to work (otherwise, you'd be distributing traffic across differing sites).
With that said, I'm going to show you just how easy it is to setup NGINX for load balancing. I'll be demonstrating on Ubuntu Server 16.04, with three servers at IP addresses:
  • Server 1 - 192.168.1.232
  • Server 2 - 192.168.1.233
  • Server 3 - 192.168.1.234
You will alter the configuration to meet your specific IP address scheme and needs (say you want to distribute across more servers). I will assume you already have NGINX up and running on each server.

Initial setup

I'm going with absolute basics here. To demonstrate how this works, I will create a new index.html in each server's /var/www/html directory, with the content:

NGINX SERVER Y


Where Y is either 1, 2, 3 (depending on which server the file is on).
With that in place, let's configure NGINX.

Configuration

We're going to create a new file to configure load balancing on SERVER 1 only. Issue the command sudo nano /etc/nginx/conf.d/load-balancer.conf. In that file, copy the following contents:
# Define which servers to include in the load balancing scheme.

upstream backend {
   server 192.168.1.232;
   server 192.168.1.233;
   server 192.168.1.234;
}

# This server accepts all traffic to port 80 and passes it to the upstream.

server {
   listen 80;

   location / {
      proxy_pass http://backend;
   }
}

NOTE: Make sure to configure the above file with your IP address scheme. You can also add as many servers as necessary for load balancing.
Save and close that file.
On our Ubuntu system, we have to remove the symbolic link to default, in the /etc/nginx/sites-enabled folder. Do this with the command:
sudo rm /etc/nginx/sites-enabled/default

Restart NGINX with the command:
sudo systemctl restart nginx

Testing

If you point a browser to the IP address of Server 1 (in our case 192.168.1.232), the load balancing will begin to round-robin requests to the NGINX servers at the other addresses, configured within the load-balancer.conf file. Your first call to Server 1's IP address will take you to Server 2 and the next to Server 3 (and so on).

The default load balancing system is round robin. This can cause problems with session persistence. If your web sites/applications require users to be directed to the same server used for their previous session, IP hashing must be used. With IP hashing in place, a user will always be directed to the same server (so long as the server is available).
To setup IP hashing, open up the /etc/nginx/conf.d/load-balancer.conf file and add the line ip_hash; below upstream backend {. That new file would look like:
# Define which servers to include in the load balancing scheme.

upstream backend {
   ip_hash;
   server 192.168.1.232;
   server 192.168.1.233;
   server 192.168.1.234;
}

# This server accepts all traffic to port 80 and passes it to the upstream.

server {
   listen 80;

   location / {
      proxy_pass http://backend;
   }
}

Save and close that file. Restart NGINX with the command:
sudo systemctl restart nginx

Now if you point your browser to the Server 1 IP address and hit refresh, you will be sent to the same server.

Comments

Popular posts from this blog

How to Create a PDF file in Cakephp 2.0 using Fpdf

Step 1: Download FPDF folder from  http://www.fpdf.org/  . Step 2: Unzip the downloaded Fpdf file and name it “fpdf” or however you require and make sure that you use the same name while calling it. Step 3: Move the “fpdf” unzipped files to  your /app/Vendor directory within Cakephp. Now you should have the directory path as   /app/Vendor/fpdf. Step 4: Create a new Cakephp layout file for the pdfs. We will use this layout when serving a pdf to the client. Create a file called pdf.ctp inside of /app/View/Layouts. Add the following code to /app/View/Layouts/pdf.ctp Layout: /app/View/Layouts/pdf.ctp 1 2 3 4 <?php      header ( 'Content-Disposition: attachment; filename="downloaded.pdf"' ) ;      echo $content_for_layout ; ?> The header function above tells the browser that it is going to receive...

Setup CakePHP Using Xampp On Windows

Step 1: Download XAMPP  and  CakePHP .   Step 2: Install Xampp Once you have installed Xampp (version 1.7.3) on your Windows with the default option, all your files will be located in the C:\xampp folder. Step 3: Mod Rewrite Module Once Xampp is installed as the local server, you can then proceed to enable mod_rewrite. To do so, you will have to open the httpd.conf file that is located in C:\xampp\apache\conf and uncomment by removing # from the following line: # LoadModule rewrite_module modules/mod_rewrite.so Step 4: Place CakePHP Files in a New Folder Extract the CakePHP (version 1.3.8) zip file and copy all its contents to your local web server, which in this instance is C:\xampp\htdocs\cakephp . I have decided to name the CakePHP folder as cakephp, and in it, you will find many files and folders for the framework, including app, cake, docs, vendors, .htaccess, and index.php. Step 5: Set Up Virtual Host Open the httpd-vhosts.conf file from th...

Dynamic Sitemap Generation plugin in Cakephp

Here for the SEO implementation we need to generate the sitemap.xml in our application which is accessed by the webmaster tool. So here i am outlined the steps to generate the Xml file . 1. Lets think we have controller by name sitemap,Inside that create an action by name sitemap and paste the following code    public function sitemap(){     $this->layout='ajax';     $this->RequestHandler->respondAs('xml');     $listData = $this->Sitemap->find('all',/*Conditions if you have any*/);     $this->set(compact('listData')); } I through variable $listData to render all data( Keywords,Title,Url,etc ...) that will be shown in sitemap.xml.  This   depends   on the   dynamic link  what   we want to   show  in sitemap.xml.For request handler to work include the RequestHandler component public $components = array('RequestHandler'); Step 2. Create View based on...

Druapl 8 Folder and File Permissions

Drupal direcories: find /var/www/html -type d -exec chmod 755 {} \; Drupal files: find /var/www/html -type f -exec chmod 644 {} \; Drupal all content related directories: find /var/www/html/your_site_name/ sites/b2b_cms_mi/files -type d -exec chmod 775 {} \; Drupal all content related files: find /var/www/html/your_project_folder/ sites/default/files -type f -exec chmod 664 {} \; Drupal content folder permission: chmod 777 /var/www/html/your_project_folder/ sites/default/files .htaccess permission cd /var/www/html/your_project_folder/ sites/default/files/config_ fdwgNYChzdnUSBU8hEm-QEe3_ SXdb6kzCJ3ffEC4sB7njFXuK06hHUF OJE888-GN0_gpEE1b0w/sync chmod 777 .htaccess cd /var/www/html/your_project_folder/ sites/default/files/ chmod 777 .htaccess Note:-  your_project_folder is the name of the project. default can be a site folder name, if the application is multisite or else it will be a default folder.