basforge

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

    RU
    Topic · Homelab

    nginx

    Scenario configs, reverse proxy, HTTPS, hardening, caching and troubleshooting

    Entries
    33
    Cases
    135
    Updated
    2026-12-01
    Series · where to startOverview

    nginx: how it works, when to pick it, and where to start

    nginx accepts incoming server connections and decides what to do with them: serve a file from disk, forward a request to an app or PHP-FPM, split traffic across several backends, cache a response, or block brute force. One worker per CPU core handles thousands of concurrent clients in a few megabytes of memory, and reloads apply a new config without dropping open connections.

    Read the overview
    1. Install & layout · ManualInstalling nginx, config layout, and safe reloadsOfficial 1.30 package vs Ubuntu package, /etc/nginx/ layout, nginx -t/-T, zero-downtime reload, and permissions.
    2. Config anatomy · ManualHow nginx reads a config: contexts, server, location, and inheritanceContexts, server_name and location selection, the add_header inheritance trap, request phases, and map instead of if.
    3. Static & SPA · ConfigStatic sites, SPAs, and file downloads in nginx: line-by-line configsThree ready configs line by line: static sites (Astro/Hugo), SPAs (React/Vue), alias, and protected downloads via X-Accel-Redirect.
    4. Reverse proxy · ConfigReverse proxy in nginx: Node, Python, Go, WebSocket, and SSE streamingProxying to Node/Python/Go, trailing slashes in proxy_pass, WebSocket via map, and unbuffered SSE streaming.
    5. HTTPS & HTTP/3 · ManualHTTPS, TLS 1.3, HTTP/2, and HTTP/3 in nginxLet's Encrypt certificates via certbot --webroot, TLS 1.3, HSTS, ssl_reject_handshake, HTTP/2, and HTTP/3 (quic).
    6. PHP-FPM · Confignginx and PHP-FPM: WordPress, Laravel, socket permissions, and fastcgi_cacheWordPress and Laravel: nginx/www-data Unix socket permissions, locking down /uploads/, php.ini limits, and fastcgi_cache.
    7. Load balancing · ConfigLoad balancing and failover in nginx: upstream, keepalive, and a 503 maintenance pageupstream pools, algorithms, keepalive, max_fails/backup, safe retries, and a 503 maintenance page.
    8. Docker · Confignginx in Docker and in front of containers: unprivileged, templates, and DNS resolvernginx-unprivileged, /healthz, /etc/nginx/templates/, and dynamic DNS via resolver 127.0.0.11.
    9. Hardening · ConfigBasic nginx hardening: default_server catch-all, limits, Slowloris, and headersserver_tokens off, a default_server catch-all, Slowloris timeouts, limit_except, and a security headers snippet with CSP.
    10. Limits & access · ManualRate limiting and access control in nginx: limit_req, Cloudflare, auth_request, mTLS, and CrowdSeclimit_req/limit_conn, real client IP behind Cloudflare, locking admin panels (allow, auth_basic, auth_request, mTLS), and CrowdSec.
    11. Tuning & compression · ConfigTuning nginx: workers, networking, buffers, Gzip, and BrotliWorkers, worker_rlimit_nofile, sendfile/open_file_cache, gzip/gzip_static, Brotli, and proxy buffering.
    12. Server cache · Confignginx caching: proxy_cache, microcaching, stale responses, and spike protectionproxy_cache, microcaching, proxy_cache_lock, serving STALE when the backend is down, and cache bypass.
    13. Logs & debugging · Manualnginx logs, metrics, and debugging: JSON logs, timings, stub_status, and 499/502/504Reading access.log, JSON logs with upstream timings, logrotate, stub_status, and tracing 499/502/504 failures.
    14. TCP/UDP stream · ConfigTCP, UDP, and SNI passthrough in nginx: the stream module line by lineL4 proxy for databases and UDP, plus routing TLS by SNI on port 443 without decryption (ssl_preread).
    15. Bots & WAF · ManualBot protection and WAF in nginx: User-Agent, CrowdSec, and when to add ModSecurity or CorazaBot lists via map, behavioral bans with CrowdSec, and when ModSecurity or Coraza makes sense.
    16. Redirects & subpaths · ConfigRedirects, rewrite, and subpath proxying in nginx: 301/308, map, and proxy_redirect301/308 codes, bulk redirects via map, return vs rewrite, and proxying under /app/ with proxy_redirect.
    17. CORS & preflight · ConfigCORS and preflight OPTIONS in nginx: origin allowlist, cookies, and API errorsOrigin allowlist via map, OPTIONS returning 204, cookies with Credentials, Vary: Origin, and preventing duplicate headers.
    18. Large uploads & S3 · ConfigLarge uploads, S3/MinIO, and WebDAV in nginx: zero disk buffering, SigV4, and Immich/NextcloudStreaming uploads without disk buffering, exact Host for SigV4 signatures in MinIO, long timeouts, and WebDAV for Immich/Nextcloud.
    19. gRPC proxying · ConfigProxying gRPC in nginx: grpc_pass, HTTP/2, long streams, and error statusesgrpc_pass and grpcs://, http2 on;, per-service routing, long-lived streams, and mapping 502/504 to grpc-status.
    20. Production /etc/nginx/ · ConfigProduction /etc/nginx/ layout: directory tree, load order, and snippet compatibilityFull /etc/nginx/ tree: thin nginx.conf, 00-–08- load order, snippet compatibility, and two sites on top.
    21. Cheatsheet · Cheat sheetnginx cheatsheet: commands, match priorities, variables, and error codesCommands, server_name and location priorities, built-in variables, error codes, and diagnostic one-liners.

    Tips

    11
    add_header in location — wipes all add_header directives from serverA single add_header Cache-Control inside a location silently drops every Strict-Transport-Security, CSP, and X-Frame-Options set at the server level.Trailing slash in proxy_pass — strips the location prefixA single trailing / in proxy_pass http://127.0.0.1:3000/; makes nginx strip the matched location prefix from the request URI.root vs alias — pair the trailing slash or hit off-by-slashroot appends the full URI to the directory, while alias replaces the matched location prefix.if inside location — keep only return or rewrite, move everything else to mapInside a location, if behaves like a hidden nested location rather than a conditional, dropping try_files, headers, and proxy settings.location priority — regex ~ overrides a prefix without ^~A plain prefix location /images/ loses to a regex location ~* \.png$, even when written higher in the file.keepalive in upstream — requires HTTP/1.1 and an empty Connection headerAdding keepalive 32; inside upstream does nothing on its own: by default nginx talks HTTP/1.0 to backends and sends Connection: close.Docker DNS in nginx — caches the old IP without resolver and a variablenginx resolves a container hostname in proxy_pass http://api:8000; once at startup.default_server catch-all — without it, hitting the IP serves another site and its certificateWhen the Host header matches no server_name, nginx routes the request to the first server block on that port — and on port 443 exposes its TLS certificate.HTTP 413 in nginx — default 1m client_max_body_size and PHP limitsBy default nginx rejects any request body larger than 1m with 413 Request Entity Too Large.502 upstream sent too big header — response header buffers for cookies, OIDC, and JWTBy default nginx allocates a single memory page for backend response headers — 4k on x86_64 and ARM64.Applying nginx config changes — always nginx -t && reload, never restartsystemctl restart nginx kills the processes, drops active downloads and WebSocket connections, and leaves the server down if the config has a typo.