π 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)
π― 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 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.
π 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.
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)
- Select the OS and Name of the server
- 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)
π 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
and ```chmod
0400 Server1.pem
400 Server1.pem
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l23pohlzdb26jx7v318d.png)
- **Now, Connect to your EC2 instance server and Deploy your** [NodeJS app on that server](https://www.youtube.com/watch?v=S45jZCvd2M8).
![EC2 instance running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xasq081j73hr4wkt7azv.png)
- **I have deployed NodeJS apps on Server 1 and Server 2 as**:-
NodeJS APIs code that is deployed for load balancing:-
![NodeJS Servers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot0uv2mvfgv4a8wg1t6t.png)
π₯ 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](https://intellipaat.com/community/3700/how-to-open-a-web-server-port-on-ec2-instance)
- **Now, here both servers are running server_1.js and server_2.js as**:-
![NodeJS Servers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea7j261tesojhici5i6z.png)
- **Now, we'll setup Nginx Webserver (As a load balancer) on the third ec2 instance**
- **Launch New EC2 instance and** [install Nginx](https://ubuntu.com/tutorials/install-and-configure-nginx#2-installing-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9ydktoff6f4wgez5jmj.png)
- 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](https://dev.to/lovepreetsingh/) for more practical tutorials
π _More Quality content will be coming on the core topics._
Top comments (5)
Well explained article! Thank a lot for this.
Awesome post bro π
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 ?
Informative!!
Very much needed for my current website thanks for sharing #devscommunity