Boost Nginx Performance: Essential Tuning Tips for High Traffic
Learn how to fine‑tune Nginx by adjusting key global, events, and HTTP settings—such as worker processes, connection limits, gzip compression, and file caching—to maximize throughput and reduce latency under heavy client loads, with a complete example configuration and restart instructions.
Most Nginx installation guides stop at basic steps—install via apt‑get, tweak a few lines, and you have a working web server. While a default install often suffices, extracting maximum performance requires deeper configuration. This guide outlines the most impactful settings you can adjust to improve Nginx performance under heavy client loads.
Basic (Optimized) Configuration
The only file we modify is nginx.conf , located in /etc/nginx . We first discuss global settings, then walk through module‑specific directives that help handle many concurrent clients efficiently. A complete configuration file is provided at the end.
High‑Level Settings
Some high‑level directives appear before module sections in nginx.conf :
user www-data;</code>
<code>pid /var/run/nginx.pid;</code>
<code>worker_processes auto;</code>
<code>worker_rlimit_nofile 100000;user and pid are left at defaults. worker_processes defines the number of worker processes; setting it to the number of CPU cores (or auto) is a good start. worker_rlimit_nofile raises the maximum number of open files per worker, preventing “too many open files” errors.
Events Module
The events block controls connection handling:
events {</code>
<code> worker_connections 2048;</code>
<code> multi_accept on;</code>
<code> use epoll;</code>
<code>}worker_connections sets the maximum simultaneous connections per worker. Combined with worker_rlimit_nofile , this can be set high, but it is ultimately limited by the OS’s available sockets (~64K). multi_accept tells Nginx to accept as many new connections as possible after a notification. use selects the event‑polling method; on Linux 2.6+ use epoll, on BSD use kqueue.
HTTP Module
The HTTP block contains core web‑server features. Only a subset is shown here:
http {</code>
<code> server_tokens off;</code>
<code> sendfile on;</code>
<code> tcp_nopush on;</code>
<code> tcp_nodelay on;</code>
<code>}server_tokens hides the Nginx version in error pages for security. sendfile enables zero‑copy file transfer, reducing CPU usage. tcp_nopush sends HTTP headers in one packet, while tcp_nodelay disables Nagle’s algorithm to send small packets promptly.
access_log off;</code>
<code>error_log /var/log/nginx/error.log crit;Disabling access_log reduces disk I/O, and setting error_log to crit logs only serious errors.
keepalive_timeout 10;</code>
<code>client_header_timeout 10;</code>
<code>client_body_timeout 10;</code>
<code>reset_timedout_connection on;</code>
<code>send_timeout 10;These timeout settings shorten idle connections and free resources faster.
limit_conn_zone $binary_remote_addr zone=addr:5m;</code>
<code>limit_conn addr 100;limit_conn_zone allocates shared memory for connection counters; limit_conn caps the number of simultaneous connections per IP address (here 100).
include /etc/nginx/mime.types;</code>
<code>default_type text/html;</code>
<code>charset UTF-8;These directives load MIME types, set the default content type, and define the default character set.
gzip on;</code>
<code>gzip_disable "msie6";</code>
<code>gzip_proxied any;</code>
<code>gzip_min_length 1000;</code>
<code>gzip_comp_level 4;</code>
<code>gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;Enabling gzip compresses responses, reducing bandwidth. The settings control which clients are compressed, the minimum size, compression level, and MIME types to compress.
# cache information about file descriptors, frequently accessed files</code>
<code>open_file_cache max=100000 inactive=20s;</code>
<code>open_file_cache_valid 30s;</code>
<code>open_file_cache_min_uses 2;</code>
<code>open_file_cache_errors on;File caching speeds up access to frequently used files.
# Virtual Host Configs</code>
<code>include /etc/nginx/conf.d/*.conf;</code>
<code>include /etc/nginx/sites-enabled/*;Complete Configuration Example
user www-data;</code>
<code>pid /var/run/nginx.pid;</code>
<code>worker_processes auto;</code>
<code>worker_rlimit_nofile 100000;</code>
<code>events {</code>
<code> worker_connections 2048;</code>
<code> multi_accept on;</code>
<code> use epoll;</code>
<code>}</code>
<code>http {</code>
<code> server_tokens off;</code>
<code> sendfile on;</code>
<code> tcp_nopush on;</code>
<code> tcp_nodelay on;</code>
<code> access_log off;</code>
<code> error_log /var/log/nginx/error.log crit;</code>
<code> keepalive_timeout 10;</code>
<code> client_header_timeout 10;</code>
<code> client_body_timeout 10;</code>
<code> reset_timedout_connection on;</code>
<code> send_timeout 10;</code>
<code> limit_conn_zone $binary_remote_addr zone=addr:5m;</code>
<code> limit_conn addr 100;</code>
<code> include /etc/nginx/mime.types;</code>
<code> default_type text/html;</code>
<code> charset UTF-8;</code>
<code> gzip on;</code>
<code> gzip_disable "msie6";</code>
<code> gzip_proxied any;</code>
<code> gzip_min_length 1000;</code>
<code> gzip_comp_level 6;</code>
<code> gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;</code>
<code> open_file_cache max=100000 inactive=20s;</code>
<code> open_file_cache_valid 30s;</code>
<code> open_file_cache_min_uses 2;</code>
<code> open_file_cache_errors on;</code>
<code> include /etc/nginx/conf.d/*.conf;</code>
<code> include /etc/nginx/sites-enabled/*;</code>
<code>}After editing, restart Nginx to apply the changes:
sudo service nginx restartConclusion
Your web server is now tuned for better performance under heavy traffic. This is one of many ways to accelerate a site; future articles will explore additional optimization techniques.
Original source: http://blog.zachorr.com/nginx-setup/
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.
