Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Nginx»How to Setup Load Balancing with Nginx in Linux

    How to Setup Load Balancing with Nginx in Linux

    RahulBy RahulOctober 16, 20172 Mins ReadUpdated: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
    
    cluster load-balancing nginx
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Skype on Fedora 35/34
    Next Article How To Install Skype on Ubuntu 18.04

    Related Posts

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Increase Request Timeout in NGINX

    Updated:January 13, 20222 Mins Read

    How To Install Elasticsearch on CentOS/RHEL 8

    Updated:October 21, 20203 Mins Read

    How to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04

    5 Mins Read

    How To Install Nginx with PHP-FPM on Ubuntu 20.04

    Updated:December 3, 20203 Mins Read

    How to Install Nginx on Ubuntu 20.04

    3 Mins Read

    1 Comment

    1. Janushanth on December 31, 2019 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.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.