Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files

LearnCode.academy · Beginner ·🌐 Frontend Engineering ·12y ago

Key Takeaways

Configuring Nginx as a proxy to an Express application, load balancer, and static cache files

Full Transcript

all right in this video we are going to be adding uh engine X as a front end to our node application which is running on Port 3000 and we're going to be setting it up as a load balancer so as our application grows and grows and grows we can add a second third fourth server and it will load balance between the four um and then we're also setting it up to where uh it will cash some static assets to imp increase performance so I've got my application server we'll call this the do two box uh the two box is running on Port 3000 like a normal uh node application would you can see as I load requests they're coming through I'm sshed in over here so you can see it um and here's a four box you can install engine X on the same box if you don't think your application's going to grow but I already know I'm planning on multiple boxes to come so I'm going to install engine X on its own box um you know to better handle the load so the four box currently does nothing this is where the domain will resolve is to this address and I'm sshed into the4 box let's go ahead and uh get engine X set up the first thing you're going to want to do is pseudo apt get update which I've already ran I don't need to do that um so then you're going to go pseudo aptg install engine X which I've already done it says it's at the newest version so now engine X is installed you can go CDC slh not not SL hosts sline X and then you can see here's my my engine X I've got a default engine X configuration file let's take a look at that um let's vim and look at this guy you can see by default it's started four worker processes you're going to want kind of as a rule one worker process per CPU that you're running that's kind of the most efficient way to run engine X um it's running 7 768 uh connections and all this other stuff and as you can see let's scroll down here these are all decent things to look into to get to know what they are um but what it's doing is it's also including any file in the sites enabled directory so that's we're going to add our that's where we're going to add our specific configuration so let's go into the sites enabled folder and then there might be a default file in here if not you're going to want to create it um default let's go ahead and create that I'm going to go over here here's a gist that I've created for you with the code that I'm going to be using so I'm going to copy the Upstream project let's paste that and I'm also going to copy the first portion of the server and I'm going to wrap that out so what I've done here is I've got three nodes I've got my2 node My3 node and my five node um let's go ahead and comment out the other two and save my file um what I'm also going to be doing here's the actual server that's going on is I'm listening to Port 80 which is your default port for your url and then I'm going to be forwarding that on to project which is up here and so any nodes I add will automatically get load Balan let's save this and then let's go pseudo service engine X start there we go starting engine X and now I should be able to refresh and it's automatically forwarding it to the two server you see as I load this then the requests are coming through to the2 server awesome let's say the load has increased um and we should now add some nodes I've got some more nodes going on here um let me drag one over here for now let's go ahead so we're getting a lot of traffic constantly we don't ever want to have to shut down engine X let's go back into our default file Let's uh uncomment these two servers save and quit nothing has changed if you notice I'm loading these it's still all the requests are going through to my2 server it's cuz I have to reload not start reload engine X so I'm going to go pseudo service engine X reload that's going to keep it going it's going to reload the configuration file and now let me go ahead and minimize you bring you over here all my requests are now getting load balanced between all three boxes which is awesome now let's have some fun and do some bench testing shall we um let's go ahead and I'm going to turn those back off turn your you off turn you off save and reload okay let's run some bench testing here let's run an a pachy bench pretty basic I'm going to run 40 concurrent requests going to run a th000 requests and we're going to hit the do four server which is all going to be coming through here right now running 100 200 300,000 requests you can see that took about 5 and a half seconds to complete let's go ahead and go back into defaults add my load balancing here reload my configuration file and let's start seeing this fly I'm going to make you take up a little different space here there we go let's run that exact same benchmarking now you can see the requests are flying through all of them and that's going way faster instead of 5 Seconds that took about 3 seconds to run great let's go ahead and add some static caching now I'm going to edit my default file again and let's cash static assets which is this little piece P of code right here we're basically going to take anything CSS JJs uh PNG any images and we're going to cash them they're not going to they're not going to reload to express for 168 hours so let's go ahead and add this in there we go uh let's reload that service and now let's go ahead and bench test Style sheets I think that's stylesheets style.css I believe that's it uh let's go Ahad and bench test that guy there we go so that flew through as you can see um almost no time whatsoever complete requests uh let's give that a bench actually before I'm kind of static caching that let me edit my default here and let's go ahead and comment this whole thing out I didn't give you a chance to see what it does without it let's save let's reload there we go you can see the stylesheets are all coming through here um our requests are coming in at a rate of 20 milliseconds to 194 milliseconds that took about 1.3 seconds let me edit that again reload boom that took almost no time at all we went down from 20 milliseconds to about 4 milliseconds which the highest is still around 210 but that went down to3 seconds so as you can see static caching your assets with engine X is way faster than serving them up with Express so that's kind of a little bit of fun with engine X um you can cash as I think I gave you some examples of how to cash some different you can cash specific API URLs uh which is nice to cut down the load on your servers have some fun with that and have a great day

