Sub topic 1
What Nginx is and how it works
Prerequisites
This chapter assumes you know basic Linux command line use, can run commands with sudo, understand how HTTP requests and responses work, and know what DNS names and TCP ports are.
What Nginx is and how it works
Nginx (pronounced "engine-x") is a web server. A web server is a program that listens on a network port, usually 80 for HTTP or 443 for HTTPS, and answers requests. Nginx can serve static files, forward requests to an application, encrypt traffic with TLS, and spread load across several backends.
Nginx uses an event-driven design. A few worker processes each handle thousands of connections at once, which keeps memory use low under heavy load.
Installing Nginx on Linux
On Debian and Ubuntu, install Nginx from the distribution repositories:
sudo apt update
sudo apt install nginxManaging the service
Nginx runs as a systemd service. Systemd is the service manager on most modern Linux systems. The following commands start Nginx, enable it at boot, and check that it answers locally:
sudo systemctl enable --now nginx
systemctl status nginx
curl -I http://localhostThe --now flag starts the service immediately, and enable makes it start at boot. A default install returns HTTP/1.1 200 OK.
How the configuration is organized
The main file is /etc/nginx/nginx.conf. It contains directives, which are name and value pairs ending in a semicolon, and contexts, which are blocks such as http, server, and location that group directives. Contexts nest: a server block sits inside http, and a location block sits inside server.
On Debian and Ubuntu, site definitions live in /etc/nginx/sites-available/. You enable a site by linking it into /etc/nginx/sites-enabled/.
Testing and reloading safely
A single typo can stop Nginx from starting, so test every change before applying it. The nginx -t command checks the syntax and reports the file and line of any error.
sudo nginx -t && sudo systemctl reload nginxThe && operator runs the reload only if the test passes.
Exercise
Exercise
Why does the command use && between nginx -t and systemctl reload?
Show answer
The reload runs only if the configuration test succeeds. If the test fails, the chain stops and the running configuration stays unchanged.