This article focuses on using Nginx with Nodejs and Docker. Follow this article to Learn how to dockerize a Nodejs application.
- Install NodeJs on your computer.
- Basic understanding of Express and Nginx
- Knowledge of Docker containerization
⚫ Introduction to Nginx
⚫ Nginx terms
⚫ Serving static files
⚫ Mime types
⚫ Location block
⚫ Running Nginx with Docker as a HTTP load balancer
NGINX is pronounced as "engine-ex" is open source software that can be used for web serving, reverse proxying, caching, load balancing, media streaming, and other purposes.
Nginx can be used effectively as an HTTP load balancer to distribute traffic to multiple application servers and to improve web application performance, scalability, and reliability.
Nginx excels at serving static content quickly, has its own robust module system, and can forward dynamic requests as needed to other software.
NGINX improves content and application delivery, security, and scalability and availability for the internet's busiest websites.
To download and install Nginx, go to this link and follow the installation instructions.
After downloading the zip pack, unzip
Cut and paste the unzip folder in your Program File Folder. Double click the
nginx.exe or run with cmd
Another method is to use the following command to start ngnix in the project folder.
Navigate to localhost:80 in your browser.
Navigate to the network tab in the developer tool and inspect the browser. We can see that we are using nginx to serve our web content to our browser highlighted in green.
To serve static files using nginx, let's create a new folder, a new file called
index.html linked to a css
Now we can serve our custom web content using the nginx.conf file
The rectangle highlighted in
orange is the file path to the root folder for the content we want to serve when we listen to the port we allocated to the server context. Then we reload Nginx with the
nginx -s reload command
Our content has just been served, but there is a problem: the CSS style has not yet been loaded; we will address this shortly.
Mime types are media that indicates the nature and format of a document, file, or assortment of bytes. source
Let's update our nginx.conf file to accommodate our style sheet.
Why the need to Load-Balance our application?
Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault‑tolerant configurations. source
I hope this post was useful in getting started with Nginx as a static content server and load balancer.
For more information, consult the official documentation.