Optimizing Nginx Performance for High-Traffic Websites
Performance optimization becomes critical as your website traffic grows. Here’s how to tune Nginx for optimal performance under heavy loads.
Performance Bottlenecks
Before making changes, identify what’s constraining your performance:
- CPU usage
- Memory utilization
- Network bandwidth
- Disk I/O
- Connection limits
Worker Processes and Connections
Optimize worker settings for your hardware:
nginx# For multi-core systems
worker_processes auto; # Or set to number of CPU cores
worker_rlimit_nofile 65535; # Increase open file limit
events {
worker_connections 4096; # Increase based on available memory
multi_accept on;
use epoll; # For Linux systems
}
Enable Gzip Compression
Reduce bandwidth usage with appropriate compression:
nginxhttp {
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
application/javascript
application/json
application/xml
text/css
text/plain
text/xml;
}
Static File Caching
Implement efficient caching for static files:
nginxhttp {
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# Set cache expiration
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
Client-Side Caching Headers
Set appropriate cache headers:
nginxlocation ~* \.(jpg|jpeg|png|gif)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000, immutable";
}
location ~* \.(css|js)$ {
expires 7d;
add_header Cache-Control "public, max-age=604800";
}
HTTP/2 Support
Enable HTTP/2 for faster concurrent connections:
nginxserver {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
# SSL optimization
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
}
FastCGI Caching for Dynamic Content
If using PHP or other FastCGI applications:
nginxhttp {
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
# Skip cache for certain conditions
fastcgi_cache_bypass $cookie_nocache $arg_nocache;
}
}
}
Microcaching for Dynamic Sites
Implement microcaching for frequently accessed dynamic pages:
nginxhttp {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:10m max_size=1g inactive=60m use_temp_path=off;
server {
location / {
proxy_pass http://backend;
proxy_cache microcache;
proxy_cache_valid 200 30s; # Short cache time
proxy_cache_lock on;
proxy_cache_use_stale updating;
}
}
}
Monitoring Performance
Set up monitoring to identify bottlenecks:
nginxhttp {
# Enable basic status monitoring
server {
location /nginx_status {
stub_status on;
allow 127.0.0.1; # Only allow local access
deny all;
}
}
}
By implementing these optimizations strategically (not all at once), you can significantly improve your Nginx performance for high-traffic scenarios.