You are on page 1of 5

8/26/2021 How To Setup Nginx Load Balancing - RoseHosting

How To Setup Nginx Load Balancing Categories

November 11, 2016 by Jeff Wilson


Select Category

About RoseHosting

Buy a Managed VPS - 25% OFF

RoseHosting Customer Reviews

Managed NVMe VPSes

Managed NVMe Dedicated


We’ll show you, How To Setup Nginx Load Balancing. Load balancing is a very useful Servers
technique to distribute the incoming network traffic across a number servers. With
this technique you can reduce the resource usage, lower the response time and
avoid server overload. In this tutorial, we will guide you through the steps of
configuring Nginx load balancing on a Linux VPS.



Nginx Load Balancing is one of the most efficient options available to achieve full
application redundancy, and it is relatively easy and quick to setup. We will configure
Nginx load balancing using the Round robin mechanism. This way it will forward all
requests to the corresponding server included in the Nginx configuration. Search …

Let’s start with the installation and configuration.

How to install Laravel


Table of Contents  on RoseHosting Cloud
1. Login via SSH
2. Update all packages
How to install ReactJS
3. Install Nginx web server
on Ubuntu 20.04
4. Append the load balancing configuration
5. Add upstream module in Nginx
6. Restart Nginx server How to Install Bagisto
7. Weight Balancing eCommerce Platform
8. Max Fails on Ubuntu 20.04
9. IP Hash Balancing
Access Your Database

1. Login via SSH Remotely Through an


SSH Tunnel
Login to your server as user root
How to install Jira
Ticketing system on RH
ssh root@IP_Address Cloud

2. Update all packages


Join our Blog
and make sure that all packages installed on it are up to date
Sign up and receive notifications
as soon as new content is posted.
apt-get update && apt-get upgrade
Enter your e-mail *

https://www.rosehosting.com/blog/set-up-nginx-load-balancing/ 1/5
8/26/2021 How To Setup Nginx Load Balancing - RoseHosting

3. Install Nginx web server Subscribe

We need Nginx web server installed on the VPS. Run the following command to
install Nginx

apt-get install nginx

Once it is installed, check if the web server is running

service nginx status

● nginx.service - A high performance web server and a reverse proxy


server

Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor


preset: enabled)

Active: active (running)

4. Append the load balancing configuration


Now, open your website’s Nginx configuration file with your favorite text editor

vim /etc/nginx/sites-available/yourdomain.com.conf

and append the load balancing configuration at the top of the file

upstream loadbalancer {

server vps1.yourdomain.com;

server vps2.yourdomain.com;

server vps3.yourdomain.com;

You should have Nginx installed and listening on port 80 on all servers listed above.

5. Add upstream module in Nginx


Within the same configuration file yourdomain.com.conf we need to add the
upstream module in the virtualhost configuration

server {

location / {

proxy_pass http://loadbalancer;

6. Restart Nginx server


Save the file and restart Nginx for the changes to take effect.

service nginx restart

This configuration will equally distribute all incoming traffic across the three servers
(vps1.yourdomain.com, vps2.yourdomain.com, vps3.yourdomain.com). Nginx can be
also configured to distribute the traffic more efficiently. It comes with balancing

https://www.rosehosting.com/blog/set-up-nginx-load-balancing/ 2/5
8/26/2021 How To Setup Nginx Load Balancing - RoseHosting

options such as weight balancing, max fails and IP hash balancing.

7. Weight Balancing
We can use this option to specify the proportion of the traffic distributed to each of
the servers we listed in the upstream.

For example:

upstream loadbalancer {

server vps1.yourdomain.com weight=1;

server vps2.yourdomain.com weight=2;

server vps3.yourdomain.com weight=5;

In this example, vps2.yourdomain.com will receive twice as much traffic compared to


vps2.yourdomain.com, and vps3.yourdomain.com will receive five times more traffic
than vps1.yourdomain.com.

8. Max Fails
If you use the default Nginx settings, it will send data to the servers even if they are
down. We can use the Max fails option to prevent such cases.

upstream loadbalancer {

server vps1.yourdomain.com max_fails=4  fail_timeout=20s;

server vps2.yourdomain.com weight=2;

server vps3.yourdomain.com weight=4;

In the example above, Nginx will attempt to connect to vps1.yourdomain.com and if


it is not responding for more than 20 seconds it will make another attempt. After
four attempts vps1.yourdomain.com will be considered as down.

9. IP Hash Balancing
With this method, the visitors will be always sent to the same server. So, if a visitor
received the content of vps1.yourdomain.com, it will be always transferred to that
server unless the servers are down or inaccessible.

upstream loadbalancer {

ip_hash;

server vps1.yourdomain.com;

server vps2.yourdomain.com;

server vps3.yourdomain.com down;

In this example, vps3.yourdomain.com is known to be inaccessible, and it is marked


as down.

https://www.rosehosting.com/blog/set-up-nginx-load-balancing/ 3/5
8/26/2021 How To Setup Nginx Load Balancing - RoseHosting

Of course you don’t have to Set up Nginx Load Balancing if you use one of our Nginx
VPS Hosting services, in which case you can simply ask our expert Linux admins to
setup Nginx load balancing for you. They are available 24×7 and will take care of your
request immediately.

PS. If you liked this post, on How To Set Up Nginx Load Balancing,  please share it
with your friends on the social networks using the buttons on the left or simply leave
a reply below. Thanks.

 Networking and Domains, Tutorials, Web Servers


 SVN Linux | Getting Started With Subversion on Linux
 How To Set Up a Firewall with UFW on Ubuntu 16.04

3 thoughts on “How To Setup Nginx Load Balancing”

david
December 21, 2016 | Reply

Nice tutorial

Can this configuration be done on the same dedicated server or is it pointless

without extra servers ?

Thank you

admin
December 21, 2016 | Reply

You need at least 3 servers.

Pingback: Physical server vs Virtual server: All you need to know – RoseHosting.com
Blog

Leave a Comment

https://www.rosehosting.com/blog/set-up-nginx-load-balancing/ 4/5
8/26/2021 How To Setup Nginx Load Balancing - RoseHosting

Name *

Email *

Save my name, email, and website in this browser for the next time I comment.

To prove you are human please solve the following *


 + 
four
 = 
five 

 Yes, add me to your new blog post notifications list

Post Comment

COMPANY SUPPORT HOSTING LINUX VPS HOSTING

About us Helpdesk System Web Hosting CentOS Hosting


Our Policies Knowledge Base Linux VPS Hosting Ubuntu Hosting
Contact Us Cloud Hosting Debian Hosting
Blog OTHER SERVICES NVMe Hosting OpenSUSE Hosting
Why RoseHosting Domain Registration Custom SSD VPS Arch-Linux Hosting
Compare Us Domain Transfer Dedicated Servers Scientific Linux Hosting
Customer Reviews SSL Certificates Hosting Solutions
Awards & Recognition Recurr. Affiliate Program
Recommended Services CPA Affiliate Program
APPS HOSTING CONTACT US

WordPress Hosting (888) ROSE-HOST


Magento Hosting (888) 767-3467
Odoo Hosting (314) 275-0414
Joomla Hosting Email us
Drupal Hosting
Laravel Hosting CONNECT

NextCloud Hosting  Twitter


PrestaShop Hosting  Facebook
Ghost Hosting  LinkedIn
MediaWiki Hosting  RSS Feed
Tomcat Hosting

Terms of Service and other policies

© 2001-2021 Rose Web Services LLC.

https://www.rosehosting.com/blog/set-up-nginx-load-balancing/ 5/5

You might also like