How To Setup Nginx Load Balancer In Linux

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 {
   listen   80;
   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:


upstream app_servers  {
   server weight=1;
   server weight=2;

IP Hash

upstream app_servers {
   server  down;

Restart Nginx Service

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

# service nginx restart
# systemctl restart nginx.service


Thank you! for visiting LookLinux.

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

About the author


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.


    • Hi Humza,
      First of all thanks for visiting looklinux.
      We can’t do this with Nginx load balancing because Nginx work work on application layer.
      You should use HaProxy, with HaProxy you have the choice of proxying traffic at layer 4 (TCP) or layer 7 (HTTP). The former is great for load balancing non-HTTP services, such as databases, whereas the latter is perfect for load balancing web applications. May be my this tutorial will help you.

Leave a Comment