Introduction
Nginx performance tuning can dramatically improve response times and throughput. Proper configuration handles thousands of concurrent connections efficiently.
Worker Configuration
# /etc/nginx/nginx.conf
worker_processes auto; # Match CPU cores
worker_rlimit_nofile 65535;
events {
worker_connections 4096;
multi_accept on;
use epoll;
}
Gzip Compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json
application/javascript text/xml
application/xml image/svg+xml;
Caching
# Proxy caching
proxy_cache_path /var/cache/nginx levels=1:2
keys_zone=my_cache:10m max_size=1g;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 1h;
proxy_cache_valid 404 1m;
proxy_pass http://backend;
}
}
Static File Optimization
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
Summary
Tune worker processes, enable gzip compression, configure caching, and optimize static file serving. Monitor with nginx -V and adjust based on your traffic patterns.
YouTip