• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Setup Load Balancing with Nginx in Linux

Written by Rahul, Updated on October 25, 2017

Prerequisites

You must have root access or sudo access to your server. Connect your server console with privileged access. Configure your site on backend servers.

Step 1 – Install Nginx Server

First of all, Login to your server with SSH access. Windows users can use PuTTY or alternatives to SSH into the server. Now install Nginx using Linux package manager. Nginx package is available under default yum and apt repositories.

Using Apt-get:

$ sudo apt-get install nginx

Using Yum:

$ sudo yum install nginx

Using DNF:

$ sudo dnf install nginx

Step 2 – Setup VirtualHost with Upstream

Let’s create a Nginx virtual host configuration file for your domain. Below is my minimal settings configuration file.

/etc/nginx/conf.d/www.example.com.conf

upstream remote_servers  {
   server remote1.example.com;
   server remote2.example.com;
   server remote3.example.com;
}

server {
   listen   80;
   server_name  example.com www.example.com;
   location / {
     proxy_pass  http://remote_servers;
   }
}

Step 3 – Other Useful Directives

You may also use some more useful settings to more customize and optimize your load balancer with Nginx. For example set, the weight and IP hash like below with configuration.

Weight

upstream remote_servers  {
   server remote1.example.com weight=1;
   server remote2.example.com weight=2;
   server remote3.example.com weight=4;
}

IP Hash

upstream remote_servers {
   ip_hash;
   server   remote1.example.com;
   server   remote2.example.com;
   server   remote3.example.com  down;
 }

Step 4 – Restart Nginx Service

After making all the changes, restart Nginx service with the following command.

$ sudo systemctl restart nginx.service

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

  1. Avatar Janushanth Reply
    December 31, 2019 at 9:41 am

    Hi RAHUL,

    I have to create the load balancing in the /etc/nginx/conf.d/ or /etc/nginx/sites-available

    when i configure the load balancer from i getting 502 Bad Gateway can you help me into this.

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy