How to Configure Nginx for Maximum Scalability and Speed
1. Tune Worker Processes and Connections
Scalability starts with hardware utilization. Set worker_processes auto to match CPU cores. In /etc/nginx/nginx.conf, adjust worker_connections (e.g., 1024). This enables handling concurrent connections efficiently for high-traffic loads.
- worker_processes auto
- worker_connections 1024
- multi_accept on
Enable multi_accept to accept all new connections at once. These core optimizations reduce latency spikes.
2. Enable Gzip Compression
Compression reduces bandwidth usage and page load times. Add to http block:
- gzip on
- gzip_vary on
- gzip_proxied any
- gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml
- gzip_min_length 256
This compresses text-based files, boosting speed for mobile users.
3. Fine-Tune Buffers and Timeouts
Adjust buffer sizes to handle request spikes. Set:
- client_body_buffer_size 128k
- client_max_body_size 10m
- client_header_buffer_size 1k
- large_client_header_buffers 4 8k
Reduce timeouts to free resources:
- keepalive_timeout 15
- send_timeout 10
- client_body_timeout 10
- client_header_timeout 10
4. Implement Static File Caching
Cache static assets to offload backend. In server block:
location ~* .(jpg|jpeg|png|gif|ico|css|js|woff2|webp)$ {
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
Add open_file_cache for file descriptor caching:
- open_file_cache max=2000 inactive=20s
- open_file_cache_valid 30s
- open_file_cache_min_uses 2
- open_file_cache_errors on
5. Use FastCGI Cache for Dynamic Content
PHP sites benefit from FastCGI caching. Configure:
fastcgi_cache_path /tmp/nginx_fastcgi_cache levels=1:2 keys_zone=PHP_CACHE:100m inactive=60m;
fastcgi_cache PHP_CACHE;
fastcgi_cache_valid 200 60m;
add_header X-FastCGI-Cache $upstream_cache_status;
This reduces PHP processing for repeat requests, improving scalability.
6. Optimize TCP and SSL Settings
For HTTPS performance, enable HTTP/2 and optimize SSL:
- listen 443 ssl http2
- ssl_protocols TLSv1.2 TLSv1.3
- ssl_prefer_server_ciphers on
- ssl_session_cache shared:SSL:10m
- ssl_session_timeout 10m
Set TCP net.core settings in OS (/etc/sysctl.conf) to match:
- net.core.somaxconn = 4096
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_fin_timeout = 15
7. Enable Sendfile and TCP_NOPUSH
Boost file serving speed with:
- sendfile on
- tcp_nopush on
- tcp_nodelay on
sendfile bypasses user-space copying. tcp_nopush optimizes packet headers for faster transmission.
8. Limit Rate Connections
Prevent resource exhaustion:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 10;
And burst handling:
limit_req_zone $binary_remote_addr zone=one:10m rate=30r/s;
limit_req zone=one burst=5 nodelay;
These controls maintain stability under traffic peaks.
9. Monitor and Test Configuration
After applying changes, always validate:
- Run nginx -t to check syntax
- Use ab (ApacheBench) or wrk for load testing
- Inspect headers with curl -I for cache and compression
Regular monitoring ensures continued speed optimization.