Skip to content

/cheatsheets/nginx/nginx-cheat-sheet

nginx cheat sheet: commands, server blocks, locations and proxying

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · nginx version: nginx/1.30.6updated 2026-10-20
tool
nginx
versions
nginx 1.25+
os
any

The nginx commands and configuration snippets used most. Paths are the Debian/Ubuntu defaults; other distributions use /etc/nginx/conf.d/ instead of sites-enabled/.

Commands

CommandDoes
sudo nginx -ttest the configuration
sudo nginx -Ttest and print the full configuration
sudo systemctl reload nginxreload without dropping connections
sudo nginx -s reloadthe same, by signalling the master process
sudo tail -f /var/log/nginx/error.logfollow the error log
nginx -Vversion and compiled-in modules

Location matching order

FormMatchesPriority
location = /pathexactly /path1: wins immediately
location ^~ /path/prefix, and skips regex checks2: longest such prefix
location ~ regex / ~* regexregex, case-sensitive / -insensitive3: first match in file order
location /path/prefix4: longest prefix, if no regex matched

A complete site

nginx
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    http2 on;
    server_name example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    root /var/www/example;
    client_max_body_size 20m;

    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location /api/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }
}

Check a configuration like this one

bash
mkdir -p logs temp
cat > nginx.conf <<'EOF'
pid logs/nginx.pid;
events {}
http {
  server {
    listen 127.0.0.1:8080;
    root html;
    client_max_body_size 20m;
    location = /health { return 200 "ok\n"; }
    location ^~ /assets/ { expires 1y; add_header Cache-Control "public, immutable"; }
    location /api/ {
      proxy_pass http://127.0.0.1:3000;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location / { try_files $uri $uri/ /index.html; }
  }
}
EOF
nginx -p . -c nginx.conf -e stderr -t 2>&1 | sed -E 's/^[0-9/]+ [0-9:]+ \[([a-z]+)\] [0-9#]+: /[\1] /'

Output

nginx: the configuration file ./nginx.conf syntax is ok
nginx: configuration file ./nginx.conf test is successful

Sources