What is Nginx?
In this article, we’re learning about What is Nginx and important facts about Nginx. And here I am planning to give you all the basic knowledge to start working with Nginx.
Here we are discussing,
- What is a web server?
- What is Nginx?
- Nginx Architecture and why you should learn, use Nginx?
- Nginx Configurations Settings
- How to install Nginx and how to deploy it as a web server?
What is a web server?
When you hit www.google.com on the search bar in your browser you can get the Google homepage in a few seconds. But do you know how the process has been processed to show you the page?
First of all the request is initiated by the browser in your local area network and then that particular request travels to the global area network and it reaches one of the computers which is basically assigned to serve your homepage. That particular computer sends back you the response containing all contents of the page. The response comes to your local area network from the global are network and finally, it serves your browser. The computer which is serving you a web page is nothing but a web server. The computer which serves your requesting webpage is a web server.
Web servers are computers that deliver the requested web pages. Every web server has an IP address and a Domain name.
When you try to request this URL on your browser, https://medium.com/geekculture/zuul-api-gateway-2bcdf4dd33e6
what happens is as soon as you type this URL, the request is generated and travels to your local area network and that request goes to a web server whose domain name is medium.com. Now what the webserver does is it fetches which is the page name is geekculture/zuul-api-gateway-2bcdf4dd33e6 and that is the index page that we want to fetch. After fetching it returns the response to the global area network and your local area network.
Let's talk about this webserver. When you use a web server you need software applications that can make your computer a web server. Any computer can become a web server. To convert your computer as a web server you need to install server software and connect your machine to the internet which is basically the global area network. There are various web server softwares in the world.
What is Nginx?
Nginx is open-source software and a web server for reverse proxying caching and load balancing. Nginx also provides HTTP server capabilities. And it is mainly designed for maximum performance and stability. Nginx provides functions like IMAP, POP3, and SMTP as a proxy server for email. Finally, it uses non-threaded and event-driven architecture. When you use Nginx as your web server you can make sure your page load time is definitely reduced and secure. Now you have a basic idea of Nginx and what is a web server.
Nginx Architecture and why you should learn, use Nginx?
Nginx uses Master-Slave architecture by supporting event-driven, asynchronous, and non-blocking models.
We know traditional processors or Threads mean handling concurrent connections using each and every processor thread and blocking input or output operations. Here basically if we have a process tread model what happens is each and every separate process there is a thread created requires. Now depending on the application, it can be very inefficient in terms of the CPU and memory consumptions. Just imagine you are implementing a website with a large number of web pages and so much data on each and every web page. It can be very inefficient in terms of CPU and Memory consumption. If we use separate processors it requires a new Runtime environment with the allocation of heap and stack memory and also new requires execution context. This will lead to poor performance, thread crashing, or context switching.
To avoid such kinds of complications Nginx has Master-Slave architecture. Nginx mainly focuses on the ongoing development of the advanced event-based mechanism in numerous number operating systems. Nginx uses event notification heavily and that leads to doing the specific tasks to specific processors. For example, if we have 10 tasks. You may have 10 different processors. Because of this processors run efficiently and there are a limited number of single-thread processors called workers. In the picture, we have 3 workers and for each of these 3 workers, Nginx can handle many 1000 concurrent connections and requests for a second. Nginx does not spawn a process or the thread for every connection. Worker processor basically does is it accepts a new request from a shared listen to socket and execute a highly efficient run loop and set each worker to process thousand of connections per worker. So every worker can process 1000 requests. In Nginx architecture, there is a single master and there are many several worker processors. There are also special purposes processors like the proxy cash that you can see in the picture. It has a cache a loader and a caching manager.
Nginx Master process: Master is responsible for reading and validating configurations(creating, binding and crossing sockets). It is also responsible for starting terminating and maintaining the configured number of worker processors. If you have 10 worker processors it’s not required that all the 10 workers would be running 1000 different processors on it. Few workers may have less number of workers and others may have more processors working on it. Master Node is also responsible for reconfiguring without any service interruption, controls non-stop binary upgrades, and reopens the log files and compiles embedded purls scripts.
Nginx’s everything is defined in a configuration file.
Caching Loader: Cashing Loader is responsible for checking the on-disk caching items and populating Nginx in-memory database with caching metadata. Cashing Loader prepares Nginx instances to work with the files already stored of the disks in a specifically allocated directory structure. It traverses the directories and checks caching contents with the metadata updates the relevant entries in shared memory and then also exists when everything is clean and ready to use.
Caching Manager: responsible for cache expiration and invalidation. It stays in the memory during normal engines operation and is restarted by the master process in case of failure.
Usecase: If you request a page once. After some time you’re going to request that same page again. What happens is this page you want to retrieve back does not go for the complete process of requesting and taking response. It is stored in the cache and the cache loader and the cache manager looks at which page are you searching for and they send back the cached page immediately.
Why should we use Nginx?
- Ease of Installation and Maintenance — It is very few steps to install Nginx onto your local machine and make sure that you have a web server working for you and it is very easy to maintain because you know you just need to type a single command to just restart or stop the servers. And also you can do many functions.
- Reduce the wait time of the users — You don’t need to wait a lot of time when you are using Nginx.
- Improve Performances and Load Balancing— Nginx improves the performance. If you have 2 or more web servers running at the same application Nginx can accelerate the performance by routing traffic to those web servers in the way that you know it enhances the overall speed. It also offers Load balancing so basically, a load balancer is a device or a service that distributes the traffic load on 2 or more web servers. It provides fault tolerance and increases performance.
- Scalability — As we all know that you know traditional web servers like Apache and Internet Information Services(IIS) can serve incoming requests without any problem until the number of concurrent requests reaches a certain limit. If the requests increase the performance will decrease eventually so that is because of the way those servers with designed and also the model by which to serve web requests. In such cases, if you use Nginx it can easily handle the increasing number of concurrent requests.
- On-the-fly Upgrades — Nginx is one of the very few systems that can be patched or upgraded without having to take downtime and disrupt your business.
Nginx Configuration Settings
The core settings of Nginx are mainly configured in the Nginx config file. The config file basically structured into context. There are mainly 2 types of contexts that you can set up.
- Events Context
- HTTP Context
This structure of configuration files enables you to mention some advanced layering of your configuration as each and every context can have nested context that inherits everything from the parent but can also override a setting as needed.
Aparat of these contexts the most important pieces of the Nginx config file are,
- Worker process — this is a setting that defines the number of worker processors that Nginx will use. Since Nginx is single-threaded this number of workers is equal to the number of CPU cores.
- Worker connections — Maximum number of simultaneous connections for each worker process. And tells how many people can simultaneously be served by Nginx.
- Access login and error logs — these are the files that Nginx will use to log any errors and access attempts. These logs are generally reviewed for debugging and troubleshooting.
- Gzip — Setting for the gzip compression of Nginx responses. Enabling this one with the way the sub-settings that by default are commented out will result in quite a big performance upgrade.
So in this tutorial, we discussed What is a web server? What is Nginx? Nginx Architecture and why you should learn, use Nginx? Nginx Configurations Settings. Let's discuss How to install Nginx and how to deploy it as a web server? in another article. I hope you all get a basic understanding of Nginx.
Thank You!