Nginx workers, upstreams, TLS, cache và overload control
Nginx event-driven workers multiplex connections; capacity vẫn bị giới hạn bởi file descriptors, worker connections, buffers, CPU, TLS, disk/cache và upstream behavior.
Nginx
Master/worker lifecycle
Master đọc config, bind sockets, quản workers và signals; workers xử lý event loops. worker_processes auto, affinity và worker_connections phải xét upstream sockets/FD limit, không nhân đơn giản thành client capacity. Reload tạo workers mới và drain workers cũ; cấu hình lỗi không thay generation đang chạy.
Request phases và proxy
Server/location selection, rewrite/access/content/filter/log phases; location matching và URI normalization dễ gây route/security bugs. Proxy request headers/Host/scheme/client IP phải explicit; buffering tách slow client khỏi upstream nhưng dùng memory/temp disk và ảnh hưởng streaming.
Upstream load balancing
Round-robin mặc định, least_conn cho varying durations, hash/ip_hash cho affinity với trade-offs. Open source passive failure detection dùng max_fails/fail_timeout; keepalive pool per worker cần proxy_http_version/header config đúng. DNS resolution/update semantics và upstream state khác theo static names/resolver/commercial features.
Timeouts và retries
proxy_connect/send/read timeouts đo phases/idle giữa operations, không luôn whole request deadline. proxy_next_upstream có conditions/tries/timeout; retry non-idempotent request có duplicate side effect nếu request đã gửi. Nginx 499 là client closed; 502 bad upstream response/connect, 504 timeout—cần upstream/app evidence.
TLS
Certificate/SNI chain, protocols/ciphers/session cache/tickets, OCSP stapling và key rotation. TLS handshake CPU/latency; HTTP/2 multiplexing thay connection behavior. HSTS chỉ sau khi HTTPS đúng; private key permissions và zero-downtime reload cần drill.
Caching
Cache key phải chứa representation dimensions phù hợp; respect/bypass/ignore headers theo policy. stale-while-error/updating giảm outage nhưng có freshness risk; cache lock giảm stampede. Không cache personalized/auth response nếu key/privacy không đúng. Purge/invalidation/versioned URLs cần ownership.
Rate/connection limiting
limit_req leaky-bucket theo key với rate/burst/delay/nodelay; limit_conn concurrent connections. Key tin cậy sau proxy processing; shared-memory zone sizing/cardinality và status/logging cần theo dõi. Rate limiting không thay auth/business quota và distributed global enforcement.
Buffers, files và static content
Client body/header/proxy buffers, temp files, sendfile/aio/directio và open_file_cache có workload trade-offs. Oversized buffers nhân theo concurrent requests; small headers/body limits có 4xx. Static assets dùng content hash, cache-control và correct MIME/security headers.
Security
Patch/version hygiene, least worker user/files, hide unnecessary surfaces, request limits, TLS policy, trusted real_ip sources, sanitize proxy headers, deny dotfiles/admin and avoid unsafe alias/root/rewrite patterns. WAF/rate limit là layers, backend vẫn authz/validate.
Observability và tuning
Access log structured với request/upstream addresses, status, request_time và upstream connect/header/response times, request ID; không log secrets. Stub/status metrics, worker connections, accepts/handled, TLS/cache/rate-limit and OS FD/socket counters. Benchmark production-like keepalive/TLS/body/upstream latency; tune one layer and verify SLO.