What is Nginx
It is a free and open source web server publicly release in 2004, Nginx can also be used as reverse proxy, load balancer, mail proxy and HTTP Cache.
Nginx was created by Igor Sysoev, as an attempt to answer the C10k problem. Where C10k is the challenge of managing 10,000 connections at the same time. Today there are even more connections that web servers have to handle. Because of this reason, nginx offers event-driven and asynchronous architecture. This feature makes nginx as the most reliable servers for scalability and speed.
There are two version of Nginx,
- Open Source
- Nginx Plus
Nginx Plus is sold as a subscription model. It offers features in addition to Nginx Open Source, such as
- active health checks
- session persistence based on cookies
- DNS-service-discovery integration
- Cache Purging API
- Dynatrace New Relic plug-ins
- Active-Active HA with config sync
- Key-Value Store
- on-the-fly with zero downtime updates upstream configurations
- and key‑value stores using Nginx Plus API
- web application firewall (WAF) dynamic module
Why use Nginx
Nginx provides multiple services such as reverse proxy, load balancer, and rate limit network services. Nginx can handle the logging, blacklisting, load balancing and serving static files while the web services focus on what they need to do.
Before knowing the process of Nginx, let's take a look at how a web server works. When we request to open a webpage, the browser contacts the server. Then the server looks for the requested files for the page and sends it to the browser. This is the only simplest kind of work for a request.
The above example is also considered as a single thread. Traditionally, web servers like Apache create a single thread for every request, but Nginx does not work that way. Nginx performs with an asynchronous, event-driven architecture. It smartly follows events of a process.
Nginx divided its job into the worker process and worker connections. Here, worker connections are used to manage 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 called the worker process.
Let's see an example for Nginx server handling concurrent MP3 and MP4 file requests:-
From the above diagram, we can see that a single worker connection can handle around 1024 connections at a time. It is the best ability of a worker connection. There may be 'n' numbers of the worker process in Nginx based on the type of server we have and each worker process handle different jobs so that it can handle more numbers of concurrent requests.
Finally, the worker process transfers the requests to the Nginx master process which directly responds to the unique requests only.
Since worker connection can take care of up to 1024 similar requests. Because of that, Nginx can handle thousands of requests without any difficulties. It is also the reason why Nginx became an excellent server for busy websites like e-commerce, search engines, and cloud storage.
How to install Nginx
sudo apt update sudo apt install nginx
# Install Nginx brew install nginx # Start Nginx using launchctl load /usr/local/cellar/nginx/1.17.0/homebrew.mxcl.nginx.plist
After installation, open you server ip in you browser You should see
Welcome to Nginx
How to restart nginx
sudo service nginx restart
How to check Nginx Logs
tail -f /var/log/nginx/error.log