Boost Your NGINX Performance: 8 Proven Configuration Tweaks
This guide walks you through eight essential NGINX configuration adjustments—including worker processes, connection limits, GZIP compression, timeout values, buffer sizes, logging, static content caching, and open file caching—to maximize web server performance on Linux systems.
What is NGINX?
NGINX is an open‑source web server that can also act as a reverse proxy, load balancer, cache, and media streaming server. It is widely used for high performance and simple configuration.
Prerequisites
NGINX must be installed on a Linux system. Configuration files are located in /etc/nginx, primarily nginx.conf and site files under /etc/nginx/sites-available. Ensure you have root or sudo access to edit these files.
1. Configure worker processes
NGINX runs one master process and several worker processes. The directive worker_processes defaults to auto, which creates a worker per CPU core. To set it manually, determine the number of cores: grep -c processor /proc/cpuinfo Edit /etc/nginx/nginx.conf and set the value, e.g. for a 4‑core machine:
worker_processes 4;2. Increase worker connections
The worker_connections directive defines the maximum simultaneous connections each worker can handle. The default is 512, which is often insufficient. First check the system’s file descriptor limit: ulimit -n Then set a higher value in the events block:
events {
worker_connections 1024;
}3. Enable GZIP compression
Compressing HTTP responses reduces bandwidth and improves load time on slow connections. Add the following directives inside the http block:
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml;
gzip_disable "MSIE [1-6]\\.";4. Reduce timeout values
Shorter timeouts free resources from idle connections. Typical values:
client_body_timeout 12s;
client_header_timeout 12s;
keepalive_timeout 15s;
send_timeout 10s;5. Tune buffer sizes
Appropriate buffer sizes prevent temporary file writes and reduce I/O. Example settings in the http block:
client_body_buffer_size 10k;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 4k;6. Manage access logging
Logging every request can impact performance. Either disable logging or use buffered logging:
# Disable logging
access_log off;Or enable buffered logging (writes logs in 16 KB chunks):
access_log /var/log/nginx/access.log main buffer=16k;7. Cache static content
Set long expiration times for static assets to reduce bandwidth and latency. Add a location block in the site configuration:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 100d;
access_log off;
}8. Enable open file cache
Caching file descriptors and frequently accessed files speeds up file serving. Include the following in the http block:
open_file_cache max=1024 inactive=10s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;Best practice
Apply one change at a time, reload NGINX with sudo nginx -s reload, and verify performance before proceeding to the next tweak. If a change degrades stability, revert to the default value.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
