Here's why you need Nginx as a Reverse Proxy for your Node.js app

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
node.js is great it can not only deal with your business logic but also act as a web server meaning it can do load balancing it has a cluster module it can also do rate limiting and so many things now the question is why are so many people still using engine X as a reverse proxy together with nodejs this is what we're going to learn in this video so if you're ready let's get started I always like to start my videos by giving a bit of context meaning before we start talking about why to use a reverse proxy we first need to Define what a reverse proxy is okay in our case it's an engine server that lives within our virtual private Cloud what it basically does it it it accepts requests from the client that are incoming and then it modifies this request on the Fly modifies meaning it can modify its headers as it usually happens or it can also modify its body in some rare case and and then it redirects this modified request to one of our node servers for example this actually we have three the cool thing is that it also works the other way the node server now sends a response to the client and our middleman our reverse proxy AKA engine X again modifies the headers if necessary and then sends it back to the client okay now that we understand what the reverse proxy is and how it operates we can also understand the benefits that it brings all right so the first benefit is an SSL encryption as you know every modern website communicates via htps in some rare cases you can see HTTP but it's usually not a good practice what can happen with HTTP or where you would you could see it is actually within your Cloud virtual private Cloud for example this node server can communicate with this node server and with your engine server with HTTP this is fine if you don't want to add overhead to your network but there's a hard requir M whenever we have a communication with the outside world meaning our client we always have to talk so this part always has to be communicated via https this is a hard requirement okay and now you can argue that why do I need an engine server if I can set up an htps communication with my node.js server well the thing is I already mentioned it in my previous videos as well if you follow my Advanced njs playlist is that this kind of configuration for example SSL encryption and many others that we're also going to talk about you don't want this configuration to live within your node server your nodejs server has to only take care of the business logic of your application everything else can basically be dedicated or outsourced your reverse proxy all right and the way you would set it up is very easy an enginex server usually has this engine x.com configuration file that you would find in every server that's running engine X and it's of course sometimes hard to read but in this case it's actually very simple so the most important block here is the HTTP block and here it has a block called server what it basically says is that hey I'm listening on Port 80 which is an HTTP port and I'm listening on these specific domains and if I receive a request on this domain on this port I'm going to return an HTTP status code 301 which translates to redirect and I'm going to redirect you to this host now the funny thing is that this host that we're being to being redirected to is actually our second server block and now this one is listening on Port 443 which is an htps Port all right again the same domains that we can Define and the interesting part is that here are our here's our configuration for SSL again very easy to put your key private key and and certificate and you can actually update them if needed with a simple Chrome job so it's super easy to maintain it versus if you would maintain it within your nodejs server so as you can see this is already a huge benefit now before we start talking about the second point which is buffering I quickly want to talk about the application that I'm using this Blackboard that you're seeing it's called eraser Well I have to say this is the first time I'm using it for one of my videos but I'm already loving it it's super nice it's super flexible and very intuitive to use and you can create different types of diagrams such as a flowchart uh entity relationship and Cloud architecture diagram that I'm actually having now to basically demonstrate my architecture and you can even create diagrams with an help of an AI basically you type code and it creates diagrams I'm pretty sure I'm going to use it in one of my future videos but what I wanted to say is that I'm already Ling this application and I would recommend recommend you to check it out it's totally free to use and shout out to my friends at eraser for sponsoring this video and now the second Point buffering buffering is kind of interesting so let's say a client makes a request to our server wants to fetch some data and our no server receives this request does some processing and wants to send the response back to the client now imagine this response is a bit bulky let's say it's like 8 megabytes all right it's not too small an interesting question arises do we send this 8 megabytes right away to our client or like one in one go or do we send it in small chunks the thing is the answer depends on the internet speed of your client if the internet speed of your client is slow this request that is coming here to your Cloud to your engine X server theoretically is going to be hanging there for a long time because we're going to be sending the response like one by one every time establishing a connection and it's really not good and actually hacker can exploit this function with a attack called slow loris where they're sending hundreds of request very slow requests that are hanging on your on your server and can bring your server down that's why we need buffering to basically tell the node server hey I need this answer for this request the node server starts sending pieces of response to your engine X server first before sending to the client and slowly this engine X server collects the small responses until it collected everything the whole big thing and then it sends it to the client at once and this is cool the thing is that we can add it by literally adding these four lines of code and of course changing the settings as we want how cool is that you wouldn't be able to do that within your nodejs serice so easily okay the next point is recovery AKA error handle meaning let's say one of your no service is down or the user is requesting a page that actually doesn't even exist what do you do then well of course you define some error pages here or you define some redirects if you want to and of course you can define a custom error page for every type of an it to be status code that you get so it's very flexible as you can see and also you can copy all this code for an engine X configuration from my GitHub page that you're going to find in the description so if you want to use it for your own engine X server feel free the next point is load balancing and you can say okay no not actually has a cluster module it can do load back baning hey what come on we're talking about Enterprise applications applications that are at least having like 500 requests per minute so there is some pressure then load balancing with engine X would be just an amazing thing to do first of all engine x super powerful with load balancing and you can enable it by adding simply this block where you define the service that you want and their IPs with ports and then the rest is taken care of and of course engine X supports different algorithms for load balancing such as Rand Robin or random assignment or looking at which server has the least CPU usage and I also have a video on load bers so check it out if you want to learn more but basically it's very powerful and can do that too the next point is Enterprise route well what is it let's say we have this node server 3 and we want to reserve This Server specifically for product searches because let's say we're Amazon we have a huge catalog of products and product Searchers are actually getting the fair amount of request from the clients so we want to redirect these specific type of request to only This Server the rest can tackle whatever all right and again it's very easy to to do it with a reverse proxy you can simply Define a route like let's say uh search and everything that goes to this route with an asteris at the end because it can have sub subpath is going to be routed specifically to This Server as easy as that the next point is jiz compression most of the website have jiz compression because it's efficient and again the way way to enable it with engine X is very simple simply two lines and of course you can Define which files you want to be jizi and it's usually recommended to jip text files such as just PL text or css or JavaScript files image files I wouldn't recommend to jip them because JPEG and PNG files are already compressed so there's no reason to jiz them even more and with that understood it's as simple as that the next point is actually caching caching static comp let's say your not server is actually sitting or serving your WordPress website and you have some static asset instead of doing it with nodejs you can configure your engine X server to do that and actually engine X is known for being very good at caching static content specifically so let's say you have some im images that you know they're not going to be changed in any time in the future you can simply add some cash headers and now your static assets are cached in the most efficient manner this is it guys if you like this kind of playlist check check out my Advanced node playlist or system design and architecture playlist I think you're going to find them useful as well and of course if you want to support my channel support my sponsors as well in this case it's eraser IO all right guys I will see you in the next video goodbye
Info
Channel: Software Developer Diaries
Views: 10,062
Rating: undefined out of 5
Keywords: software development, software developer, programming, software engineering, javascript, web development, coding, nodejs, nodejs api, nodejs reverse proxy, nginx, nginx reverse proxy, nodejs ssl encryption, nodejs https, nodejs caching, nodejs aws, aws reverse proxy, aws load balancer, aws caching, nginx tutorial, nginx conf
Id: -QcQd3Bkc9o
Channel Id: undefined
Length: 10min 17sec (617 seconds)
Published: Sat Dec 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.