Skip to content
ohsudo, Home
Networking

nginx Cheat Sheet

The nginx commands and config you use every day: testing and reloading, server blocks, reverse proxy, HTTPS, SPAs, limits, timeouts, logs and location matching.

Updated November 15, 2026 · Applies to nginx 1.25+ · Ubuntu · Debian · RHEL / Rocky

Commands

Terminal window
sudo nginx -t # test the config (always before reload)
sudo nginx -T # test, and print the whole config
sudo systemctl reload nginx # apply changes, no downtime
sudo systemctl restart nginx # full restart
nginx -v # version
nginx -V # version, build options and modules
sudo tail -f /var/log/nginx/error.log

Where the config lives

Path What
/etc/nginx/nginx.conf Main file, includes the rest
/etc/nginx/conf.d/*.conf Site files (official packages, RHEL)
/etc/nginx/sites-available/ Site files (Debian, Ubuntu)…
/etc/nginx/sites-enabled/ …enabled with a symlink
Terminal window
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/

A reverse proxy

server {
listen 80;
server_name app.example.com;
location / {
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;
}
}

WebSockets also need, in that location:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

HTTPS

server {
listen 443 ssl;
http2 on; # nginx 1.25.1+
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
}
server {
listen 80;
server_name app.example.com;
return 301 https://$host$request_uri;
}
Terminal window
sudo certbot --nginx -d app.example.com -d www.app.example.com
sudo certbot renew --dry-run

Read a certificate’s names and dates with the SSL Certificate Decoder.

Static files and single-page apps

root /var/www/app/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html; # SPA: unknown paths → index.html
}
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}

Password protection

location /admin/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Terminal window
sudo htpasswd -B -c /etc/nginx/.htpasswd anna # -c creates the file: only for the first user

Or make the lines in your browser with the htpasswd Generator.

Limits and timeouts

client_max_body_size 50m; # uploads (default 1m → 413)
proxy_connect_timeout 5s;
proxy_read_timeout 120s; # slow backends (default 60s → 504)
proxy_buffer_size 16k; # big headers from the backend
proxy_buffers 8 16k;
large_client_header_buffers 4 16k;
server_names_hash_bucket_size 128;

Logs with timings

log_format timed '$remote_addr "$request" $status '
'rt=$request_time urt=$upstream_response_time';
access_log /var/log/nginx/access.log timed;
error_log /var/log/nginx/error.log warn;
Terminal window
awk '{print $3}' /var/log/nginx/access.log | sort | uniq -c | sort -rn # count by status

The $3 is the status in the timed format above; in the default format, it’s $9. The Log Analyzer reads both formats and does the counting for you. In the official Docker image, access.log and error.log are links to the container’s output: read them with docker logs, or log to other file names.

Which location wins

Form Matches
location = /health Exactly this path, checked first
location ^~ /static/ This prefix, and stops looking at regexes
location ~ \.php$ Regex, case-sensitive, first match in file order
location ~* \.(jpg|png)$ Regex, case-insensitive
location /api/ Longest prefix, used if no regex matches

Common errors

Generate a full server block with the nginx Config Generator.

Other cheat sheets