# Nginx as a Load Balancer

### Configure the servers

You need to have node preinstalled in your system or you can use any server of your choice.

Our server running on the following sockets:

*   [localhost:3000](http://localhost:3000)
    
*   [localhost:3001](http://localhost:3001)
    
*   [localhost:3002](http://localhost:3002)
    

I will be creating 3 simple node servers running on my local machine.

Run `npm init` in your root project directory.

Then create three files `server_1.js`, `server_2.js`, `server_3.js`.

Here is what my project directory will look like.

```bash
.
├── package.json
├── server_1.js
├── server_2.js
└── server_3.js
```

Here is the code for `server_1.js`

```javascript
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Hello from - server_1</h1>\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
```

Run `node server_1.js` for starting this server on 3000.

Here is the code for `server_2.js`

```javascript
const http = require('http');

const hostname = '127.0.0.1';
const port = 3001;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Hello from - server_2</h1>\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
```

Open a new terminal and run `node server_1.js` for starting this server on 3001.

Here is the code for `server_3.js`

```javascript
const http = require('http');

const hostname = '127.0.0.1';
const port = 3002;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Hello from - server_3</h1>\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
```

Open a new terminal and run `node server_3.js` for starting this server on 3002.

### Configure Nginx

If you want to install Nginx from the source code, please follow the guideline of this article [https://techblog.akashojha.com/install-nginx-from-source-code](https://techblog.akashojha.com/install-nginx-from-source-code)

If you already running Nginx, first you need to stop it.

```bash
sudo systemctl stop nginx
```

Now let's add a new file `load_balancer.conf` in `/etc/nginx` directory.

```bash
sudo vim /etc/nginx/load_balancer.conf
```

Let's add the following content in your `load_balancer.conf` file.

```bash
events {
}

http {
    upstream nodeapis {
        server localhost:3000;
        server localhost:3001;
        server localhost:3002;
    }
    server{
        listen 8888;

        location / {
            proxy_pass http://nodeapis/;
        }
    }
}
```

Let's start your Nginx server.

```bash
sudo nginx -c /etc/nginx/load_balancer.conf
```

If you go to your terminal and run the following command

```bash
while sleep 0.5; do curl http://localhost:8888/; done;
```

You should be able to see the response like this in the terminal

```bash
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
```

This is following the round-robin method i.e., choose from all the local servers with equal priority.

You can go to your browser and open [http://127.0.0.1:8888](http://localhost:8888/) and see the same response by reloading the server again and again.

So, we have learnt how to use Nginx as a load balancer.

Happy Learning!!
