Nginx

How To Setup Nginx Load Balancer In Linux

nginx-load-balancer
mm
Written by Santosh Prasad

Nginx is a high performance and lightweight web server. It also work as web reverse proxy and email (POP3/IMAP)proxy server. It runs on Linux, UNIX, BSD, Mac OS X, Solaris and Microsoft Windows. As per the Netcraft more than 6% of all domains on the internet use Nginx web server. Nginx powers several high traffic web sites like WordPress, Github, Hulu, and SourceForge. The need for serving large number of concurrent requests is raising every day. Also Nginx solved the C10K ( i.e 10,000 concurrent clients) Problem.

In this tutorial I will explain how to setup Nginx load balancer in Linux.

Install Nginx Web Server

First of all you have to install Nginx web server. Follow the below command or Link to install Nginx.

Link: How To Install Nginx

On Ubuntu/Debian

$ sudo apt-get install nginx

On CentOS/RHEL/Fedora

#yum install nginx

Configure VirtualHost

Now create a Nginx virtual host configuration file as per your domain. Follow the below minimal setting configuration file.

# vim /etc/nginx/conf.d/virtual-domain.conf

upstream app_servers  {
   server app1.example.com;
   server app2.example.com;
}

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

Upstream Setting

You can also set some useful setting to more customize and optimize your load balancer with Nginx such as weight and IP hash like below configuration:

Weight

upstream app_servers  {
   server app1.example.com weight=1;
   server app2.example.com weight=2;
}

IP Hash

upstream app_servers {
   ip_hash;
   server   app1.example.com;
   server   app2.example.com;
   server   app3.example.com  down;
 }

Restart Nginx Service

Now restart the Nginx web service after making above changes using below command:

# service nginx restart
OR
# systemctl restart nginx.service

Thanks:)

Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment