Introduction to Nginx
Learn what nginx is in this nginx tutorial, including nginx as a load balancer, reverse proxy, and web server. Also learn about reloading nginx vs restarting nginx and how to start and stop nginx.
Table of Contents š
What is Nginx?
Nginx is a software that can act as a web server, reverse proxy, load balancer, and more. It can handle thousands of concurrent connections with its event-driven and asynchronous architecture. As a result, nginx is usually placed between clients and application servers to increase performance by managing traffic and distributing it among application servers. Architecturally, nginx has one master process and several worker processes. The master process reads and evaluates configuration, maintains worker processes, etc. The worker processes process the requests.
Here, nginx is acting as a load balancer. Distributing the requests from clients among application servers to maximize performance. An intranet can be defined here as a computer network that works together as a unit.
Install Nginx
For this series, we will be running nginx on Ubuntu. Most Linux distributions have nginx in their package repositories and can be installed through usual install methods such as apt on Debian, etc. To install nginx on an operating system different from Ubuntu, use the following link.
https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
To install nginx on a Debian-based Linux distribution use sudo apt install nginx.
sudo apt install nginx
This will install Nginx and any required dependencies. To ensure Nginx was installed successfully, type in the command nginx -v.
nginx -v
The output should be similar to the following.
nginx version: nginx/1.18.0 (Ubuntu)
By default, nginx will be installed in /etc in a folder called nginx. Etc stands for etcetera and contains all system configuration files. Change directories to the nginx folder by using cd /etc/nginx.
cd /etc/nginx
Start and Stop Nginx
When working with nginx, it won't be uncommon to have times where nginx needs to be stopped, started, reloaded, or restarted. To start nginx, we use the command sudo systemctl start nginx.
sudo systemctl start nginx
This command starts the nginx service. We can stop nginx using the command sudo systemctl stop nginx.
sudo systemctl stop nginx
A common reason to stop nginx is to change its configuration (which we will do in a later article), but it should be noted that stopping nginx will shut down all nginx processes even if there are open connections with clients.
Restart vs Reload Nginx
If we want to load a new configuration into nginx without any downtime, we can reload nginx using the command sudo systemctl reload nginx.
sudo systemctl reload nginx
Reloading nginx allows open connections to be handled by lingering worker processes (before they are discarded) while the configuration is updated. Specifically, when nginx receives a signal to reload its configuration, it first checks the validity of the configuration file and attempts to apply it. If it is applied successfully, then the master process starts new worker processes and shuts down the old ones. If there are issues with the configuration file, the master process continues to work with the old configuration. We can also restart Nginx with sudo systemctl restart nginx.
sudo systemctl restart nginx
However, reloading nginx is safer than restarting. This is because if there are any problems with a new configuration when restarting nginx, nginx will stop and won't start until the error is fixed.
Checking Status of Nginx
As nginx runs in the background as a service we don't see any statuses on it. However, we can display the status of nginx by using the command sudo systemctl status nginx.
sudo systemctl status nginx
Output should be similar to the following.
ā nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2022-06-22 22:12:48 CEST; 5h 40min ago Docs: man:nginx(8) Process: 15978 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 15979 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 15980 (nginx) Tasks: 13 (limit: 18997) Memory: 10.3M CGroup: /system.slice/nginx.service āā15980 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; āā15981 nginx: worker process āā15982 nginx: worker process āā15983 nginx: worker process āā15984 nginx: worker process āā15985 nginx: worker process āā15986 nginx: worker process āā15987 nginx: worker process āā15988 nginx: worker process āā15989 nginx: worker process āā15990 nginx: worker process āā15991 nginx: worker process āā15992 nginx: worker process
Jun 22 22:12:48 wittcode-Blade systemd[1]: Starting A high performance web server and a reverse proxy server... Jun 22 22:12:48 wittcode-Blade systemd[1]: Started A high performance web server and a reverse proxy server.
The output shows if nginx is active (running) or inactive. If nginx had issues loading, such as from a bad configuration, the status will be listed as failed.