• Home
  • About Us
  • Contact
  • Privacy Policy

LinuxAdmin.io

  • Home
  • Tutorials
    • System Administration
    • Linux Tutorials
    • Web Hosting
    • WordPress
    • Virtualization
    • Troubleshooting
    • OpenStack

Nginx Load Balancing

Nginx Load Balancer

Nginx can be used as a load balancer to multiple back-end web servers using the proxy functions. This guide will cover the basics of configuring the proxy server to pull from the other web servers. It assumes you already have completed the initial install of Nginx. If you do not already have a install please see Nginx Compile From Source On CentOS.

You will want to create a new configuration (or add to an existing one)

nano /etc/nginx/load-balancer.conf

Insert the following:

upstream cluster {
 server 192.168.100.10; 
 server 192.168.100.11;
 server 192.168.100.12;
}

server {
 listen 80;
server_name domain.com;

location / {
 proxy_pass http://cluster;
 }
}

And save the file. You will want to substitute the 192.168.100.xx IP addresses with addresses of each of the web servers you want to send requests too.   Once you have completed the load balancing configuration, you will want to restart Nginx to load the new configuration

service nginx restart

Nginx Load Balancing Methods

There are a couple of different clustering types you can utilize each will direct traffic differently

Round Robin

by default without specifying one, round-robin will be used. Sending each new request to a different server.

upstream cluster {
   ip_hash;
   server 192.168.100.10; 
   server 192.168.100.11;
   server 192.168.100.12;
}

ip_hash will send all subsequent requests from the same IP address to the same server. This is useful if you need sessions to persist on the same server for the same IP address.

Least Connections

upstream cluster {
   least_conn;
   server 192.168.100.10; 
   server 192.168.100.11;
   server 192.168.100.12;
}

least_conn will send new requests to the server with the least amount of active connections. This is useful for balancing each server equally.

Weighted Load Balancing

upstream cluster {
   server 192.168.100.10 weight=3; 
   server 192.168.100.11 weight=1;
   server 192.168.100.12  weight=1;
}

Specifying a weight will distribute more requests to servers with a higher weight. In the above example for each 5 requests, 3 will be sent to 192.168.100.10, and one request will be sent to each of other 2 servers. This is useful in situations where you might have one server which is more powerful than the others or you would like to do testing and control a portion of traffic going to a specific server.

May 25, 2017LinuxAdmin.io
0 0 vote
Article Rating
How To Configure A Nginx Reverse Proxy With ApacheInstall The ngx_cache_purge Module In Nginx
You Might Also Like
 
Install And Configure ProFTPD On CentOS
 
MariaDB 10 Upgrade From MySQL 5.1 on CentOS 6
Subscribe
Notify of
guest
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Cleta
Cleta
5 years ago

It works very well for me

0
Reply
5 years ago 1 Comment Linux Tutorials, Nginx, Web Hosting centos, linux, Linux Performance Tuning, sysadmin788
Recent Posts
  • Laravel Installation Guide For CentOS
  • Openstack Services Explanation And Overview
  • OSSEC Intrusion Detection Installation On Centos 7
  • Configure ProFTPd for SFTP on CentOS
  • How To Check And Repair MyISAM Tables In MySQL
Most Commented
Hot Clone Linux Server with Rsync
Hot Clone A CentOS Server With Rsync
14 Comments
ngx_cache_purge module
Install The ngx_cache_purge Module In Nginx
8 Comments
piwik-nginx
Piwik Analytics on Nginx
8 Comments
Tags
linuxcentosLinux Performance Tuningsysadminkvmnetworkingmemcachedanalyticssystemd
About

We love Linux and are dedicated to creating Linux administration tutorials for System Administrators since 2016.

Most Viewed
Default Gateway
How To Configure A Default Gateway on CentOS
62,984 views
Zend Opcache
Setup and Optimize Zend OpCache
46,714 views
Install ffmpeg
FFMpeg Install On CentOS 7
20,909 views
Archives
Email subscription

Sign up for our newsletter to receive the latest news and event postings.

2018 © LinuxAdmin.io
wpDiscuz