Performance Optimization
Nginx is fast out of the box. These settings push it toward its limits — squeezing out lower latency, higher throughput, and better cache hit rates.
What You Will Learn
- How to enable HTTP/2 and HTTP/3 (QUIC)
- How gzip and Brotli compression work and when to use each
- How Nginx's proxy cache works and how to configure it for PHP and upstream apps
- How to tune worker processes, connections, and file descriptor limits
- How to use
sendfile,tcp_nopush, andtcp_nodelaycorrectly - Browser caching — setting
Cache-ControlandExpiresheaders
Topics in This Module
- Worker and Connection Tuning —
worker_processes,worker_connections,multi_accept, and OS limits - HTTP/2 and HTTP/3 — Enabling ALPN protocols, verifying negotiation, QUIC requirements
- Gzip Compression —
gzip_types, levels, minimum size, andVaryheaders - Brotli Compression — Installing
ngx_brotli, config directives, vs. gzip tradeoffs - Proxy Cache —
proxy_cache_path, cache keys, bypass rules, purge strategies - Browser Caching —
expires,Cache-Control, fingerprinted assets - Sendfile and Buffer Tuning —
sendfile,tcp_nopush,tcp_nodelay, buffer sizes
Quick Performance Wins (in order of impact)
| Optimization | Impact | Directive |
|---|---|---|
| Enable OPcache for PHP-FPM | Very High | php.ini opcache.enable=1 |
| Enable HTTP/2 | High | listen 443 ssl http2 |
| Enable gzip | High | gzip on; gzip_types ... |
| Set browser cache headers | High | expires 1y; for static assets |
| Use UNIX socket for PHP-FPM | Medium | fastcgi_pass unix:/run/php/... |
Enable sendfile | Medium | sendfile on; in http block |
| Enable Brotli | Medium | requires module install |
Tune worker_connections | Medium | match ulimit -n |
Success Checkpoint
By the end of this module you should be able to enable HTTP/2, configure gzip, set browser cache headers, and tune worker settings for your server's CPU and RAM.