One of the fundamental units of the internet is the web server. These web servers are computers built to deliver the requested webpage. Every web server has an IP address and a domain name. To make your computer a web server, you have to install web server software, such as NGINX, XAMPP, Apache, Tornado, Caddy, or Microsoft Internet Information Services (IIS). In this article, we’ll focus on NGINX.
What Is NGINX?
NGINX is open-source web server software used for reverse proxy, load balancing, and caching. It provides HTTPS server capabilities and is mainly designed for maximum performance and stability. It also functions as a proxy server for email communications protocols, such as IMAP, POP3, and SMTP.
The NGINX Architecture
By implementing event-driven, asynchronous, and non-blocking models, NGINX uses master-slave architecture.
It also uses an advanced event-based mechanism in many operating systems. Additionally, NGINX uses multiplexing and event notifications and dedicates specific tasks to separate processes. For example, if you have 10 tasks, 10 different processes will handle each of them. NGINX processes highly efficient run loops in a single-thread process called workers.
- Workers accept new requests from a shared listen socket and execute highly efficient run loops inside each worker to process thousands of requests.
- Masters read and validate configurations by creating, binding, and crossing sockets. They also handle starting, terminations, and maintaining the number of configured worker processes. The master node can also reconfigure the worker process with no service interruption.
- Proxy caches are special processes. They have a cache loader and manager. The cache loader checks the disk cache item and populates the engine’s in-memory database with the cache metadata. It prepares the NGINX instances to work with the files already stored on the disk in a specifically allocated structure. The cache manager handles cache expiration and invalidation.
Benefits of NGINX
Using NGINX comes with several benefits, including the following:
- Reduces the waiting time to load a website. You don’t have to worry about high latency on your websites, therefore providing a good user experience.
- Speeds up performance by routing traffic to web servers in a way that increases the overall speed. This feature provides a good browsing experience to your users.
- Acts as an inexpensive and robust load balancer.
- Offers scalability and the ability to handle concurrent requests.
- Allows on-the-fly upgrades without downtime.
Limitations of NGINX
Although NGINX has lots of benefits, there are some limitations, too. It has a low level of community support and contributions from developers, leading to fewer features and updates. Both maintenance and setup require expert knowledge.
Although NGINX is free to use, it also has a paid version called NGINX Plus, an all-in-one load balancer, content cache, web server, API gateway, and microservices proxy that costs $2,500 per year.
Use Cases
An instance of NGINX can be configured as any of the following:
- A web server. This is the most common because of its performance and scalability.
- A reverse proxy server. NGINX does this by directing the client’s request to the appropriate back-end server.
- A load balancer. It automatically distributes your network traffic load without manual configuration.
- An API gateway. This is useful for request routing, authentication, and exception handling.
- A firewall for web applications. This protects your application by filtering incoming and outgoing network requests on your server.
- A cache. NGINX acts as a cache to help store your data for future requests.
- Protection against distributed-denial-of-service (DDoS) attacks.
- K8s. These automate deployments and scaling and manage containerized applications.
- A sidecar proxy. This routes traffic to and from the container it runs alongside.
Getting Started With NGINX
The core settings of NGINX are mainly configured in the nginx.conf file. It’s structured into two main contexts, the event and HTTP context. The config file contains the following:
- Worker_ processes. This defines the number of worker processes NGINX will use.
- Worker_ connections. This is the maximum number of simultaneous connections for each worker process. It tells NGINX how many people it can serve.
- Access_logand error_log. These files log errors and access attempts.
- Gzip. These are the settings for the gzip compression of NGINX responses.
How to Install NGINX
In this tutorial, we’ll show you how to install NGINX on Linux. Open your Linux machine and run an update using the command below:
sudo apt-get update
Next, run this command:
sudo apt-get install nginx
Then, enable your firewall with the following:
sudo ufw enable
To verify NGINX is installed, run the following:
nginx -v
You can run the command below to find out if NGINX is running:
sudo ufw status
After running this command, you should see the following:
status: active
To check whether your NGINX server is working fine, run the following:
sudo systemctl status nginx
You should see a success message if everything is working properly.
Common NGINX Issues
Some of the common NGINX issues include the following:
- The NGINX server is consuming too much of your CPU power even though there isn’t much traffic.
- NGINX workers crash because of a lack of memory on your server. This is related to excessive CPU consumption and insufficient memory.
- Upstream timeout errors like “502 Bad Gateway.” This usually happens when one of your servers receives an invalid response from another server. An overloaded server can also cause it. Solving these issues will require the user to clear cache and browser cookies, which can be an unpleasant experience.
Introducing Papertrail
Tracing logs on your server and debugging errors can help you resolve these issues. And you can better monitor your logs using SolarWinds® Papertrail™, an easy-to-use log management software designed to help you analyze, tail, and search your log messages. It’s fast to set up and includes powerful integrations, team visibility, and a lot more. You can get started with the free version.
If you’d like to learn more, you can take an even deeper dive our article, “Logging With NGINX—How to Configure It and What to Watch Out For.”
This post was written by Jethro Magaji. Jethro is a front-end engineer who’s passionate about the tech world and uses creative thinking to solve business problems with a user-centered approach.