Original Description

Nginx ROCKS! In this Nginx Tutorial, we're going to setup Nginx to receive http requests to our domain name (port 80), then proxy to our Express.js Node.js App, which is running on port 3000. View Nginx Configuration Code here: https://gist.github.com/learncodeacademy/ebba574fc3f438c851ae We're also going to set it up Nginx load balancing, so the http requests will get evenly distributed between all of our servers. See full documentation on Load Balancing (upstream) here: http://nginx.org/en/docs/http/ngx_http_upstream_module.html Lastly, we're going to setup static file caching on Nginx, so our css, js and image files are only served from our Node.js application the first time...this DRASTICALLY improves application performance. -~-~~-~~~-~~-~- Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device" https://www.youtube.com/watch?v=fgOO9YUFlGI -~-~~-~~~-~~-~-
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LearnCode.academy · LearnCode.academy · 43 of 60

1 Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
2 Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
3 How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
4 Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
5 Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
6 CSS Tutorial - Web Development Tutorial for Beginners (#5)
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
7 Node.js tutorial for beginners - an introduction to Node.js with Express.js
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
8 Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
9 Javascript Tutorial - Programming Tutorial for Beginners Pt 1
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
10 Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
11 AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
12 WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
13 YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
14 BOWER! - Streamline Web Workflow with Bower Package Manager
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
15 Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
16 GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
17 GITHUB PULL REQUEST, Branching, Merging & Team Workflow
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
18 Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
19 jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
20 jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
21 jQuery Tutorial #1 - jQuery Tutorial for Beginners
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
22 Node.js MongoDB Tutorial using Mongoose
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
23 Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
24 WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
25 jQuery Tutorial #4 - DOM Traversal with jQuery
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
26 jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
27 jQuery Tutorial #6 - Building a jQuery Image Slider
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
28 jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
29 jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
30 jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
31 jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
32 How to put your website online - how to FTP to a domain & upload files to a webhost
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
33 Basic Terminal Usage - Cheat Sheet to make the command line EASY
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
34 SSH Tutorial - Basic server administration with SSH
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
35 Vagrant Tutorial - Running a VM For Your Local Development Environment
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
36 Sublime Text Favorite Packages and Workflow
Sublime Text Favorite Packages and Workflow
LearnCode.academy
37 What Makes Javascript Weird...and AWESOME - Pt 1
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
38 Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
39 Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
40 FREE REST API - Practice Developing Javascript AJAX Apps with this API
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
41 Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
42 Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
44 Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
45 Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
46 Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
47 Web Development Advice - Interview with Dev Tips
Web Development Advice - Interview with Dev Tips
LearnCode.academy
48 How the Internet Works for Developers - Pt 2 - Servers & Scaling
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
49 How the Internet Works for Developers - Pt 1 - Overview & Frontend
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
50 HAPROXY vs NGINX - 10,000 requests while killing servers
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
51 Node.js Cluster - Boost Node App Performance & Stability with Clustering
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
52 Web Dev Training with Treehouse
Web Dev Training with Treehouse
LearnCode.academy
53 What is Node.js Exactly? - a beginners introduction to Nodejs
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
54 How to deploy node.js applications #1 - spin up a server
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
55 Deploying node.js applications #2 - provision server & setup flightplan
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
56 Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
57 Mobile Web Design - Coding Workflow For Mobile Websites
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
58 WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
59 GRUNT TUTORIAL - Grunt makes your web development better!
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
60 STOP USING FTP!  - How to Deploy with Flightplan over SSH
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy

Related Reads

📰
HTML vs JSX — What You Actually Lose When You Switch
Understand the trade-offs when switching from HTML to JSX, and how it affects your browser, bundle, and workflow
Medium · Programming
📰
A modern marketing site without a modern front-end stack
Learn to evaluate modern front-end stacks for marketing sites and why it matters for performance and user experience
Dev.to · NessFlow
📰
7,798 Components. Import One, Ship One.
Learn how to optimize frontend package management by importing and shipping only necessary components, reducing bundle size and improving performance
Dev.to · James Coombs
📰
State Management in Front-end Web Development: Mutators
Learn how mutators simplify state management in front-end web development and improve code maintainability
Dev.to · Abbey Perini
Up next
Another Linux Distro Dropped Deepin Desktop
Brodie Robertson
Watch →