What is NGINX and how reverse proxies work?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
but this is how and this is why you need a reverse proxy in your life hey everyone welcome back and in this video i want to talk a little bit about proxies and reverse proxies and especially even with reverse proxies we'll just go a little bit into nginx because i have worked a lot with nginx and it is a fantastic open source free reverse proxy software available so let's see how these work and what these exactly are also if you're new here make sure you leave a like subscribe to the channel and hit the bell icon in order to receive free daily updates on programming on code dam this video is a part of code dam's t-shirt giveaway program for the month if you want to take part and win an amazing goal damn t-shirt all you have to do is leave a comment on this video about what you think and that's it you are eligible if your comment gets a heart from code damn you will win a t-shirt for absolutely free okay let's say you created your own family website let's say this is family.com or whatever and then slash i have my name right this let's assume this page should be rendered by a node.js app right because node.js is my favorite programming language let's say the second member we have m2 for example likes vue.js a lot right and this is just a static website for example this is a obviously a server side rendered thing because we are using node.js with let's say some ejs as a system and then slash member 3 likes to have php right as their home page now you want to create this system how would you possibly create something like this just think about this for a minute now if you have watched the code damn http fundamentals course which is a free course on codedam.com you know a few things about how domain names work so you see this domain right here could ideally at a given time only resolve to one ip address which your browser would connect to right so this would be a single ip address and for the sake of simplicity let's just assume for now that you're hosting this on ec2 or some standard linux virtual machine right now the problem is that whenever your browser whenever your browser tries to connect to server the first step in the connection part is that it locates that server with the help of this ip address which you specify and does not use this path information or anything or even port numbers if you specify in this communication in the initial communication right this path and everything goes in the http body when the communication is happening so that means the option of having multiple servers is eliminated right at least right now so in terms of you know just keeping the things simple if you have a simple ip ec2 app instance you pretty much cannot point it to different servers on just different path names right they have to point on a single ip address at a given point okay so this is where the things get interesting what we actually wanted to do is we wanted to say that hey my client i want if you're on slash mehul then you want to have this node.js which might be another server like you were thinking this could be another server for let's say m2 and this could be another server for let's say m3 right but we just showed that this architecture is not possible so what we do instead of this is that we write a special program on the server itself which does this handling and we put client one layer behind right so your server right here now determines what path you are coming in this in this specific case right whether it's me hole whether it's m2 whereas m3 and the client is just connecting to a regular server right so this might as well be ec2 now now this is interesting because these can be separate ec2s and this software which does this right here is called as a reverse proxy it's as simple as that reverse proxy is just a piece of software which does this mapping in some way which does this direction of control in some way now this is not specific to just path it could be pretty much any criteria from tcp that is layer 4 i think i think layer 4 is the lowest you could go in engine x to all the way to http which is your application layer at layer seven right so you can do this with nginx you can do this which with haproxy as well these are the two proxies i have used personally and i mean i do recommend nginx but hf proxy is also good for some use cases but yeah i mean for nginx you can pretty much do this redirection on the applications whether it's node.js whether it's php whether it's view whatever with the help of these path names or you can do them on the basis of port numbers as well right so for example you can have a port number three three three three for this then three three three four and so on or you can also do it for sub domains i mean as long as a bunch of parts are different set of urls go to the same host which has this nginx proxy running you can perform this redirection right now the point is that how does this reverse proxy system actually works let's look into that so in this specific case which we are discussing what will happen is that your client actually hits nginx at the very first place and the reason it hits nginx and not your node.js or any other process is because nginx as a server is listening on port 80 or port 443 which is the standard port for http or https so when that happens now nginx is in control right and the job for nginx is not to do any processing not to compute this and that which your application does but it's just simply redirecting stuff at least in the most in most of the cases so what it will do is that you can specify let's say you have a node.js server here which listens on port one two three four right then you have a php server which is listening on one two three five then you have a vue.js server which is of course static so these are just static files but there's a static server running with the help of express or whatever and this is on one two three six right let's see so you see these applications are listening on ports which are not accessible via internet right so these ports are not accessible via internet that means ideally if they were accessible you could just write port number one two three four and bypass nginx but that's bad for security as well as for i mean for getting this infrastructure ready so what we do in fact instead is that if i'm writing slash mayhew here engine x allows me to write a rule that hey if the location is in fact mehul i want the proxy to pass at this port number so i'll say proxy pass localhost one two three four right so the reason i'm writing localhost here is because i'm assuming that node and vue.js and php are running on the same host as this engine x is running but this this need not to be the case right it could very well be in the same intranet or you know in the same subnet so that way this redirection would still work right you would have to specify a different dns name or a hard-coded ip address but this would still work now why is this thing important well you can think of nginx as the ultimate middleware to your application where you can perform all sorts of things in a very very very performant way because nginx is written in c or c plus plus i don't know but you can see it's not your node.js thing right so c and c plus plus are actually very very very very performant languages i mean closest to assembly so you can do some real performance good damage if you're writing your softwares in cnc plus plus so nginx is performant it is feature rich that means you can do rate limiting you can do web sockets connections you can do i don't know like there are so many options to do you can do load balancing you can do a lot of stuff with nginx right so but the core fundamental of reverse proxies is the following that it just sits as a front layer in most of the cases and then distributes the traffic to different applications or sometimes maybe the same application running differently just to act as a load balancer right so for example a standard use case of node.js app would be to have an nginx nginx process and then let's say four nodes node apps right and when would you do that you would do this on a four core system why because a node.js is a single threaded app therefore you have basically you're using all the four cores of your server now with the help of nginx load balancing kind of right so you can perform the same load balancing with nginx hd proxy pretty much any other reverse proxy application as well but this is how and this is why you need a reverse proxy in your life because now all the authentication all the rate limiting all that stuff could be in a single place and most importantly the tls termination as well because this is also computationally a bit expensive i won't say much because now i mean it's it's basically you know the standard stuff but tls domination is much faster than nginx compared to node.js tls domination is just converting your https traffic to http and why you need that because your applications need to be read plain text information coming right they don't need to read the garbage but yep that's pretty much it i think we covered most of the stuff which needs to be known as a back-end developer if you're just starting with reverse proxies so that is all for this video i hope you understood it i hope you enjoyed it if you did make sure you leave a like subscribe to the channel and hit the bell icon that is all for this one i'm gonna see you in the next video really soon [Music] [Music] you
Info
Channel: codedamn
Views: 7,115
Rating: undefined out of 5
Keywords: web development, codedamn, proxy server, reverse proxy, reverse proxy vs forward proxy, reverse proxy and forward proxy difference, forward proxy vs reverse proxy, how does reverse proxy work, how does proxy server works, reverse proxy explained simply, reverse proxy simple explanation, what is proxy server, proxy server how it works, how proxy server works, how reverse proxy server works, example of proxy server, reverse proxy explained, reverse proxy example, hussein nasser
Id: PAEDJrGJyaY
Channel Id: undefined
Length: 10min 16sec (616 seconds)
Published: Thu Sep 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.