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
1 Comment
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.