The NGINX Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome back to the channel in this video what we're going to do is we're going to cover nginx in great great detail now let's actually go ahead and begin by discussing what exactly is nginx and what its purpose is and i think the best way to show this is to show an example where nginx is being used so what i'm going to do is i'm going to go to my browser right over here and i'm going to go to airbnb.ca now as soon as i go to airbnb.ca you can see that my browser is served a bunch of different content you can see there's content here we have this huge image right over here we have this image etc etc now the question is is how did my browser get all of this content well in order to understand this we can actually dissect this by inspecting it right click inspect and then looking at the network tabs right over here so what i'm going to do is i'm going to refresh and as soon as i refresh you can see here i get bombarded with a bunch of different networks so these are network requests that were made from the browser to a particular server in order to get content now i'm going to go ahead and click on the very first network request just to get a little bit more information about it so you can see here this is the url that was made to a particular server you can get a bunch of more information but i want to go over here scroll down a little bit and what i want to do is i want to go to this server section right over here so you can see here the server is actually engine x so engine x was the one that served us content served us this web content to our browser and that's why nginx is very typically known as a web server it is a server that is going to serve web content to our browser now that's not particularly accurate because it has another definition we'll talk about that later but it is typically used and treated as a web server so again a server that is going to serve us web content to our browser i hope that makes sense but if it doesn't i actually have a bunch of diagrams that i would like to show you so let's actually go ahead and go through this example but in diagram format so over here let's say i am on my computer and i go to airbnb.ca now typically what we would think would happen is we make a request over the internet to a server that airbnb is hosting i'm pretty sure airbnb uses aws so let's say there is a server on aws that it is hosting and once we make a request it's going to process that request and send back a response for that web content whether it's the image the content etc now you might be thinking where in the world does nginx fit in here i don't see nginx at all well right here there is no nginx but this is typically not how web content is served typically it is served something like this so instead of when i go to airbnb.com instead of making a request directly to the uh to the airbnb server i make a request to engine x an engine x server so the the the browser makes the request to the nginx server and then the nginx server makes a request to the airbnb server the airbnb server responds to the engine x server giving all the content and then nginx what it does is it sends back the content to the browser now if you're familiar with infrastructure and architecture this is actually known as a reverse proxy again treat it as a web server but the technical term for this is a reverse proxy now you might be thinking why in the world do i even want this middleman here this just seems like a whole lot of more work why can't i just make a request directly to the airbnb server well this wouldn't be a bad idea if we weren't getting a lot of traffic to our application however if we get a lot of traffic what we need to do is we need to somehow increase the number of servers that we have inside of our uh our aws uh account now the reason for this is because airbnb is very popular has millions of millions of users using it all the time if you only had one server then this over here is going to get so many requests that it's going to reach a bottleneck and eventually the latency is going to be extremely low so what we need to do is we just need to increase the number of servers that we have now by increasing the number of servers this actually presents another problem well now what we need to do is somehow we'll go to this server one time go to this server another time go to this server another time so how do we do that well this is where engine x comes into place so over here the client only only sends the the request to nginx we have one instance of nginx right over here and nginx is responsible for forwarding forwarding this request to a available server now this is actually known as a load balancer so nginx can act as a load balancer so for example right now we make a request to nginx nginx makes a request to this server gets the response sends it back maybe the next time we make a request another person makes a request it makes a request to nginx see we're still just making a request to nginx but then this time nginx is making a request to this server and then it's getting the response and sending it back now the client doesn't have to do all this it just makes a request to this load balancer and then it's just going to get the response and this is very good for scaling your application okay so i hope you can see this is just one really really important use case you can see nginx can act as a load balancer now another very important use case is encryption so if you ever go to a website you can either see that it is either http or https so if it's https that means that the server is encrypting the data that is being sent and returned so it's encrypting and decrypting this data and that's the way that we get https and we have a secure server now this actually presents a problem when we have multiple servers because if you have multiple servers we're going to have to encrypt every single one well that's why we would probably want to use nginx instead of encrypting every single instead of having this encryption and decryption process in every single server you can just have it right over here in nginx and then and then we can make a request it can be encrypted send it to the servers get it back decrypt it back over here and you can see this this is way easier to set up https now there's a bunch of other methods or a bunch of other good reasons why you'd want to use nginx and reverse proxy in general but those were the two most important you can actually look up why you would want to use a reverse proxy you can just search it up and there's a bunch of different content but i hope this diagram illustrates that now with that being said let's just go ahead and get started with actually coding out nginx in this section of the course what we're going to do is install nginx on our local machine so we can get started and work with it now i'm going to show you how to install nginx on a mac however if you are on a windows device it is not that difficult i will leave a link in the description as to how to install it now if you are on a mac let's go ahead and install it so in order to do this what we're going to need to do is open up our terminal and very simply just do brew install nginx now in order to do this you're going to need to have homebrew installed which is a package manager i'll have a link in the description as to how to install this again relatively simple now i already did this so you go ahead and do it if you haven't already now what this is going to do is it's going to create an nginx directory inside of the user slash local slash etsy file so over here we're going to have an nginx directory and if you do an ls and inspect what's inside it you can see we have a bunch of different files and folders so to open this up in vs code i'm just going to do code dot let's go over here and right away you can see we have again a server's directory and then we also have a bunch of different files now specifically what we're going to be working on is just one file and that is this file right over here the nginx.conf which is the configuration file for configuring nginx the reverse proxy so this is where we're going to do a lot of the configuration where it's going to reach out to a particular server and then serve us our web content so this is what we're going to be editing and in the future section i'm going to go ahead and just completely remove everything and start from scratch but for now what i want to do is i want to start up nginx to start up engine x all you have to do is say engine x like so and then let's go ahead over here to our browser and let's go to localhost 880. and right away you can see that we get this file right over here so what's happening is we're making a request our browser once we go to localhost 880 it's making a request to nginx nginx is then going ahead and serving this content of course we're going to always see this by looking at the network tabs so let's go over here let's do a quick refresh and then you can see here that we're making this particular request you can see that the server is indeed n gen x okay so this is looking quite good now let's go ahead and start actually returning some other uh files and folders and images that we want for our application now that we have installed nginx and we are running it locally on our machine let's just talk about some of the important nginxed terminology so in order to do this let's go to our nginx.com file and let's go ahead and explore exactly what is going on here now there's quite a bit and this might be confusing at first but if you look around you can see that there are two distinct things that exist inside of this file so the first thing that exists is something that looks like a key value pair so right over here we have the key in this case the key is worker underscore processes and then the value is one you can see here we also have another key include then we have the value of mime dot types over here default type as the key and then we have this value right over here so you can see that we have a bunch of these things but we also have this other thing right over here this events block so this block of code and then within it we have those key value pairs so instead of referring to them as things let's actually go ahead and define exactly what these are so this right here these key value pairs they are known as directives so over here we have the key we have the value this right here is a directive this right here is a directive this is a directive this is a directive now these blocks of code so with the curly braces well these are known as the context and within the context you can have directives for that specific context now right over here we have the http context so what that is going to do is define our http server and then within this http context we have directives that are going to define it all right so that's pretty much all it is that we need to know in terms of the definition let's actually go ahead and just completely remove everything right in here and we're going to start from scratch all right my good friends now that we have that all set up let's talk about exactly how we can start serving content to our web page specifically we're going to work on serving static content and what i mean by static content are simple files like html files css files and images to our web browser so let's actually go ahead and go to our terminal i'm gonna move into my desktop and i'm gonna make a new directory so with mr i'm gonna say my site i'm gonna move into my site and then i'm going to say code dots to open up a vs code instance so let's go over here and i'm going to go ahead and open that up and then right in here i'm going to create an index.html file and i'm going to add an html tag let me zoom in here html tag and then i'm going to add a body and then i'm going to add an h1 and then in here i'm going to say hello my friends i am served from nginx all right so this is looking great so now what i want to do is somehow configure engine x to serve this index.html file whenever i go to localhost 8080. so how are we going to do this well of course what we need to do is we need to define some contacts as well as directives now because what we're doing is we're dealing with http responses what we need is we need an http context we also need an events context that we're not going to use we just need to define it for nginx configuration to work so right over here what we're going to do right inside of the http we're going to define another context and this is going to be the server and then inside of the server this is where we're going to define a bunch of different directives that are going to configure this nginx server so the first thing that we want to do is we want to have the listen directive we want to say that we want to listen on port 8080 now the next directive is going to be is the root directive and this is going to be a file path that is going to contain a bunch of different files that we want to serve when we go to this port right over here now this path is going to be well this folder right over here this my sites folder so what we can actually do here is we can go here copy the path and then we can say root is this path and we can remove the index.html and just have a semicolon so we're saying once we go right over here to this port i want to serve all the files from this root now what it's going to do is it's going to look for an index.html and serve it once we hit this path so that's pretty much all it is that we need to do in order to well serve this static html so let's go ahead and save that now if we go back to localhost 880 and we do a refresh you can see that we're still getting this default so it's not serving our html and that's because what we need to do is we need to reload nginx with this new configuration this is relatively easy thing to do you can go ahead and open up any terminal i'm using the integrated vs code terminal and i'm going to say nginx dash s and then reload so that's pretty much all it is that you need to do and now if you click refresh you can see that now nginx is serving this wonderful uh uh beautifully designed uh static html file so i hope this makes sense we have these directives and then right in the server we're saying hey we want to listen on localhost 880 and this is what we want to serve so we this is this is where the file that we want to serve that we want to serve lives in this video what we're going to do is learn exactly about mime types now to explain what this is what i'm going to do is i'm going to go over here to the website and let's say i'm sick and tired of this plain old website what i need are some styles so i'm going to go over here i'm going to add a styles.css file and then in here i want to style the h1 i wanted to have a background color of pink i also wanted to have a color of aqua like so and then over here what we're going to do is we're going to rework this so we can actually go ahead and use this style so at the very top what i want to do is i want to say doctype html and then over here inside of the html tag i'm going to add a head and then i'm gonna add meta i'm over here gonna say char set is equal to utf-8 let's also add a title might as well just saying my nginx project and then lastly what i'm going to do is i'm going to add a link and this is going to link the styles.css all right so let's go ahead and refresh and now if i go here you can see that the styles actually isn't being applied now why is that is it not being served by nginx well let's go ahead and inspect and you can see if we go over here to the console you can see that well it doesn't seem as though it is served however if i go to the network tab and i were to refresh let's go ahead and refresh this after a few more reloading you can see that the styles.css is actually served to the web page so why do we not see the styles inside of this html file well the reason for this and we can actually explore this when we click on it let's go ahead and click on the styles.css i'm going to zoom in here and let's uh let's just give us a little bit more real estate actually we can just close this that looks good and now if you go scroll down over here you can see that the content type is text dash plane whereas it should be text dash css so that is the issue that we're facing right now any type of file is going to be relayed with this content type and what we need to do is we need to change it so right over here what we can actually do inside of the http context we can define all of the different types so i'm going to say something that is text dot css is going to have a css extension so it's going to have dot css anything that is going to be html well this is going to have an html extension so now what i'm going to do is i'm going to go ahead and reload this and now if i were to refresh this and click on the content type you can see it's still text plain uh let's just do a hard reload now uh now you can see that it is indeed text css so that's what it should have been i just needed to do a hard to reload because the caching was uh was showing that it is text plain so hard reload is uh command shift r now you can see that finally that the styles are being applied because now we have the correct content type now this right over here is good the approach that we did but there's so many different files out there we're gonna have to catch all of the files manually and we're going to need to add them inside of these types right over here now luckily we actually do not have to do this because nginx comes with default mime types so right over here we can actually click on the mime types and let's go ahead over here and you can see all of the different types that we can have we can have gifs we can have jpeg and it's just a bunch of different configurations that we can add so what we can do is we can actually copy this and paste it in here but we can actually do one better instead what we can do is just completely get rid of this and use the include directive i'm going to say i want to include the mime types and this is just going to include include all of the different types inside of this nginx configuration so let's go over here i'm going to go ahead and just reload the nginx server and of course we need a a semicolon right over here reload the nginx configuration let's do a hard refresh and now you can see that everything is working the exact same way so that right there are mime types in this section of the course what we're going to do is we're going to learn about the location block or the location context now this is a very very important concept in nginx because it allows us to specify certain endpoints certain pages that we can hit and then we can serve different types of html elements so for example let's go to my site and what i'm going to do is i'm going to create a directory let's call this fruits so let's say over here this is a website that is going to list a bunch of different food and then over here inside of the index.html i'm going to go ahead and just copy everything that we have right over here and we have an index.html inside of this fruits directory and let's just get rid of the styles here we don't need that and then over here let's create a list item or a ul and then an li and then inside of the li let's say mango let's add another ally let's say here strawberry and then lastly i want another ally and let's add another fruit watermelon something like so so what we want to do is when we hit the slash fruits directory we want to serve this index.html now we can do that with a location context now this is going to live inside of the server so over here what we can do is we can say location and this is going to take a second argument and this is going to be the path that we want to hit so i'm going to say fruits so this is when we go to localhost 8080 slash fruits what we want you to do is we want you to serve the index.html inside of the fruits directory so what we can actually do here is very simply just grab this same exact root and paste it in here now what this is going to do is it's going to specify is the root when we hit the slash roots directory slash user slash lathe hard slash desktop slash my site they might be thinking well is that going to serve this index.html file and not this index.html file well if that was it then yes it would serve the the this parent index.html file but when we use location what what's going to happen is it's also going to grab this right over here the slash fruits and add it in like so and that's why we didn't specify uh uh uh slash fruits at the very end because then what would what would happen is it would grab this right over here and then append it like that so it'd be slash fruit slash fruits so hope that is nice and clear so let's go over here let's go back to our terminal and let's do a reload so i'm gonna go here say engine x slash reload and then over here let's get rid of this for now we don't need that and now if we go to slash fruits you can see that now we are serving this index.html how incredible is that now let's say what we want to do now is we want to go to slash carbs for example and right now of course we're getting a 404 but what i want to do is i want to go to slash carbs and i want to get these same uh fruits over here because these are very carb heavy foods might as well reuse them for slash carbs so how are we going to do that well you might be thinking that we can just do the exact same thing we can do slash location and then over here we can say carbs and then we can specify the root over here now this is going to present an issue because well what's going to happen is it's going to go ahead and append slash carbs right over here and not slash fruit and as you can see we do not have any carbs directory inside of our application so instead what we're going to do is we're going to somehow need to specify hey i don't want to i don't want to append this to the very end of the route and i want it to be slash fruits so in cases like these instead of using roots what you would use is alias and so alias is not going to append this right over here into the very end so right here what we can say is slash fruits like so and then let's go ahead and copy that open up the terminal and let's go here reload and now if i go to slash carbs slash carbs like so you can see now we're getting the exact same index.html file so remember alias is not going to append this path over here like root would so anytime we use root you actually have to make sure you have that exact same directory inside of your application whereas with alias you do not need to have a carbs directory okay so this is looking really really good this is incredible uh but now what i want to do is i want to add another directory let's add a vegetables directory so this is a tough word to spell so just make sure you have it right it's uh v e g e and then tables like so and let's say inside of this directory we actually do not have um we actually do not have an index.html file instead what we have is let's say a uh let's say e veggies.html file so it's not index it's veggies.html i'm gonna go ahead and just copy this paste that in there and let's go ahead and name some vegetables uh lettuce uh what are some other vegetables let's just say eggplants uh and then one more vegetable onion okay so now what we want to do is well we'll say when we go to uh when we go to veggie tables veggie tub bowls like so what we want to do is we want well let's specify roots because we have a vegetables directory over here now do you think that this is going to work so let's go ahead and actually just give this a quick little test uh and then let's go here let's do a quick little reload let's go to slash vegetables slash vegetables and now you can see we're actually getting a 403 forbidden so it's not a 404 but it's a 403 because we cannot find an index.html file in here now we do not want to serve the index.html instead what we want to serve is the veggies.html so in order to do this what we can do is we can actually add another directive right over here called try files so by default it's going to look for an index.html however when we specify try files we can say here try files and we can specify a bunch of different uh a bunch of different directories that we want you to try so over here what we're gonna say is hey we want you to look for vegetables slash and then vegetable slash and then over here we're gonna say veggies.html so this is going to be from the uh the root path so vegetable slash veggies html so look for this file so if this file exists then go ahead and use it so use this file indeed now if this file does not exist what we can also say here is well use the um you can say use the index.html so this is going to be this file right over here right in the default so that's going to say hello my friends i am from nginx now if none of these exist then what we want you to do is just to just throw a 404 error and so let's actually go ahead and give this a try now so i'm going to say nginx reload and now you can see now we have lettuce eggplant and onion and so what i'm going to do now is i'm going to change this so let's say i change this to veggies with a z so what it's going to do is it's going to try to find this file and if it can't find this file then it's going to use the index.html that exists in the root so now if i refresh you can see hello my friends i am serving from nginx now if it can't find neither of these it's going to throw a 404 error so let me just quickly go ahead and maybe just rename this to let's say indie i'm going to rename them back now you can see we're getting a 404 so that is the premise of try files let's go ahead and just rename all of these back to veggies.html and html is all good another thing that we can actually do is we can actually add regular expressions to these location context so over here let's say i have a location and i have maybe a pass count and then and then over here i want a specific number so what we can do here is we can say from zero to nine you can add any number in order to actually specify this we can say tilde star as a regular expression now over here what we can say is well i just want you to take me to the root of the website and we can just add a try file right over here so it just takes us to the index.html so i'm going to say try file index dot html and if that doesn't exist just throw a 404 so now what we what's going to happen is anytime we go to slash count slash four slash count slash five slash count slash six it's going to redirect us to the index.html in the root directory so let's go here and let's just do a quick reload and if i were to go to slash count slash five you can see now we are redirected back if i go to 2 again we'll redirect it back because now we have this really awesome regular expression so i think that pretty much uh sums up the location block the location context pretty important to learn because it is heavily used inside of engine x all right everybody now that we have explored the location block let's go ahead and talk about some more advanced concepts specifically redirects and rewrites we're gonna start with redirects because they're relatively simple so i'm gonna go right over to the nginx configuration file and let's say what i want to do is whenever uh you go to slash i don't know let's just say slash crops i want to show the stuff that is inside of this file right over here so one way of doing that is of course adding another location block but we've already done this instead what i want to do is whenever you go to slash crops i want to redirect you to slash fruits and then show the html file in this root so how are we going to do that well really really simple actually this is going to be another location block so i'm going to say location and then over here i'm going to say that this location block is going to be slash crops and then i'm going to say return so i'm going to go ahead and return early this time i'm not going to specify a route and i'm going to return a 307 so this is a uh http code that is going to redirect the user and we're going to specify that we want to redirect to slash fruits so let's go ahead and uh copy this and let's go ahead and reload our nginx configuration and now what i'm gonna do is i am going to go to slash crops and now you can see we get redirected to slash fruits and if you look at the url i just went ahead and copied it you can see that whenever i go to slash crops it will always redirect me to slash fruits what if however i don't want this behavior whenever i hit slash crops i wanted to stay at slash crops but forward me the html inside of that location context inside of the fruits location context how are we going to do that let's actually look at another example so right over here we have this context right over here slash count slash number let's say i want to have another location block or another endpoint that is slash number and then slash whatever the number is and i want to forward it to this location context here however i want the url to stay like this and not essentially change the slash count slash the particular number zero to nine so in that case what we're going to do instead is a rewrite rather than a redirect now for a rewrite we do not have to specify the location directive or the location context instead we just have a rewrite directive and then over here what we can say is well i want number and then after the number what we can do is we can just wrap this in braces and over here we can say uh the back bracket w plus and this is just a variable over here and then what we can say is we want to rewrite this right over here so number slash whatever to count slash and then over here we can say dollar sign one which is just going to be the variable so dollar sign of one so this right over here is going to be this right here and then we can just close it off like so and that's all it is that we really need to do so let's go ahead and just do an nginx reload and now what i'm going to do is i'm going to go to number slash let's say 3 and you can see number slash three stays exactly the same however we are essentially getting redirected to this because what we're doing is we're relaying this index.html file in this very last section let's explore how we can configure nginx as a load balancer so let's remember exactly what i mean about a load balancer when our application starts getting a lot of users what we need to do is scale our application and the best way to scale our application is just to build multiple servers within our infrastructure now this obviously presents an issue how is the client going to make the request to the server and which server will it make the request to so this can get really complicated and that's why we have nginx in the middle so instead of the client worrying exactly where to make the request to it's just going to make a request straight to the internet and that's going to be caught by nginx and then it's nginx responsibility to forward that request to any particular server that it chooses and this is typically done by a specific algorithm now the most common algorithm is a round robin algorithm where well the first request comes in it just forwards it to this server and then sends it right back then the next request comes in well already sent it to this one that's going to send it to this one over here this server and then forward the request back and then it's going to go ahead and send it to this and then send it and forward the request back and it can just keep going back and forth in a round robin approach okay so that is what we need to configure now in order to do this what we're going to need to do is actually build a we're going to need to build multiple servers and the best way to do this is through docker because docker allows us to build uh uh server containers that are completely isolated and we can actually spin up as much as we want so that's what we are going to do so we're going to build up a docker container right over here so if you're unfamiliar with docker or and if you don't have docker installed that is a okay you can just follow along but if you do i'll have some resources below i also do have a docker crash course all right so what i'm going to do here is i'm going to create a brand new directory and i'm going to call this just server and then in here what i'm going to do is i'm going to create an index.js and then let's go to our terminal and then i am going to do cd and i'm going to move into that server directory and i'm going to do npm init to initialize a node application so let's go ahead and let's do nt npm init dash y and then what i'm gonna do is i'm gonna go over here and i'm gonna just say npm install express because that's gonna be my server so let's go here and now i'm gonna say const express is equal to require express and then let's go ahead and do const app is equal to express and then over here i'm going to say app dot and let's just make a guess request here and then i'm going to say that this is going to take in the wreck and the res and it's going to just respond with i am a endpoint or something like that something silly lastly what i want to do is i want to app.listen so i'm going to say listen on port let's say port 7777 and then over here we can have a callback we can say console.log console.log listening on port 7776 so very very simple uh node application so in order to actually start this up what we can very simply do is we can just say node index.js but a better way to do this is to go over here to our scripts and then right here we can say uh start add a new script and we can say npm run start like so so let's just say npm run start now and we should start up the server let's give it some time the server should start come on come on i was expecting the console log but let's actually go ahead and see if this server is running at this point uh let's go here and of course i'm checking nba scores so let's go here let's go to localhost 7777 i believe it was and the server cannot be reached oh okay of course so this should be node index it shouldn't be npm run start this is my fault for rushing there we go now listening listening on port 777 there we go i am an endpoint so now what we need to do is we need to actually write a docker file uh for this application this is uh how we're going to build an image and spin up multiple containers of our image again if you're unfamiliar with this it's it's a-okay i'm actually just gonna go ahead and just research docker file for express app just so we don't have to build it out ourselves so let's go here uh let's go here let's see um okay so this is looking pretty good i'm gonna go ahead and just copy this and i am gonna go over to here and i'm gonna say docker file i'm gonna paste in this docker file configuration so what is going on here so let's change this to seven seven seven seven so the first thing that we're doing is we're specifying a uh a in an image a base image so it's gonna have the basic configuration uh with just the basics of node and npm and stuff like that then what we're going to do is we're going to specify a working directory and then we're going to copy the package.json and then we're going to do an npm install to install all the dependencies then we're going to copy everything and then over here we can either do node index.js or we could do npm run start so let's go ahead and do that npm run start and that's pretty much all it is that we need again if you're interested in learning more about docker i do have a docker crash course that covers all this stuff in great detail but now what i want to do is i want to spin up let's say four different containers uh that have this particular application so let's go over here and what we can do now is well we first need to build this image this shouldn't be a very long thing so let's move into the server directory i'm going to say docker build dot and i'm going to give it the tag name of let's say my server something like that so let's go ahead and wait for this process now as this goes what you can actually do if you have the docker utility tool so you can go over here to docker and i actually have a bunch of other apps spinned up let me go ahead and remove them because they're all running on the ports that i probably want to run on so these are these are the containers what we can do now is we can actually go to the image and we should see my server eventually pop up here uh so we got the node app let's go back over here it's taking its sweet time but it should pop up right over here let me zoom in i guess it's not possible to zoom in so yeah so this is a tool that you get which is uh docker desktop in order to see all the images that you have as well as all of the different uh containers that you are running as well as some other really cool neat things now this is gonna take some time so i'm probably just gonna pause the video and i'll meet you back so it seems like it's all done now that we have built the image what we want to do is run containers based off that image so to do this what we can do is we can do docker run and then over here i'm going to specify the ports so remember this application is running on port 777 so actually go ahead and close it from here so what we can do here is we can actually do port mapping so i can say even though the container is running on port 7777 i can map it to port 111 on my machine this is how we can spin up multiple instances of our app so over here i'm going to say port 111 is going to map port 7777 inside of the container i'm also going to run this in detach mode and i'm going to specify the image that we have just created which is my server so let's go ahead and do that and now what should happen if we go to our tools and go to container we should see we have a container running on port one one one one and if we go on localhost one one one one we should see i am an endpoint so our server is running now we can do the exact same thing over here so i can say this time two two two two and then over here we can say three three three three and then lastly i can say four four four four all right so this is looking quite nice now what is it that we want to do so now let's go back to our nginx configuration and now what we want to do is we want to add a load balancer so whenever we hit ports localhost 8080 i want to hit one of these servers and then whenever i make another request i want to hit the other server and the other server on the other server i want to round robin across these four different servers that i have created so let's actually go right here to our configuration and what we're going to do here is we're going to remove the route because once we hit the slash route so just a localhost 8080 without any path we want to serve our back end so over here let's just go ahead and get rid of this uh this uh root for now so i'm gonna go ahead and get rid of that root and actually no we're not you know what we can actually leave the root as is instead what we can do here is we can say location dash and then whenever we hit this we're going to specify the round robin now right after right before the server context what we're going to need to do is we're going to specify our back end so i'm going to say here upstream and then i'm going to call this backend server we can call this whatever it is that we want and this is going to be a context and then in here we can specify all of the different backend urls that we have so well we have one one one one we have two two two two we have three three three three and we have four four four four so over here we can say one two seven dot zero dot zero dot one and then i can say colon one one one one and then i can do the exact same thing so over here let's just go ahead and say two two two two three three three three four four four four and then over here what we can very simply do is say proxy pass and then i'm going to specify http colon dash dash and then this back-end server so this name is important you have to go over here we're going to say backend server dash then we're going to close it off with a semicolon and so by default what this is going to do is it's going to around robin anytime we make a request to slash is going to round robin to this server then this server then this server and then this server and it's going to move back to this server move back to this server et cetera et cetera so that's pretty much all it is that it's going to do so let's actually go ahead and give this a quick go so i'm gonna do a nginx reload configuration seems fine now when i hit localhost 8080 you can see here i had to just quickly do a refresh you can see that it hit i am an endpoint and then if i enter it again it's not really visible here maybe we should have added a log but it is indeed round robining across all of the different servers that we have up again you're probably gonna have to just take my word for it but it is indeed doing it all right so that pretty much sums up this nginx course i hope you guys enjoyed it and i'll see you in the next one
Info
Channel: Laith Academy
Views: 449,301
Rating: undefined out of 5
Keywords:
Id: 7VAI73roXaY
Channel Id: undefined
Length: 50min 52sec (3052 seconds)
Published: Fri May 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.