basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Cheat sheetHomelabnginx

    nginx cheatsheet: commands, match priorities, variables, and error codes

    Service control and validation commands, server_name and location selection order, everyday built-in variables, 4xx/5xx error codes (including 444 and 499), and diagnostic one-liners on one screen.

    Service control and config validation

    Command What it does
    sudo nginx -t test syntax and all include files without reloading
    sudo nginx -t && sudo systemctl reload nginx apply changes with a zero-downtime reload
    sudo nginx -T dump the full assembled config across all files
    sudo nginx -T 2>&1 | grep '^# configuration file' list every config file loaded by nginx
    nginx -v / nginx -V short version / OpenSSL version and compiled modules
    sudo nginx -s reopen reopen log files after rotation (USR1 signal)
    sudo nginx -s quit stop workers gracefully after finishing active requests
    pgrep -a nginx list master and worker processes with PIDs
    sudo ss -ltnup 'sport = :80 or sport = :443' check what listens on TCP 80/443 and UDP 443 (QUIC)

    location match priority (highest to lowest)

    Order Modifier Example How it matches
    1 = location = /healthz exact URI match; search stops immediately
    2 ^~ location ^~ /assets/ longest prefix; if it wins, regex blocks are skipped
    3 ~ / ~* location ~ \.php$ regular expressions top-to-bottom in file order (case-sensitive / insensitive)
    4 (none) location /api/ longest plain prefix (used if no regex matched)
    — @ location @maintenance named block for internal redirects only (error_page, try_files)

    server_name match priority

    Order Type Example
    1 Exact name app.example.org
    2 Leading wildcard (longest match) *.example.org
    3 Trailing wildcard (longest match) example.*
    4 Regular expression (first in config) ~^api-\d+\.example\.org$
    5 Default server listen 80 default_server;

    Everyday built-in variables

    Variable Value
    $uri normalized path without ?query (changes after rewrite / try_files)
    $request_uri original full path including ?query (use in return 301)
    $args / $query_string query string after ? (aliases)
    $host requested hostname (lowercase, no port; falls back to server_name if Host is empty)
    $scheme request scheme: http or https
    $remote_addr client IP (or restored real IP after ngx_http_realip_module)
    $proxy_add_x_forwarded_for incoming X-Forwarded-For with $remote_addr appended
    $request_time total request time from first client byte to last sent byte, in seconds
    $upstream_response_time time spent waiting on the backend response, in seconds
    $upstream_addr backend address(es) contacted by nginx
    $upstream_cache_status cache outcome: MISS, HIT, BYPASS, EXPIRED, STALE, UPDATING
    $realpath_root real filesystem path of root with symlinks resolved

    Error codes and where to look first

    Code Meaning First cause to check
    400 Bad Request malformed request, oversized cookie (large_client_header_buffers), or plain HTTP sent to an HTTPS port
    403 Forbidden missing index.html, missing +x for nginx on a parent directory (namei -l), deny all, or limit_except
    404 Not Found wrong root/alias, missing try_files ... /index.html in an SPA, or direct hit to an internal block
    413 Request Entity Too Large upload exceeds client_max_body_size (default 1m)
    429 Too Many Requests limit_req or limit_conn tripped (check realip behind Cloudflare)
    444 (nginx code) TCP connection dropped with no response — default_server catch-all or filter matched
    499 (nginx code) client closed connection before backend replied — inspect $upstream_response_time
    502 Bad Gateway backend stopped, wrong port, changed Docker container IP, or PHP-FPM socket permissions
    503 Service Unavailable maintenance mode, limit_req without limit_req_status 429, or all upstreams down
    504 Gateway Timeout backend did not reply within proxy_read_timeout (default 60s)

    Quick terminal diagnostics

    Command What it checks
    curl -sI -H "Host: example.org" http://127.0.0.1/ headers and status of a specific server block without DNS
    curl -svI --http2 https://example.org/ 2>&1 | grep -iE 'SSL|ALPN|HTTP/|alt-svc' TLS 1.3, HTTP/2, and the Alt-Svc header for HTTP/3
    openssl s_client -connect 203.0.113.10:443 -servername unknown.test </dev/null whether ssl_reject_handshake hides certificates
    sudo tail -f /var/log/nginx/error.log live errors for 502, 504, 403, socket, and TLS failures
    sudo -u nginx namei -l /var/www/example.org/index.html nginx worker permissions along every directory in the path
    sudo jq -r 'select(.up_time != "") | "\(.up_time)\t\(.status)\t\(.uri)"' /var/log/nginx/access.log | sort -rn | head slowest backend requests from a JSON access log