nginx
Scenario configs, reverse proxy, HTTPS, hardening, caching and troubleshooting
Scenario configs, reverse proxy, HTTPS, hardening, caching and troubleshooting
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/etc/nginx/ layout, nginx -t/-T, zero-downtime reload, and permissions.server_name and location selection, the add_header inheritance trap, request phases, and map instead of if.alias, and protected downloads via X-Accel-Redirect.proxy_pass, WebSocket via map, and unbuffered SSE streaming.certbot --webroot, TLS 1.3, HSTS, ssl_reject_handshake, HTTP/2, and HTTP/3 (quic).nginx/www-data Unix socket permissions, locking down /uploads/, php.ini limits, and fastcgi_cache.upstream pools, algorithms, keepalive, max_fails/backup, safe retries, and a 503 maintenance page.nginx-unprivileged, /healthz, /etc/nginx/templates/, and dynamic DNS via resolver 127.0.0.11.server_tokens off, a default_server catch-all, Slowloris timeouts, limit_except, and a security headers snippet with CSP.limit_req/limit_conn, real client IP behind Cloudflare, locking admin panels (allow, auth_basic, auth_request, mTLS), and CrowdSec.worker_rlimit_nofile, sendfile/open_file_cache, gzip/gzip_static, Brotli, and proxy buffering.proxy_cache, microcaching, proxy_cache_lock, serving STALE when the backend is down, and cache bypass.access.log, JSON logs with upstream timings, logrotate, stub_status, and tracing 499/502/504 failures.ssl_preread).map, behavioral bans with CrowdSec, and when ModSecurity or Coraza makes sense.301/308 codes, bulk redirects via map, return vs rewrite, and proxying under /app/ with proxy_redirect.map, OPTIONS returning 204, cookies with Credentials, Vary: Origin, and preventing duplicate headers.Host for SigV4 signatures in MinIO, long timeouts, and WebDAV for Immich/Nextcloud.grpc_pass and grpcs://, http2 on;, per-service routing, long-lived streams, and mapping 502/504 to grpc-status./etc/nginx/ tree: thin nginx.conf, 00-–08- load order, snippet compatibility, and two sites on top.server_name and location priorities, built-in variables, error codes, and diagnostic one-liners.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.