Check out a free preview of the full Full Stack for Front-End Engineers, v3 course:
The "Adding a Load Balancer" Lesson is part of the full, Full Stack for Front-End Engineers, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Jem walks through adding a load balancer to allow nginx to decide which server to pass the connection to.

Get Unlimited Access Now

Transcript from the "Adding a Load Balancer" Lesson

[00:00:00]
>> So for the last time today, sudo/etc/nginx/ sites-enabled/full stack front end And I'm just gonna jump up here. And I'm gonna create an upstream back end. Do it right above here. Say upstream. Actually, do I need to create this here? No, sorry, wrong place. We don't do it in the server, we do it in Nginx conf cuz that wouldn't make sense.

[00:00:52] So I'm just gonna U all the way back, there we go. I'm gonna quit. So sudo vi /nginx.conf. Now we can create upstream backend. So it doesn't really matter. We'll do it here. And we're gonna create a http block first, if we're not already there. Let me double check.

[00:01:19] Yeah, it's already in there. So I'll just go at the top here. So we say upstream, and we'll call this the nodebacked end just in case we have different types of languages or services running. Call it backed, it's backend. And we'll just lift all our servers. We only have two right now.

[00:01:46] So we'll say server one is running on localhost:3000. And the other one was running on 3001. Okay, not too bad. And for real this time, the last time we're gonna edit this Nginx configuration for this course. So all we have to do now, instead of proxy passing directly to that one open port, we're just gonna proxy pass to Node backend.

[00:02:33] And we'll let Nginx decide which server to pass to. So proxy pass and node backend. And right-click. And what do we do? Sudo nginx -t, looks good. So sudo service nginx Restart. And if we did things correctly, the website's still up and running. But, you're like, how do I know it's actually being load balanced?

[00:03:25] So this is bonus, you don't have to do this. You can believe me that it's being load balanced, but I'm gonna add in a little bit of logging just so I know I can see how the load is being distributed. So I'm gonna edit my nginx.conf. Again, you don't have to do this.

[00:03:43] I'm just showing you what's actually happening in the background. And I'm gonna add a log format line. So I've already had this copy pasted cuz I certainly can't remember all this. We're creating an upstream log format, and all these things are parameters that Nginx logs that we can log to.

[00:04:04] And this is just a really, really, really, really verbose log. And really what we wanna know is where things are headed. And this, again, I just copy pasted it. I don't remember the software head, I put in the notes. If you're like, how did you remember all that?

[00:04:18] I didn't, that's my secret. And we're just gonna edit, I'm gonna edit my nginx.conf. And I just wanna say, For my access log, I'm just gonna specify that I wanna use that upstream log. And the logging is in var/ log/ngnix /access.log, the default spot. And I'm just gonna specify when to use this log formats.

[00:05:02] Let me write let's double-check, oops. Everything looks good, let's restart. And I'm gonna tail sudo tail- f / var / log / nginx / The access.log. All right, looks like nothing's happening right now. So let's go ahead and Reload my server a few times. I must have put something in the wrong place cuz nothing's showing up here.

[00:05:50] The dash f, it's always one off, it's always the one off. There we go. So here we can see, as I refresh the server, sometimes it goes to port 3000, sometimes it goes to 3001. If we had more servers, you'll see the load getting distributed out. Other than that, we get to add in special logging into our applications themselves to respond to the server about which one they are.

[00:06:14] But again, a little bit more complicated, we need to get. Pretty cool. So now you have as many servers as you want. You can bring them up and down easily as you want, and Nginx will take care of all of it for you as long as you tell them which server cluster to route to.