DEV Community

Cover image for NGINX: Advanced Load Balancer, Web Server, & Reverse Proxy
Lovepreet Singh
Lovepreet Singh

Posted on

NGINX: Advanced Load Balancer, Web Server, & Reverse Proxy

πŸ“ Concurrency

πŸ‘‰ When it comes to making a request to the server, it usually happens that multiple users will be making requests. And Server needs to handle all the requests. This is where the important points to consider come into play like:-

  • Is your server concurrent
  • Is your backend architecture well designed (Monolithic, Microservices, etc)
  • Is your server performing (Performance Testing) well in different network load (Load Balancing)

Load Balancing

🎯 One of the methods to distribute the load is to use software load balancers. This is where Nginx comes into play. In general, Nginx is mainly used as a load balancer. Though Nginx can be used as a mail proxy, reverse proxy, HTTP cache, etc.

πŸ™‹ But what exactly the Nginx and How do we use it??

Nginx

πŸ“ Nginx

Nginx is a web server that is capable of handling ~10k concurrent users or network load per second. It became the fastest available web server followed by Apache.

Nginx web server
Image Reference:- Intellipath

πŸ“ How to setup Load Balancing with NGINX

πŸ‘‰ Note:- Follow for more, Because we'll see the implementation of load balancers, Proxy, etc in our NodeJS app in our future blogs.

πŸ™‚ With Nginx we can use Simple (Round Robin), Weighted, and Least connections load balancing.

Steps that we'll follow:-

  • Will use simple Node APIs and will run in two EC2 instances (EC2 instances are nothing but virtual servers or machines that can run our backend code)
  • Then, we'll run Nginx in the third EC2 instance or a third machine
  • Then we'll set up load balancing between the two EC2 servers running our Node App.

😌 Note:- Don't worry follow along you'll get the most out of it.

LOL

  • Create an AWS account, Don't worry you can use it for free until you do not exceed free limits

  • Now, Go to the Amazon EC2 Service from the Homepage and Create an EC2 instance (Launch an EC2 instance)

AWS EC2 instance

  • Select the OS and Name of the server

Creating an EC2 Instance

  • Select the key pair as .pem file and save the file on your local machine. Here you can see I have created two EC2 instances to run two Servers (Our NodeJS app)

SSH to EC2 machine

πŸ™‚ Note:- Here Public IP is the one that we will need to SSH your EC2 machine from our local machine (Basically in layman's words SSH means you can control the EC2 virtual server from your local machine's Terminal)

  • Now, go to the directory that is having your pem file. Enter a command like ssh -i Server1.pem ubuntu@18.212.63.176 where Server1.pem is the pem file that you saved while creating the instance and ubuntu@public_ip contains the public IP of your instance.

Note:- Before running the above command, do change the previlige of the pem file by entering

chmod 0400 Server1.pem

and

chmod 400 Server1.pem

Image description

EC2 instance running

  • I have deployed NodeJS apps on Server 1 and Server 2 as:-

NodeJS APIs code that is deployed for load balancing:-

NodeJS Servers

πŸ”₯ Note:- In case your server is listening in the EC2 instance but you are not able to connect to the server from the local machine terminal then follow these:-

  • In your SSH terminal of EC2 instance type kill -9 node
  • Change the port of your server.js file to 80
  • Add HTTP security Group from this Link

    • Now, here both servers are running server_1.js and server_2.js as:-

NodeJS Servers

  • Now, we'll setup Nginx Webserver (As a load balancer) on the third ec2 instance

  • Launch New EC2 instance and install Nginx in your ubuntu Ec2

  • Now, in this new EC2 instance, after installing Nginx go to /etc/nginx/conf.d directory and create a file named lb.config

  • Enter Content in the file as:-

Load Balancer

  • Here, endpoint1 is the name that you can give and the same name should be present in the location block. Servers here are the Private IPs of our Ec2 instances entered.

  • This is the simple load balancing (Round Robin) means the first request will route to server 1 and the second will to server 2 and so on...


Now, you can access Nginx EC2 instance public IP from browser URL and It will make requests like the first request will route to server 1 and the second will to server 2, and so on...

🎯 I hope you guys have enjoyed the Load balancing using Nginx. Follow for more practical tutorials

πŸ™‚ More Quality content will be coming on the core topics.

Oldest comments (5)

Collapse
 
vaibhav68849256 profile image
Vaibhav Khandare

Very much needed for my current website thanks for sharing #devscommunity

Collapse
 
taha_syed profile image
Taha Syed

Informative!!

Collapse
 
jareechang profile image
Jerry

This is cool! πŸ‘

Do you know if there is a way you can automatically add new instances into nginx ?

Letβ€˜s say I provision another server, how do I add that in with this setup ?

Collapse
 
digi18 profile image
Digvijay Singh Thakur

Awesome post bro πŸ‘

Collapse
 
annetawamono profile image
Anneta Wamono

Well explained article! Thank a lot for this.