Boost NGINX Performance: Essential Linux and NGINX Tuning Tips
This guide explains how to fine‑tune Linux kernel parameters and NGINX directives—such as backlog queues, file descriptors, worker processes, keep‑alive settings, access‑log buffering, sendfile, and request limits—to achieve optimal web server performance for high‑traffic sites.
Introduction
NGINX powers about 40% of the world’s busiest websites. While default Linux and NGINX settings work well for many scenarios, targeted tuning can unlock higher performance.
Linux Configuration
Backlog Queue
net.core.somaxconn – controls the size of the queue for connections waiting to be accepted by NGINX. Increase it for high‑traffic sites, matching the value with the
listen backlogdirective if set above 512.
net.core.netdev_max_backlog – sets the rate at which the NIC buffers packets before the CPU processes them. Raise it on high‑bandwidth machines.
File Descriptors
sys.fs.file_max – system‑wide limit of file descriptors.
nofile – per‑user limit, configured in
/etc/security/limits.conf.
Temporary Ports
net.ipv4.ip_local_port_range – defines the range of ephemeral ports; expand if ports are exhausted (commonly 1024‑65000).
net.ipv4.tcp_fin_timeout – time before a closed port can be reused; can be safely reduced from the default 60 seconds to 30 or 15 seconds.
NGINX Configuration
Worker Processes
worker_processes– number of worker processes; usually one per CPU core, set to
autofor automatic matching.
worker_connections– maximum connections per worker; default 512, raise according to hardware and traffic.
Keepalives
keepalive_requests– number of requests a client can send over a single persistent connection (default 100).
keepalive_timeout– idle time before a persistent connection is closed.
keepalive– number of idle persistent connections from a worker to an upstream server.
To enable upstream keep‑alive, set
proxy_http_version 1.1and
proxy_set_header Connection "".
Access Log Buffering
Enable buffering with the
access_logdirective’s
buffer=sizeoption and optionally
flush=timeto reduce CPU/IO overhead.
Sendfile
When enabled,
sendfiletransfers data directly between file descriptors in the kernel, achieving zero‑copy transmission and lower CPU usage. It is disabled by default.
Limits
limit_conn/
limit_conn_zone– restrict connections per client IP.
limit_rate– cap bandwidth per connection.
limit_req/
limit_req_zone– limit request processing rate, useful for protecting login pages.
max_conns– maximum concurrent connections to a single upstream server.
queue– defines request queuing behavior when
max_connsis reached.
Other Considerations
Cache
Enabling NGINX cache for load‑balanced backends can dramatically reduce response time and backend load.
Compression
Response compression saves bandwidth but consumes CPU; enable it when bandwidth savings outweigh the CPU cost, and avoid compressing already compressed assets such as JPEG images.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.