Why Nginx and How it works?

Why Nginx and How it works?

A brief look into why we need Nginx & it's working

Β·

4 min read

Introduction

Created in 2004, NGINX (pronounced as "engine- ex") is basically a web server, reverse proxy,mail proxy and load balancer. Before you worry about these terms, let me tell you that we will dive into them shortly.

But before that, we must understand the problem which is solved by Nginx .By understanding this, you would get the whole point of this being created and would be more eager to dig into this topic ;)

Case Study

Lets say we are developing an application, we tend to build and host it on our local environment for testing purposes so as to check if its working fine which is usually the case of any application in its development phase. But someday, we may also need to put it on a big gigantic server like AWS, heroku or digital ocean.. In this system we have a machine lets say, ubuntu. We have installed database let's say Postgres and all the dependencies used for our app and its running.

Postgres db in your system is set at port 5432 by default

blogs (1).png

We spin off our machine and then it runs on a port "3000". We can now invite users to use our application on this. But as users grow, we will realize there will be traffic and it would lead to server load failures since server wont be able to handle a lot of users.

So, we can run another instance of the application using pods or Kubernetes, whatever you prefer. We will have another instance running on port lets say 4000. Hence we can redirect some users to this instance so as to decrease the load from previous port.

blogs (2).png

And some users can go to other port but it wont be that convenient for them to move from port to port. Again if traffic grows again, you may have to start another instance on 5000 So , new users may be confused weather to start their application on which port. Also it wont be god for us aswell to redirect the users ourselves and create so many instances.

This is the classical problem that is solved by Nginx!

Solution

So through nginx, you know few ports are by default 443, 80 served by Browers. It puts a barrier wall so that user doesn't have to bothered where to connect. Users will just look nginx port as a gateway from where they will use the app. It takes all the requests from the user and wherever this app is serving it will talk to nginx. Nginx manages the load and checks which server or instance has a low amount of load and then redirects new users to that port. This is how nginx acts like a load balancer

blogs (3).png

We can also dedicate a route to an instance that will only be used from one server. Let's say we have new route page on a new server so incoming sudden traffic may be expected in that port. So nginx loads up a cache that all the requests for this particular route will be stored in the cache so that it doesn't have to talk to the database again and again and serve the requests more faster. This is why nginx is also called reverse proxy

Nginx acts both like a Web server and a proxy.

Some high-profile companies using Nginx include Autodesk, Atlassian, Intuit, T-Mobile, GitLab, DuckDuckGo, Microsoft, IBM, Google, Adobe and Salesforce etc.

Functions

Nginx is built to offer low memory usage and high concurrency. Rather than creating new processes for each web request, Nginx uses an asynchronous, event-driven approach where requests are handled in a single thread.

  • Reverse proxy with caching
  • IPv6
  • Load balancing
  • FastCGI support with caching
  • WebSockets
  • Handling of static files, index files

=========================================================

References:

commclassroom.org/fullstack-roadmap

kinsta.com/knowledgebase/what-is-nginx

youtube.com/watch?v=WuH0T9FHDZ4&ab_chan..

Thank you for reading the blog.πŸ™ŒπŸ»πŸ“ƒ

πŸ”° Keep Learning !! Keep Sharing !! πŸ”°

πŸš€ Feel free to connect with me :

Keep Growing and don't forget to have fun :) 🍻

Β