Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Nginx in Two Sentences

Nginx is open-source software that serves websites: it accepts connections from browsers on ports 80 and 443 and answers with pages and files. It also works as a reverse proxy, load balancer, and cache in front of other programs. The name is pronounced "engine-x".

What Nginx Is Used For

  • Web server: serves HTML, images, CSS, and JavaScript straight from disk, quickly and with little memory.
  • Reverse proxy: receives requests and passes them to an application on the same server or another one, such as a Node.js app on port 3000. See Nginx reverse proxy.
  • Load balancer: spreads requests over several application servers and skips one that stops answering. The idea is explained in what is a load balancer.
  • TLS termination: handles HTTPS certificates so the application behind it does not have to.
  • Cache: keeps copies of responses from the application and serves them again without asking it.

How It Works

Nginx starts one master process, which reads the configuration and opens the ports, and a few worker processes, usually one per CPU core. Each worker handles thousands of connections at once. Instead of giving every visitor a thread that waits, a worker reacts to events: data has arrived on this connection, that one is ready to receive. A slow or idle visitor costs almost nothing while it waits, so memory use stays low under heavy traffic.

You can see this on a server where Nginx runs:

ps -C nginx -o pid,user,args

The master runs as root; the workers run as www-data on Debian and Ubuntu and as nginx on AlmaLinux and Rocky Linux.

Static Files and Dynamic Pages

Nginx itself only serves files. It does not run PHP, Python, or JavaScript. For a dynamic page it hands the request to another program: PHP scripts go to PHP-FPM over the FastCGI protocol, and applications with their own HTTP server receive the request through proxy_pass:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;

    location / {
        try_files $uri $uri/ =404;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:3000;
    }
}

Here files under /var/www/example.com are served directly, and every address that starts with /api/ goes to the application on port 3000.

Nginx vs Apache

Apache traditionally handles each connection with its own process or thread and lets every folder change settings through .htaccess files; Nginx uses a few event-driven workers and keeps all settings in central files. Both serve sites well, and some servers run Nginx in front of Apache; the details are in Apache vs Nginx.

Where the Configuration Lives

  • Main file: /etc/nginx/nginx.conf on every distribution.
  • Debian and Ubuntu: one file per site in sites-available, enabled with a link in sites-enabled.
  • AlmaLinux and Rocky Linux: files ending in .conf in /etc/nginx/conf.d/.

Test every change before you apply it, then reload:

sudo nginx -t
sudo systemctl reload nginx

If the test reports an error, do not reload: the running server keeps the old configuration until you fix the file. Locations are listed in Nginx config file location.

Check It on Your Server

sudo nginx -v
systemctl status nginx
curl -I http://localhost

The first line prints the version, the second shows whether the service runs, and the response headers from curl include a Server line that names nginx. When something fails, read the logs as shown in Nginx logs.

First Steps

  1. Install it: how to install Nginx.
  2. Add a site with Nginx server blocks.
  3. Open port 80 and port 443 in the firewall.
  4. Add a certificate with Certbot for Nginx.
Was this answer helpful?
Loading...