NGINX: Advanced Load Balancer, Web Server, & Reverse Proxy
Tags: webdev, node, devops, tutorial
Published: 2022-12-11 06:17:49

## 📍 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](https://lovepreet.hashnode.dev/oops-write-scalable-and-modular-code) (Monolithic, Microservices, etc)
- Is your server [performing](https://lovepreet.hashnode.dev/make-multiple-requests-to-your-server-jmeter) (Performance Testing) well in different network [load](https://lovepreet.hashnode.dev/load-balancing-a-necessity-to-learn-introduction) (Load Balancing)

![Load Balancing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fprpbeafbskr8zozehc2.png)

🎯 One of the methods to distribute the load is to use software [load balancers](https://lovepreet.hashnode.dev/load-balancing-a-necessity-to-learn-introduction). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pm0vzcachl1418d1zg09.png)

## 📍 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8aehem3aca5i909qpar.png)
_Image Reference:- Intellipath_

## 📍 How to setup Load Balancing with NGINX

👉 **Note:- [Follow](https://dev.to/lovepreetsingh/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlhuajv9cq4fmul001we.png)

- **Create an** [AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpmk5ofgs4cw4tkupvg4.png)

- **Select the OS and Name of the server**

![Creating an EC2 Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uujwiazagy2h6hwecgu3.png)

- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qb1bqjht4z09770ns68.png)

🙂 _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](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._