Nginx Performance Optimization Techniques
To keep Nginx fast under heavy load, adjust worker processes and connections to match CPU cores, enable gzip compression, set browser and proxy caching headers, turn on sendfile with optimal chunk and TCP settings, tune log levels, and optionally use a CDN and HTTP/2.
Nginx is a high‑performance web server widely used on the Internet. Under high concurrency and traffic, tuning is essential to maintain efficiency.
This article introduces several optimization methods.
1. Adjust worker processes and connections
The number of worker processes is set by the worker_processes directive. Example:
worker_processes 4;The maximum connections per worker are controlled by worker_connections :
worker_connections 1024;Choose values that match the server’s CPU cores and expected load.
2. Enable Gzip compression
Install the gzip module when compiling Nginx, then add the following to the http block:
http {
...
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
...
}3. Configure caching strategies
Set browser cache headers using Expires and Cache‑Control :
location ~* \.(jpg|jpeg|gif|png|css|js)$ {
add_header Cache-Control "public, max-age=31536000";
}For reverse‑proxy caching, use proxy_cache_valid :
location / {
proxy_pass http://backend;
proxy_cache mycache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}4. Optimize file delivery
Enable sendfile to transfer files directly from disk to network:
http {
...
sendfile on;
...
}Adjust sendfile_max_chunk and tcp_nopush for better throughput:
http {
...
sendfile_max_chunk 1m;
tcp_nopush on;
...
}5. Tune logging
Set an appropriate log_level , e.g., info :
http {
...
log_level info;
...
}Configure log rotation and compression to reduce disk usage.
6. Additional recommendations
Use a CDN to cache static assets and enable HTTP/2 for multiplexed, efficient transfers:
server {
listen 443 ssl http2;
...
}Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.