/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
| Command | Does |
|---|---|
| sudo nginx -t | test the configuration |
| sudo nginx -T | test and print the full configuration |
| sudo systemctl reload nginx | reload without dropping connections |
| sudo nginx -s reload | the same, by signalling the master process |
| sudo tail -f /var/log/nginx/error.log | follow the error log |
| nginx -V | version and compiled-in modules |
Location matching order
| Form | Matches | Priority |
|---|---|---|
| location = /path | exactly /path | 1: wins immediately |
| location ^~ /path/ | prefix, and skips regex checks | 2: longest such prefix |
| location ~ regex / ~* regex | regex, case-sensitive / -insensitive | 3: first match in file order |
| location /path/ | prefix | 4: longest prefix, if no regex matched |
A complete site
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
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
Related pages
commands · nginxnginx -t: test the configuration before reloadingnginx -t checks the configuration syntax and the files it references without touching the running server; nginx -T also prints the full merged config.config · nginxnginx proxy_pass with and without a trailing slashproxy_pass http://app without a path forwards the full URI; with a path like http://app/ nginx replaces the matched location prefix. Real requests shown.codes · nginxnginx status codes 444, 499, 413 and 494-497nginx-specific status codes: 444 closes the connection without a reply, 499 means the client left first, 494 to 497 are header and TLS errors, plus 413.