Master Nginx Performance: 20+ Proven Optimization Techniques
This comprehensive guide walks you through Nginx performance tuning, covering worker processes, CPU affinity, file descriptor limits, event models, gzip compression, fastcgi caching, expires headers, anti‑hotlinking, kernel tweaks, and system limits to dramatically improve web server efficiency.
Nginx Process and Worker Settings
Set the Nginx worker process to run as a non‑root user and match the number of CPU cores. Use worker_processes auto; and worker_cpu_affinity auto; to bind workers to specific CPUs, reducing context switches.
# Example worker configuration
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;Event Model and Connection Limits
Enable the epoll event model and configure the maximum connections per worker. Adjust worker_connections based on system file descriptor limits.
events {
use epoll;
worker_connections 10240;
accept_mutex on;
}HTTP Core Optimizations
Enable efficient file transmission with sendfile on; and tcp_nopush on;. Configure keep‑alive timeouts, client body size limits, and disable server version disclosure.
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 200m;
server_tokens off;
}Gzip Compression
Compress text responses to save bandwidth. Set minimum length, compression level, and MIME types.
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_comp_level 6;
gzip_types text/plain text/css application/javascript application/xml;FastCGI Tuning and Caching
Configure FastCGI timeouts, buffer sizes, and enable persistent connections with fastcgi_keep_conn on;. Set up FastCGI cache paths and validity periods.
fastcgi_connect_timeout 240;
fastcgi_send_timeout 240;
fastcgi_read_timeout 240;
fastcgi_buffer_size 64k;
fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;Expires Headers and Caching Static Assets
Set long expiration times for static resources to reduce repeat downloads.
location ~* \.(ico|jpe?g|png|gif|css|js)$ {
expires 30d;
access_log off;
log_not_found off;
}Anti‑Hotlink Configuration
Prevent other sites from directly linking to your media files using valid_referers and returning a 403 status for invalid requests.
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked *.example.com;
if ($invalid_referer) { return 403; }
}Kernel and System Parameter Optimizations
Adjust Linux kernel settings to improve network performance and increase the maximum number of open files.
# /etc/sysctl.conf examples
net.ipv4.tcp_syncookies = 1
net.core.somaxconn = 1024
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 10Update /etc/security/limits.conf to raise nofile limits for all users and the Nginx user.
* soft nofile 65535
* hard nofile 65535Common Pitfalls and Troubleshooting
Address issues such as character set problems, directory listings, and proper use of location modifiers. Use autoindex off; to hide directory indexes and set charset utf-8; where needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
