What Is NGINX? Learn About Nginx Working And Why To Use It?

You may have gone through with my previous article about Nginx installation and setting Nginx with few specific configuration like installing Nginx in /var/www/html/ or setting up Nginx with WordPress and also we had a discussion for Apache vs Nginx and did some response time tracking.

In this article, we will go through with some hot topics regarding Nginx. So fasten your seat belt and take a deep dive for Nginx.

What is Nginx And Little History?

NGINX (Pronounced as Engine-X) is an open source, lightweight, high-performance web server or proxy server. Nginx was developed for a problem called C10K problem. Basically, Apache was not able to handle 10000 concurrent connection and giant sites like Google and Facebook getting millions of hits every day so they name this problem as C10K (Concurrent 10 Thousand). Mr. Igor Sysoev took this challenge and commenced development on Nginx in 2002.

Later in the year, July 2011 Sysoev decided to commercialize this product to get product support for software and the business place is in San Francisco, California. A paid service for Nginx plus subscription was launched in August 2013. Nginx got a support and a token of investments from big giants like WordPress developer Automattic and Content Delivery Network provider MaxCDN also these organizations are now have become funding partners.

Why To Use Nginx?

As already told NGINX is an open source, high-performance HTTP. And this has got much reason to use Nginx Over other web servers. Following are the few reasons that you should use Nginx

  1. Nginx As Load Balancer: NGINX provides a software-based application delivery platform that load balances HTTP and TCP applications. This load balancing is Scalable and easier to maintain. Also, it provides more flexibility for development environments.
  2. Nginx As Revere Proxy: Clients connects directly to NGINX, which then acts as a reverse proxy, load balancing requests to pools of back-end servers.
  3. Nginx As Cache Server: The web cache sits in between a client and an “origin server”, and saves copies of all the content it sees. Nginx can also cache and returns the content directly without contacting the origin server.
  4. Nginx As Streaming Server: Nginx can be used for streaming media on the server and it supports RTMP protocol. You just need to compile it with RTMP module. We will see it later.
  5. Excellent Application Delivery: Nginx provides performance and scalability at both ADC and web layers for web application delivery. NGINX made this possible to deliver applications reliably and without fear of instance, VM, or hardware failure.
  6. Platform Independent: Nginx can be deployed on any hardware of choice, like size wise and also it can be tuned and optimized for any specific work environment(physical, virtual, or cloud) need.
  7. Support for Automation: NGINX uses nginScript, JavaScript, to extend the software’s capabilities which have made easy to automate deployment with automation tools like Ansible, Puppet, and Chef
  8. Support for Cloud: This makes it easier for organizations to install a private cloud or move to a hybrid cloud-based solution.

Working of Nginx

Nginx does not create individual threads for each process like Apache. Nginx is basically event-based. Nginx follows events of a process. Below is the demonstration of a Nginx server handling concurrent HTML and Images requests.

Working Of Nginx

Example explained

In this given example main process of Nginx get divides job into Worker Connections and Worker Process. And each worker connections are managing the request made and the response obtained by users on the web server. At the same time, these requests are passed to its parent process which is Worker Process.

If not tuned a single worker connection can handle around 1024 connections by default at a time. There could be “n” numbers of the worker process for the main process of Nginx. each worker process handles different jobs so that it can handle more numbers of concurrent requests.

Finally, the worker process transfers the requests to Nginx Master Process which quickly responds to the unique requests only.

Nginx is Asynchronous; that means each request in Nginx can be executed concurrently which results in the enhancement of the virtual shared resources without being dedicated and blocked to one connection. This is the only reason Nginx is able to do the same work with less amount of memory and utilizes that memory in an optimized way.

A Useful Case Study For Nginx

Why is Nginx + PHP-FPM better Then Nginx + Apache?

Nginx is good to serve the static (js, CSS, and img) files. So you have a fixed amount of RAM handling all the regular files. Also, it uses a fixed amount of RAM because it is event driven. So you can use your RAM for other things, like PHP.

While Apache on the other hand Apache makes copies (process forks) of itself for every request like the request for PHP or a CSS file request. Each running version eats up more RAM. So you have a very low limit on the number of things it can do at one time.

Nginx is great for static file serving capabilities using Nginx+php-fpm for PHP without the additional overhead of Apache. And in addition, we don’t need Apache + mod_php just to create dynamic content, as Nginx can do it better with the lesser amount of Ram. Hence it’s good to use Nginx with php-fpm over Nginx with Apache.