Operations 5 min read

6 Nginx Configuration Optimizations for High-Concurrency Performance

This article details six essential Nginx configuration optimizations for high-concurrency scenarios, covering worker processes, connection limits, epoll event model, TCP reuse via sendfile and TCP options, HTTP keepalive, and upstream connection pooling with concrete examples and configuration snippets.

Architect Chen
Architect Chen
Architect Chen
6 Nginx Configuration Optimizations for High-Concurrency Performance

1. Optimize Worker Processes

Nginx uses a master-worker multi-process model. The worker_processes directive determines the number of worker processes.

Recommended setting: worker_processes auto; With auto, Nginx automatically sets the worker count based on CPU cores. For example, on an 8-core CPU, this yields 8 worker processes, avoiding manual misconfiguration that wastes CPU resources.

2. Optimize Worker Connections

The worker_connections directive sets the maximum connections per worker. Combined with worker_rlimit_nofile to raise the file descriptor limit.

worker_rlimit_nofile 65535;

events {
    worker_connections 65535;
}

With 8 workers and 65535 connections each, theoretical capacity is ~520,000 connections (8 × 65535). However, actual limits depend on file descriptors, memory, CPU, network bandwidth, and upstream service capacity.

3. Enable Efficient Event Model (epoll)

On Linux, use the epoll event model for high concurrency:

events {
    use epoll;
    worker_connections 65535;
}

Traditional models (select/poll) traverse all connections to find active ones, causing CPU overhead proportional to total connections. Epoll only processes active connections, significantly reducing CPU waste under heavy load.

Linux production environments can explicitly set use epoll or rely on Nginx's auto-selection.

4. Enable TCP Connection Reuse

① Enable sendfile

sendfile on;

Traditional file transfer copies data from disk → user space → kernel space → user space → network card (4 context switches). With sendfile on, data moves directly from disk → kernel → network card (2 context switches), eliminating user-space copies. Especially valuable for static files, images, and video.

② Enable tcp_nopush

sendfile on;
tcp_nopush on;

Used with sendfile to reduce network packet count, improving large-file transfer efficiency.

③ Enable tcp_nodelay

tcp_nodelay on;

Reduces latency for small TCP packets. Common production configuration combines all three:

sendfile on;
tcp_nopush on;
tcp_nodelay on;

5. Enable HTTP Keepalive

In high-concurrency scenarios, avoid establishing a new TCP connection per HTTP request. Enable keepalive:

keepalive_timeout 65;
keepalive_requests 1000;

This allows clients to reuse connections for multiple requests, reducing TCP three-way handshakes, connection teardowns, system calls, and CPU consumption. Ideal for APIs, microservices, and long-lived HTTP connections.

6. Optimize Reverse Proxy Connection Pool

When Nginx acts as a gateway/reverse proxy, optimize the upstream block with keepalive connections to backend servers:

upstream backend {
    server 10.0.0.1:8080;
    server 10.0.0.2:8080;
    keepalive 128;
}

In the location block, use HTTP/1.1 and clear the Connection header to enable upstream keepalive:

location / {
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_pass http://backend;
}

The keepalive 128 directive maintains up to 128 idle persistent connections to each upstream server, reducing connection establishment overhead.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

high-concurrencysendfileNginxReverse Proxyworker-processesepollkeepaliveconfiguration-optimization
Architect Chen
Written by

Architect Chen

Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.