Introduction to Nginx 101

for web serving, reverse proxying, caching, and load balancing

Shamoda Jayasekara
Tech It Out
5 min readJul 5, 2021

--

As a developer, you may develop your application functionalities in your development environment. And now it's time to package your application and host it on a web server. Your application might consist of some backend web services and a set of static files like HTML, JavaScript, and CSS. So, NGINX will be the ideal webserver to host your application and get the best performance out of it. Let’s find out why?

NGINX is the younger brother of Apache, but it’s not just a typical web server. It can act as a lightweight but high-performance load balancer and reverse proxy server. It also capable of content caching, compressing, and many more. Apache uses a process-driven architecture, which means it will create a separate thread for each client request. But Nginx uses an event-driven architecture to process multiple client requests within one thread. When it comes to performance, Nginx is lightweight and much faster than his elder brother Apache. 😊

Installation

So, I’m gonna install NGINX on my fresh EC2 instance which is running on Ubuntu. Actually, installation steps may be varied from the Linux flavor to flavor. But it’s really easy and straightforward. You can find the installation guide according to your Linux flavor from here 👈.

Okay! First, we need to update all the packages, and then let’s install NGINX from the official Ubuntu repository.

You can run the below command to verify the installation and it will return the NGINX version as well. In this case, we’ve installed the 1.18.0 version of NGINX.

Basic Configurations

Default configuration root for the NGINX server can be found at /etc/nginx/. Within this directory, we can find default configuration files and other related folders which instruct NGINX on how to behave.

NGINX root directory

This is the root structure of NGINX version 1.18.0. But if you are using a newer version like 1.20.1 or above, some folders like sites-available, sites-enabled, modules-available, and modules-enabled cannot be seen. But the underlying concepts are exactly the same.

nginx.conf

nginx.conf file is the most important file because it will act as the default configuration entry point of the NGINX service. In this file, a set of global configurations like worker processes, worker connections, dynamic modules, and other global configurations will be defined.

worker_processes: This defines the number of worker processes that NGINX uses. But you already know NGINX is a single-threaded server. Therefore, most of the time this number will equal to the number of CPU cores.

worker_connections: this directive defines the number of simultaneous connections that each worker_process can serve.

access_log and error_log: NGINX will use these files to log all the access attempts and errors which will be encountered.

nginx.conf file also includes the top-level HTTP block or the context, which loads all the configuration files from conf.d and sites-enabled directories dynamically.

sites-available and sites-enabled directories

With NGINX you can create multiple virtual hosts. Configurations related to all the virtual hosts or sites will be stored in the sites-available directory. But these sites will not go live until we create a symbolic link from that configuration file to the sites-enabled folder. We can simply copy the config file to the sites-enabled directory as well, but it's better to go with symbolic links because the symbolic links will ensure that only one copy of each file will be in the sites-enabled directory.

Server context

This will be the very basic structure of a server context. This server will listen to port 80 of our EC2 instance and when a request comes to the matching location it will serve the index.html which stored in /usr/share/nginx/html/ directory.

Basic commands

We can check the status of the NGINX server with the following command.

If the NGINX server is not active and running, we can simply start the server with the following command.

Once we have made any changes to configuration files we have to reload or restart our NGINX server to apply those changes. Here if we want to apply those changes without having any downtime we can simply reload the server otherwise we can go for a restart with the following commands.

But before we apply any changes, it is better if we can check or test our configurations are errorless or not. Yes, there is a separate command to test our configuration files.

Cool! Now we know our configuration changes are applied without any errors. Now it’s time to check the output of our server that we just configured. Let's send a curl request to port 80, for that we can use either localhost or the loopback address which is 127.0.0.1.

Server Output

Hooray!!! It says our NGINX server is successfully installed and working 😊

So, yeah! that’s it for this quick introduction, hope this article was helpful to understand the basics of NGINX. So, let's dig deep into some topics like load-balancing, reverse proxying, and other optimization-related topics in the next few articles. You can look into the NGINX official documentation 👈 as well for more information.

Cya!!!

--

--